Kaggle competition: COVID19 Global Forecasting (Week 4)
content_explation_agg=train_df[["content_id","prior_question_had_explanation",target]].groupby(["content_id","prior_question_had_explanation"])[target].agg(['mean'] )<rename_columns>
PATH_WEEK4='/kaggle/input/covid19-global-forecasting-week-4'
df_train = pd.read_csv(f'{PATH_WEEK4}/train.csv')
 df_test = pd.read_csv(f'{PATH_WEEK4}/test.csv' )
content_explation_agg=content_explation_agg.unstack()
content_explation_agg=content_explation_agg.reset_index()
content_explation_agg.columns = ['content_id', 'content_explation_false_mean','content_explation_true_mean']<feature_engineering>
mitigation = pd.read_csv('/kaggle/input/mitigation-day/mitigations.csv')
 mitigation.loc[mitigation['Name']=="United States",'Name']='US'
mitigation['start'] = pd.to_datetime(mitigation['start'], infer_datetime_format=True )
content_explation_agg['explation_impact'] =(( content_explation_agg['content_explation_true_mean'] -
content_explation_agg['content_explation_false_mean'])/
content_explation_agg['content_explation_false_mean'])
 content_explation_agg['explation_impact'] = np.where(content_explation_agg['explation_impact'] < 0, 0, content_explation_agg['explation_impact'])
 content_explation_agg['explation_impact'] = np.where(content_explation_agg['content_explation_false_mean']==0, content_explation_agg['content_explation_true_mean'], content_explation_agg['explation_impact'] )<data_type_conversions>
df_train.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_test.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_train.rename(columns={'Province_State':'State'}, inplace=True)
 df_test.rename(columns={'Province_State':'State'}, inplace=True)
 df_train['Date'] = pd.to_datetime(df_train['Date'], infer_datetime_format=True)
 df_test['Date'] = pd.to_datetime(df_test['Date'], infer_datetime_format=True )
content_explation_agg.content_id=content_explation_agg.content_id.astype('int16')
 content_explation_agg.content_explation_false_mean=content_explation_agg.content_explation_false_mean.astype('float16')
 content_explation_agg.content_explation_true_mean=content_explation_agg.content_explation_true_mean.astype('float16')
 content_explation_agg.explation_impact = content_explation_agg.explation_impact.astype('float16' )<train_model>
NULL_VAL = "NULL_VAL"
def fillState(state, country):
if state == NULL_VAL:
return country
return state +':' + country
def fillState2(state, country):
if type(state)==str:
return country
return state +':' + country
X_Train = df_train.loc[:, ['State', 'Country', 'Date', 'ConfirmedCases', 'Fatalities']]
X_Train['State'].fillna(NULL_VAL, inplace=True)
 X_Train['State'] = X_Train.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 firstDay = np.min(X_Train['Date'])
 X_Train.loc[:, 'Date'] =(X_Train['Date']-np.min(X_Train['Date'])).values / 86400000000000
X_Train["Date"] = X_Train["Date"].astype(int)
 X_Test = df_test.loc[:, ['State', 'Country', 'Date', 'ForecastId']]
X_Test['State'].fillna(NULL_VAL, inplace=True)
 X_Test['State'] = X_Test.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Test.loc[:, 'Date'] =(X_Test['Date']-firstDay ).values / 86400000000000
X_Test["Date"] = X_Test["Date"].astype(int )
print('start handle attempt_no...' )<data_type_conversions>
mitigation['start_day'] =(( mitigation['start'] - firstDay ).values / 86400000000000 ).astype(int )
train_df["attempt_no"] = 1
train_df.attempt_no=train_df.attempt_no.astype('int8')
 attempt_no_agg=train_df.groupby(["user_id","content_id"])["attempt_no"].agg(['sum'] ).astype('int8')
 train_df["attempt_no"] = train_df[["user_id","content_id",'attempt_no']].groupby(["user_id","content_id"])["attempt_no"].cumsum()<filter>
le = LabelEncoder()
countries = X_Train.Country.unique()
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in countries:
states = X_Train.loc[X_Train.Country == country, :].State.unique()
for state in states:
condition_train =(X_Train.Country == country)&(X_Train.State == state)
 X_Train_CS = X_Train.loc[condition_train, ['Date', 'ConfirmedCases', 'Fatalities']]
y1_Train_CS = X_Train_CS.loc[:, 'ConfirmedCases']
y2_Train_CS = X_Train_CS.loc[:, 'Fatalities']
X_Train_CS = X_Train_CS.loc[:, ['Date']]
condition_test =(X_Test.Country == country)&(X_Test.State == state)
 X_Test_CS = X_Test.loc[condition_test, ['Date', 'ForecastId']]
X_Test_CS_Id = X_Test_CS.loc[:, 'ForecastId']
X_Test_CS = X_Test_CS.loc[:, ['Date']]
row = mitigation[mitigation['State'] == state]
if row.shape[0]>0:
day = row['start_day'].values[0]
print("use mitigation", state, day)
 X_Train_CS['Mitigation'] =(X_Train_CS['Date'] - day)> 0
X_Train_CS['Mitigation_day'] = X_Train_CS['Date'] - day
X_Train_CS.loc[X_Train_CS['Mitigation_day']<0, 'Mitigation_day'] = 0
X_Test_CS['Mitigation'] =(X_Test_CS['Date'] - day)> 0
X_Test_CS['Mitigation_day'] = X_Test_CS['Date'] - day
X_Test_CS.loc[X_Test_CS['Mitigation_day']<0, 'Mitigation_day'] = 0
else:
X_Train_CS['Mitigation'] = False
X_Train_CS['Mitigation_day'] = 0
X_Test_CS['Mitigation'] = False
X_Test_CS['Mitigation_day'] = 0
model1 = XGBRegressor(n_estimators=1000)
 model1.fit(X_Train_CS, y1_Train_CS)
 y1_pred = model1.predict(X_Test_CS)
 model2 = XGBRegressor(n_estimators=1000)
 model2.fit(X_Train_CS, y2_Train_CS)
 y2_pred = model2.predict(X_Test_CS)
 df = pd.DataFrame({'ForecastId': X_Test_CS_Id, 'ConfirmedCases': y1_pred, 'Fatalities': y2_pred})
 df_out = pd.concat([df_out, df], axis=0)
 df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.tail()
attempt_no_agg=attempt_no_agg[attempt_no_agg['sum'] >1]<data_type_conversions>
df_out.to_csv('submission.csv', index=False )