Kaggle competition: COVID19 Global Forecasting (Week 3)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-week-3<rename_columns>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
df_train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/train.csv')
 df_test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/test.csv')
Tourney_Win_Results.rename(columns={'WinningSeed':'Seed1', 'LoosingSeed':'Seed2', 'WScoreTotal':'ScoreT1', 'LScoreTotal':'ScoreT2'}, inplace=True )<prepare_output>
def fillState(state, country):
if state == "NA": return country
return state
def fixData(input_set):
input_set['Province_State'].fillna("NA", inplace=True)
 input_set['Province_State'] = input_set.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 input_set['Date'] = pd.to_datetime(input_set['Date'], infer_datetime_format=True)
 input_set.loc[:, 'Date'] = input_set.Date.dt.strftime("%m%d")
 input_set["Date"] = input_set["Date"].astype(int)
 return input_set
tourney_lose_result = Tourney_Win_Results.copy()
tourney_lose_result['Seed1'] = Tourney_Win_Results['Seed2']
tourney_lose_result['Seed2'] = Tourney_Win_Results['Seed1']
tourney_lose_result['ScoreT1'] = Tourney_Win_Results['ScoreT2']
tourney_lose_result['ScoreT2'] = Tourney_Win_Results['ScoreT1']
tourney_lose_result<feature_engineering>
X_train = df_train
X_test = df_test
X_train = fixData(X_train)
 X_test = fixData(X_test)
 X_train.head()
Tourney_Win_Results['Seed_diff'] = Tourney_Win_Results['Seed1'] - Tourney_Win_Results['Seed2']
Tourney_Win_Results['ScoreT_diff'] = Tourney_Win_Results['ScoreT1'] - Tourney_Win_Results['ScoreT2']
tourney_lose_result['Seed_diff'] = tourney_lose_result['Seed1'] - tourney_lose_result['Seed2']
tourney_lose_result['ScoreT_diff'] = tourney_lose_result['ScoreT1'] - tourney_lose_result['ScoreT2']<save_to_csv>
Tourney_Win_Results['result'] = 1
tourney_lose_result['result'] = 0
tourney_result_Final = pd.concat(( Tourney_Win_Results, tourney_lose_result)).reset_index(drop=True)
 tourney_result_Final.to_csv('Tourneyvalidate.csv', index=False )<drop_column>
label_encoder = preprocessing.LabelEncoder()
countries = X_test.Country_Region.unique()
tourney_result_Final1 = tourney_result_Final[[
'Seed1', 'Seed2', 'ScoreT1', 'ScoreT2', 'Seed_diff', 'ScoreT_diff', 'result']]<feature_engineering>
sub = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 sub = []
for country in countries:
province_list = X_train.loc[X_train['Country_Region'] == country].Province_State.unique()
for province in province_list:
X_train2 = X_train.loc[(X_train['Country_Region'] == country)&(X_train['Province_State'] == province),['Date']].astype('int')
 Y_train21 = X_train.loc[(X_train['Country_Region'] == country)&(X_train['Province_State'] == province),['ConfirmedCases']]
Y_train22 = X_train.loc[(X_train['Country_Region'] == country)&(X_train['Province_State'] == province),['Fatalities']]
X_test2 = X_test.loc[(X_test['Country_Region'] == country)&(X_test['Province_State'] == province), ['Date']].astype('int')
 X_forecastId2 = X_test.loc[(X_test['Country_Region'] == country)&(X_test['Province_State'] == province), ['ForecastId']]
X_forecastId2 = X_forecastId2.values.tolist()
X_forecastId2 = [v[0] for v in X_forecastId2]
model2 = XGBRegressor(n_estimators=1020)
 model2.fit(X_train2, Y_train21)
 Y_pred2 = model2.predict(X_test2)
 model3 = XGBRegressor(n_estimators=1020)
 model3.fit(X_train2, Y_train22)
 Y_pred3 = model3.predict(X_test2)
 for j in range(len(Y_pred2)) :
dic = { 'ForecastId': X_forecastId2[j], 'ConfirmedCases': Y_pred2[j], 'Fatalities': Y_pred3[j]}
sub.append(dic)
tourney_result_Final1.loc[lambda x:(x['Seed1'].isin([14,15,16])) &(x['Seed2'].isin([1,2,3])) ,'result'
] = 0 
<load_from_csv>
submission = pd.DataFrame(sub)
 submission[['ForecastId','ConfirmedCases','Fatalities']].to_csv(path_or_buf='submission.csv',index=False )
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' )<feature_engineering>
test_df['Season'] = test_df['ID'].map(lambda x: int(x[:4]))
test_df['WTeamID'] = test_df['ID'].map(lambda x: int(x[5:9]))
test_df['LTeamID'] = test_df['ID'].map(lambda x: int(x[10:14]))
test_df<merge>