Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<drop_column>
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import ExtraTreesRegressor, RandomForestRegressor, GradientBoostingRegressor
from sklearn.svm import SVR
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
from sklearn.preprocessing import PolynomialFeatures
def remove_outliers_swim(data):
print('Number of swim distance outliers: ' + str(len(data.loc[data['swimDistance'] > 500])))
 data.drop(data.loc[data['swimDistance'] > 500].index, inplace=True)
 return str('Swim distance outliers removed.' )<drop_column>
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 sub = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )
remove_outliers_swim(df_train )<feature_engineering>
def fuckProvince(s):
if pd.isnull(s['Province_State']):
if pd.isnull(s['Country_Region']):
return 'Null'
else:
return s['Country_Region']
else:
return s['Province_State']
df_train['total_distance'] = df_train.rideDistance + df_train.swimDistance + df_train.walkDistance<feature_engineering>
train['Province_State'] = train.apply(fuckProvince,axis=1)
 test['Province_State'] = test.apply(fuckProvince,axis=1 )
def remove_movecheat(data):
data['killsWithoutMoving'] =(( data['kills'] > 0)&(data['total_distance'] == 0))
print('Number of movement cheaters: ' + str(len(data.loc[data['killsWithoutMoving'] == True])))
 data.drop(data.loc[data['killsWithoutMoving'] == True].index, inplace=True)
 return str('Cheaters removed.' )<drop_column>
tr = train.copy()
ts = test.copy()
remove_movecheat(df_train )<filter>
X = train.drop(['ConfirmedCases','Fatalities'],axis=1)
 y = train[['ConfirmedCases','Fatalities']]
y1 = train['ConfirmedCases']
y2 = train['Fatalities']
def remove_outliers_road(data):
roadk1_mnPlus3Std = data[data['roadKills'] >= 1]['roadKills'].mean() + 3*data[data['roadKills'] >= 1]['roadKills'].std()
print('Number of vehicle kill outliers: ' + str(len(data.loc[data['roadKills'] > roadk1_mnPlus3Std])))
 data.drop(data.loc[data['roadKills'] > roadk1_mnPlus3Std].index, inplace=True)
 return str('Vehicle kill outliers removed.' )<drop_column>
def fuckDate(s):
return s.month * 100 + s.day
remove_outliers_road(df_train )<filter>
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'] )
def remove_outliers_kills_old(data):
kills_mnPlus3Std = data['kills'].mean() + 3*data['kills'].std()
print('Number of kill outliers: ' + str(len(data.loc[data['kills'] > kills_mnPlus3Std])))
 data.drop(data.loc[data['kills'] > kills_mnPlus3Std].index, inplace=True)
 return str('Kill outliers removed.' )<drop_column>
train['Fucking_Useful_Dates'] = train['Date'].apply(fuckDate)
 test['Fucking_Useful_Dates'] = test['Date'].apply(fuckDate )
def remove_outliers_kills(data):
print('Number of kill outliers: ' + str(len(data.loc[data['kills'] > 20])))
 data.drop(data.loc[data['kills'] > 20].index, inplace=True)
 return str('Kill outliers removed.' )<drop_column>
train['Month_People_Were_Fucked'] = train['Date'].dt.month
test['Month_People_Were_Fucked'] = test['Date'].dt.month
remove_outliers_kills(df_train )<drop_column>
train.drop(['Date'],axis=1,inplace=True)
 test.drop(['Date'],axis=1,inplace=True )
def remove_outliers_longkills(data):
print('Number of longest kill outliers: ' + str(len(data.loc[data['longestKill'] > 800])))
 data.drop(data.loc[data['longestKill'] > 800].index, inplace=True)
 return str('Longest kill outliers removed.' )<drop_column>
pred = pd.DataFrame(columns=['ForecastId','ConfirmedCases','Fatalities'] )
remove_outliers_longkills(df_train )<filter>
pred['ForecastId'] = test['ForecastId'].copy()
print('Number of players who acquired more than 50 weapons: ' + str(len(df_train[df_train['weaponsAcquired'] > 50])))
<drop_column>
LE = LabelEncoder()
train['Province_State'] = LE.fit_transform(train['Province_State'])
 test['Province_State'] = LE.transform(test['Province_State'])
 LE2 = LabelEncoder()
train['Country_Region'] = LE2.fit_transform(train['Country_Region'])
 test['Country_Region'] = LE2.transform(test['Country_Region'] )
def remove_outliers_weapons(data):
print('Number of weapons acquired outliers: ' + str(len(data.loc[data['weaponsAcquired'] > 50])))
 data.drop(data.loc[data['weaponsAcquired'] > 50].index, inplace=True)
 return str('Weapons acquired outliers removed.' )<drop_column>
for state in tr['Province_State'].unique() :
try:
tr_in = tr[ tr['Province_State'] == state ].index
ts_in = ts[ ts['Province_State'] == state ].index
ids = ts[ ts['Province_State'] == state ].loc[:,'ForecastId']
X_train = train.iloc[tr_in,:].drop(['ConfirmedCases','Fatalities','Id'],axis=1)
 y1_train = train.iloc[tr_in,:].loc[:,'ConfirmedCases']
y2_train = train.iloc[tr_in,:].loc[:,'Fatalities']
X_test = test.iloc[ts_in, :].drop(['ForecastId'],axis=1)
 pred.at[ts_in,'ForecastId'] = ids
model1 = XGBRegressor(n_estimators = 3000)
 model1.fit(X_train,y1_train)
 pred.at[ts_in,'ConfirmedCases'] = model1.predict(X_test)
 model2 = XGBRegressor(n_estimators = 3000)
 model2.fit(X_train,y2_train)
 pred.at[ts_in,'Fatalities'] = model2.predict(X_test)
 except:
print(state )
remove_outliers_weapons(df_train )<feature_engineering>
pred['ConfirmedCases'] = np.round(pred['ConfirmedCases'].astype(np.double),0)
 pred['Fatalities'] = np.round(pred['Fatalities'].astype(np.double),0 )
df_train['heals_boosts'] = df_train['heals'] + df_train['boosts']<filter>
pred.to_csv('submission.csv',index=False )