Kaggle competition: COVID19 Global Forecasting (Week 2)
application_train = application_train.merge(right=avg_pos.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_pos.reset_index() , how='left', on='SK_ID_CURR' )<merge>
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
application_train = application_train.merge(right=avg_installments.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_installments.reset_index() , how='left', on='SK_ID_CURR' )<merge>
path = '/kaggle/input/covid19-global-forecasting-week-2/'
df_train = pd.read_csv(path+'train.csv')
 df_test = pd.read_csv(path+'test.csv' )
application_train = application_train.merge(right=avg_credit_card.reset_index() , how='left', on='SK_ID_CURR')
 application_test = application_test.merge(right=avg_credit_card.reset_index() , how='left', on='SK_ID_CURR' )<split>
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 )
X = application_train
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 42 )<create_dataframe>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
train_data=lgb.Dataset(x_train,label=y_train)
 test_data=lgb.Dataset(x_test,label=y_test )<init_hyperparams>
df_train['Province_State'].fillna(EMPTY_VAL, inplace=True)
 df_train['Province_State'] = df_train.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 df_train.loc[:, 'Date'] = df_train.Date.dt.strftime("%m%d")
 df_train["Date"] = df_train["Date"].astype(int)
 print(df_train.shape)
 df_train.head()
params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': {'binary_logloss', 'auc'},
'metric_freq': 1,
'is_training_metric': True,
'max_bin': 255,
'learning_rate': 0.1,
'num_leaves': 63,
'tree_learner': 'serial',
'feature_fraction': 0.8,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'min_data_in_leaf': 50,
'min_sum_hessian_in_leaf': 5,
'is_enable_sparse': True,
'use_two_round_loading': False,
'is_save_binary_file': False,
'num_machines': 1,
'verbose': 0,
'subsample_for_bin': 200000,
'min_child_samples': 20,
'min_child_weight': 0.001,
'min_split_gain': 0.0,
'colsample_bytree': 1.0,
'reg_alpha': 0.0,
'reg_lambda': 0.0
}<train_model>
df_test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 df_test['Province_State'] = df_test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 df_test.loc[:, 'Date'] = df_test.Date.dt.strftime("%m%d")
 df_test["Date"] = df_test["Date"].astype(int)
 print(df_test.shape)
 df_test.head()
clf = lgb.train(params, train_data, 2000, valid_sets=test_data, early_stopping_rounds= 40, verbose_eval= 10 )<predict_on_test>
le = preprocessing.LabelEncoder()
df_train['Country_Region'] = le.fit_transform(df_train['Country_Region'])
 df_train['Province_State'] = le.fit_transform(df_train['Province_State'])
 df_test['Country_Region'] = le.fit_transform(df_test['Country_Region'])
 df_test['Province_State'] = le.fit_transform(df_test['Province_State'] )
y_prediction=clf.predict(application_train)
<compute_test_metric>
def build_model_1() :
model = RandomForestRegressor(n_estimators = 100, random_state = 0)
 return model
def build_model_2() :
model = XGBRegressor(n_estimators=1000)
 return model
def build_model_3() :
model = DecisionTreeRegressor(random_state=1)
 return model
def build_model_4() :
model = LogisticRegression()
return model
def build_model_5() :
model = LinearRegression()
return model
def build_model_6() :
model = LGBMRegressor(random_state=5)
 return model
def build_model_7() :
model = LGBMRegressor(iterations=2)
 return model
score = roc_auc_score(y, y_prediction)
 print("Overall AUC: {:.3f}".format(score))<predict_on_test>
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []} )
submit = clf.predict(application_test )<save_to_csv>
for country in range(len(unique_countries)) :
current_country_df_train = df_train.loc[df_train['Country_Region'] == unique_countries[country]]
current_country_df_test = df_test.loc[df_test['Country_Region'] == unique_countries[country]]
print("Current Country: ", unique_countries[country])
 X_train = current_country_df_train[['Country_Region', 'Province_State', 'Date']].to_numpy()
Y1_train = current_country_df_train[['ConfirmedCases']].to_numpy()
Y2_train = current_country_df_train[['Fatalities']].to_numpy()
X_test = current_country_df_test[['Country_Region', 'Province_State', 'Date']].to_numpy()
Y1_train = Y1_train.reshape(-1)
 Y2_train = Y2_train.reshape(-1)
 model1 = build_model_2()
model1.fit(X_train, Y1_train)
 res_cnf_cls = model1.predict(X_test)
 model2 = build_model_2()
model2.fit(X_train, Y2_train)
 res_fac = model2.predict(X_test)
 current_country_df_test_Id = current_country_df_test.loc[:, 'ForecastId']
df_ans = pd.DataFrame({'ForecastId': current_country_df_test_Id, 'ConfirmedCases': res_cnf_cls, 'Fatalities': res_fac})
 df_out = pd.concat([df_out, df_ans], axis=0)
application_test['TARGET'] = submit
application_test[['SK_ID_CURR', 'TARGET']].to_csv('submission.csv', index=False, float_format='%.8f' )<set_options>
df_out.ForecastId = df_out.ForecastId.astype('int')
 df_out.head(100 )
%matplotlib inline 
<load_from_csv>
df_out.to_csv('submission.csv', index=False )