Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<concatenate>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()<feature_engineering>
train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv" )
match = all_data.groupby('matchId')
 all_data['killsPerc'] = match['kills'].rank(pct=True ).values
all_data['killPlacePerc'] = match['killPlace'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
all_data['walkPerc_killsPerc'] = all_data['walkDistancePerc'] / all_data['killsPerc']<data_type_conversions>
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'] )
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance'] 
<data_type_conversions>
train = train[train['Date']<='2020-04-14']
train['Location'] = train['Province_State'].astype(str)+ train['Country_Region'].astype(str)
 test['Location'] = test['Province_State'].astype(str)+ test['Country_Region'].astype(str)
 test = test.merge(train[['ConfirmedCases','Fatalities','Location','Date']],how='left',on=['Location','Date'])
 train = train[train['Date']<='2020-04-01']
data = pd.concat([train,test],axis=0)
 data = data.sort_values(['Country_Region','Date'] )
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )<feature_engineering>
data.drop(['Id','Province_State','Country_Region'],axis=1,inplace=True )
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
fillInf(all_data, 0 )<drop_column>
del train,test
gc.collect()
all_data.drop(['boosts','heals','killStreaks','DBNOs'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )<groupby>
poly = PolynomialFeatures(degree=4)
match = all_data.groupby(['matchId'])
 group = all_data.groupby(['matchId','groupId','matchType'])
 agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']<categorify>
Res = None
for i in tqdm(locations):
df = data[data['Location']==i].reset_index().drop(['index'],axis=1 ).reset_index()
X_c = poly.fit_transform(np.array(df['index'] ).reshape(-1,1))
df[[0,1,2,3,4]] = pd.DataFrame(X_c)
 train = df[df['Date']<='2020-04-14'][['ForecastId','ConfirmedCases','Fatalities',0,1,2,3,4]]
test = df[df['Date']>='2020-04-15'][['ForecastId','ConfirmedCases','Fatalities',0,1,2,3,4]]
model = LinearRegression()
model.fit(train.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1),train['ConfirmedCases'])
 test['ConfirmedCases']=model.predict(test.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1))
model = LinearRegression()
model.fit(train.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1),train['Fatalities'])
 test['Fatalities']=model.predict(test.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1))
pred = pd.concat([train[-13:][['ForecastId','ConfirmedCases','Fatalities']],test[['ForecastId','ConfirmedCases','Fatalities']]])
 Res = pd.concat([Res,pred],axis=0 )
minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
match_data = pd.merge(match_data, minKills[nKills][['matchId','groupId',c]], how='left')
 match_data = reduce_mem_usage(match_data)
 del minKills, nKills
print(match_data.shape )<data_type_conversions>
Res['ForecastId'] = Res['ForecastId'].astype(int )
<merge>
Res.to_csv("submission.csv",index=False )