Kaggle competition: COVID19 Global Forecasting (Week 4)
magic = submission['F2'].sum()
japan_emergency = 119
submission.loc[submission['id'].str.contains('CA_1'), 'F2'] *= 0.994
submission.loc[submission['id'].str.contains('CA_2'), 'F2'] *= 0.998
submission.loc[submission['id'].str.contains('CA_3'), 'F2'] *= 0.994
submission.loc[submission['id'].str.contains('CA_4'), 'F2'] *= 0.996
submission.loc[submission['id'].str.contains('TX_1'), 'F2'] *= 0.995
submission.loc[submission['id'].str.contains('TX_2'), 'F2'] *= 0.996
submission.loc[submission['id'].str.contains('TX_3'), 'F2'] *= 0.996
submission.loc[submission['id'].str.contains('WI_1'), 'F2'] *= 0.998
submission.loc[submission['id'].str.contains('WI_2'), 'F2'] *= 1
submission.loc[submission['id'].str.contains('WI_3'), 'F2'] *= 0.998
submission['F2'].sum()<feature_engineering>
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.loc[submission['id'].str.contains('CA_1'), 'F3'] *= 0.994
submission.loc[submission['id'].str.contains('CA_2'), 'F3'] *= 0.998
submission.loc[submission['id'].str.contains('CA_3'), 'F3'] *= 0.994
submission.loc[submission['id'].str.contains('CA_4'), 'F3'] *= 0.996
submission.loc[submission['id'].str.contains('TX_1'), 'F3'] *= 0.995
submission.loc[submission['id'].str.contains('TX_2'), 'F3'] *= 0.996
submission.loc[submission['id'].str.contains('TX_3'), 'F3'] *= 0.996
submission.loc[submission['id'].str.contains('WI_1'), 'F3'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F3'] *= 1.005
submission.loc[submission['id'].str.contains('WI_3'), 'F3'] *= 1
submission['F3'].sum()<feature_engineering>
train.rename(columns={'Country_Region':'Country'}, inplace=True)
 test.rename(columns={'Country_Region':'Country'}, inplace=True)
 train.rename(columns={'Province_State':'State'}, inplace=True)
 test.rename(columns={'Province_State':'State'}, inplace=True)
 train['Date'] = pd.to_datetime(train['Date'], infer_datetime_format=True)
 test['Date'] = pd.to_datetime(test['Date'], infer_datetime_format=True)
 train.info()
test.info()
sex_constant = 1.0073
submission.loc[submission['id'].str.contains('CA_1'), 'F4'] *= 0.98
submission.loc[submission['id'].str.contains('CA_2'), 'F4'] *= 1.02
submission.loc[submission['id'].str.contains('CA_3'), 'F4'] *= 0.98
submission.loc[submission['id'].str.contains('CA_4'), 'F4'] *= 1
submission.loc[submission['id'].str.contains('TX_1'), 'F4'] *= 0.99
submission.loc[submission['id'].str.contains('TX_2'), 'F4'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F4'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F4'] *= 1.00
submission.loc[submission['id'].str.contains('WI_2'), 'F4'] *= 1.02
submission.loc[submission['id'].str.contains('WI_3'), 'F4'] *= 1.00
submission['F4'].sum()<feature_engineering>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
color_of_the_night = 995874
color_of_the_night /= 1000000
submission.loc[submission['id'].str.contains('CA_1'), 'F5'] *= 0.995
submission.loc[submission['id'].str.contains('CA_2'), 'F5'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F5'] *= 0.995
submission.loc[submission['id'].str.contains('CA_4'), 'F5'] *= 0.998
submission.loc[submission['id'].str.contains('TX_1'), 'F5'] *= 0.996
submission.loc[submission['id'].str.contains('TX_2'), 'F5'] *= 0.998
submission.loc[submission['id'].str.contains('TX_3'), 'F5'] *= 0.998
submission.loc[submission['id'].str.contains('WI_1'), 'F5'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F5'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F5'] *= 1
submission['F5'].sum()<feature_engineering>
X_xTrain = train.copy()
X_xTrain['State'].fillna(EMPTY_VAL, inplace=True)
 X_xTrain['State'] = X_xTrain.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_xTrain.loc[:, 'Date'] = X_xTrain.Date.dt.strftime("%m%d")
 X_xTrain["Date"] = X_xTrain["Date"].astype(int)
 X_xTrain.tail()
injury = 1.000376
submission.loc[submission['id'].str.contains('CA_1'), 'F6'] *= 1.0002
submission.loc[submission['id'].str.contains('CA_2'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('CA_3'), 'F6'] *= 1.0002
submission.loc[submission['id'].str.contains('CA_4'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('TX_1'), 'F6'] *= 1.000
submission.loc[submission['id'].str.contains('TX_2'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('TX_3'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('WI_1'), 'F6'] *= 1.001
submission.loc[submission['id'].str.contains('WI_2'), 'F6'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F6'] *= 1.001
submission['F6'].sum()<feature_engineering>
X_xTest = test.copy()
X_xTest['State'].fillna(EMPTY_VAL, inplace=True)
 X_xTest['State'] = X_xTest.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_xTest.loc[:, 'Date'] = X_xTest.Date.dt.strftime("%m%d")
 X_xTest["Date"] = X_xTest["Date"].astype(int)
 X_xTest.tail()
np.random.seed(198505)
 correction = np.random.randint(1000000)/1000000
submission.loc[submission['id'].str.contains('CA_1'), 'F7'] *= 0.994
submission.loc[submission['id'].str.contains('CA_2'), 'F7'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F7'] *= 0.994
submission.loc[submission['id'].str.contains('CA_4'), 'F7'] *= 0.998
submission.loc[submission['id'].str.contains('TX_1'), 'F7'] *= 0.995635
submission.loc[submission['id'].str.contains('TX_2'), 'F7'] *= 0.998
submission.loc[submission['id'].str.contains('TX_3'), 'F7'] *= 0.998
submission.loc[submission['id'].str.contains('WI_1'), 'F7'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F7'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F7'] *= 1
submission['F7'].sum()<feature_engineering>
le = LabelEncoder()
X_xTrain.Country = le.fit_transform(X_xTrain.Country)
 X_xTrain['State'] = le.fit_transform(X_xTrain['State'])
 X_xTest.Country = le.fit_transform(X_xTest.Country)
 X_xTest['State'] = le.fit_transform(X_xTest['State'] )
submission.loc[submission['id'].str.contains('CA_1'), 'F8'] *= 0.996
submission.loc[submission['id'].str.contains('CA_2'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F8'] *= 0.996
submission.loc[submission['id'].str.contains('CA_4'), 'F8'] *= 0.998
submission.loc[submission['id'].str.contains('TX_1'), 'F8'] *= 0.9988
submission.loc[submission['id'].str.contains('TX_2'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F8'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F8'] *= 1.01
submission.loc[submission['id'].str.contains('WI_3'), 'F8'] *= 1
submission['F8'].sum()<feature_engineering>
filterwarnings('ignore')
 le = LabelEncoder()
countries = X_xTrain.Country.unique()
submission.loc[submission['id'].str.contains('CA_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_3'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('CA_4'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('TX_3'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_1'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_2'), 'F11'] *= 1
submission.loc[submission['id'].str.contains('WI_3'), 'F11'] *= 1
submission['F11'].sum()<categorify>
xout = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in countries:
states = X_xTrain.loc[X_xTrain.Country == country, :].State.unique()
for state in states:
X_xTrain_CS = X_xTrain.loc[(X_xTrain.Country == country)&(X_xTrain.State == state), ['State', 'Country', 'Date', 'ConfirmedCases', 'Fatalities']]
y1_xTrain_CS = X_xTrain_CS.loc[:, 'ConfirmedCases']
y2_xTrain_CS = X_xTrain_CS.loc[:, 'Fatalities']
X_xTrain_CS = X_xTrain_CS.loc[:, ['State', 'Country', 'Date']]
X_xTrain_CS.Country = le.fit_transform(X_xTrain_CS.Country)
 X_xTrain_CS['State'] = le.fit_transform(X_xTrain_CS['State'])
 X_xTest_CS = X_xTest.loc[(X_xTest.Country == country)&(X_xTest.State == state), ['State', 'Country', 'Date', 'ForecastId']]
X_xTest_CS_Id = X_xTest_CS.loc[:, 'ForecastId']
X_xTest_CS = X_xTest_CS.loc[:, ['State', 'Country', 'Date']]
X_xTest_CS.Country = le.fit_transform(X_xTest_CS.Country)
 X_xTest_CS['State'] = le.fit_transform(X_xTest_CS['State'])
 param = {'max_depth':range(2,6),'n_estimators':[100,500,1000]}
xgb1 =XGBRegressor()
x1=GridSearchCV(xgb1,param_grid=param,cv=3,scoring='neg_mean_squared_error')
 x1.fit(X_xTrain_CS, y1_xTrain_CS)
 y1_xpred = x1.predict(X_xTest_CS)
 xgb2 = XGBRegressor()
x2=GridSearchCV(xgb2,param_grid=param,cv=3,scoring='neg_mean_squared_error')
 x2.fit(X_xTrain_CS, y2_xTrain_CS)
 y2_xpred = x2.predict(X_xTest_CS)
 print(X_xTrain_CS.shape)
 xdata = pd.DataFrame({'ForecastId': X_xTest_CS_Id, 'ConfirmedCases': y1_xpred, 'Fatalities': y2_xpred})
 xout = pd.concat([xout, xdata], axis=0 )
for i in range(9,20):
if i!=11:
submission.loc[submission['id'].str.contains('CA_1'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('CA_2'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('CA_3'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('CA_4'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('TX_1'), 'F'+str(i)] *= 1
submission.loc[submission['id'].str.contains('TX_2'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('TX_3'), 'F'+str(i)] *= 1.013
submission.loc[submission['id'].str.contains('WI_1'), 'F'+str(i)] *= 1.015
submission.loc[submission['id'].str.contains('WI_2'), 'F'+str(i)] *= 1.02
submission.loc[submission['id'].str.contains('WI_3'), 'F'+str(i)] *= 1.015
print(submission['F'+str(i)].sum() )<categorify>
xout.ForecastId = xout.ForecastId.astype('int')
 xout.to_csv('submission.csv', index=False )