Kaggle competition: COVID19 Global Forecasting (Week 4)
warnings.simplefilter('ignore' )<load_from_csv>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
from sklearn import preprocessing
from xgboost import XGBRegressor
from sklearn.tree import DecisionTreeRegressor
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
test_set = pd.read_csv(".. /input/test_V2.csv")
 train = pd.read_csv(".. /input/train_V2.csv" )<correct_missing_values>
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )
train.dropna(inplace=True )<count_missing_values>
xtrain=train[['Id']]
test['Id']=test['ForecastId']
xtest=test[['Id']]
ytrain_cc=train[['ConfirmedCases']]
ytrain_ft=train[['Fatalities']]
print(f'Nan values: {train.isnull().values.any() }' )<define_variables>
xtr=np.array_split(xtrain,313)
 ycc=np.array_split(ytrain_cc,313)
 yft=np.array_split(ytrain_ft,313)
 xte=np.array_split(xtest,313 )
feature_list = ['assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill',
'rankPoints', 'revives', 'rideDistance', 'roadKills',
'swimDistance', 'teamKills', 'vehicleDestroys', 'walkDistance',
'weaponsAcquired', 'winPoints']<categorify>
a=np.max(xtr[0] ).values
b=a-71
b=b[0]
for x in feature_list:
train[f'match_{x}'] = train.groupby('matchId')[x].transform(sum)
 test_set[f'match_{x}'] = test_set.groupby('matchId')[x].transform(sum )<feature_engineering>
xte[0]=xte[0]+a
for i in range(312):
xte[i+1]=xte[0]
for i in range(312):
xtr[i+1]=xtr[0]
for x in feature_list:
train[f'{x}_rate'] =(train[x] / train[f'match_{x}'] ).replace([np.inf, -np.inf, np.nan], 0)
 test_set[f'{x}_rate'] =(test_set[x] / test_set[f'match_{x}'] ).replace([np.inf, -np.inf, np.nan], 0 )<count_missing_values>
poly=PolynomialFeatures(3)
 y_pred_cc=[]
for i in range(313):
xtr[i]=poly.fit_transform(xtr[i])
 xte[i]=poly.fit_transform(xte[i])
 model=XGBRegressor()
model.fit(xtr[i],ycc[i]);
y_pr_cc=model.predict(xte[i])
 ycc[i]= ycc[i][71:]
y_pr_cc=y_pr_cc[b:]
y_pr_cc=np.append(ycc[i], y_pr_cc)
 y_pred_cc.append(y_pr_cc )
print(f'Nan values: {train.isnull().values.any() }' )<prepare_x_and_y>
y_pred_ft=[]
for i in range(313):
model=XGBRegressor()
model.fit(xtr[i],yft[i]);
y_pr_ft=model.predict(xte[i])
 yft[i]= yft[i][71:]
y_pr_ft=y_pr_ft[b:]
y_pr_ft=np.append(yft[i], y_pr_ft)
 y_pred_ft.append(y_pr_ft )
y = train['winPlacePerc']
X = train.drop(['winPlacePerc','matchType','Id','groupId','matchId'],axis=1)
 test_pred = test_set.drop(['matchType','Id','groupId','matchId'],axis=1 )<categorify>
y_pred_ft_1 = [item for sublist in y_pred_ft for item in sublist]
y_pred_cc_1 = [item for sublist in y_pred_cc for item in sublist]
match = pd.get_dummies(train['matchType'],drop_first=False)
 tmatch = pd.get_dummies(test_set['matchType'],drop_first=False )<concatenate>
df_final=pd.DataFrame({'ForecastId':submission.ForecastId, 'ConfirmedCases':y_pred_cc_1, 'Fatalities':y_pred_ft_1})
 df_final.to_csv('/kaggle/working/submission.csv', index=False)
keep_list = ['squad-fpp','squad','solo-fpp','solo']
X = pd.concat([X,match[keep_list]],axis=1)
 test_pred = pd.concat([test_pred,tmatch[keep_list]],axis=1 )<drop_column>
data=pd.read_csv('/kaggle/working/submission.csv')
 data.head(20 )