Kaggle competition: COVID19 Global Forecasting (Week 2)
roc_auc_score(train.TARGET,GPII(train))<compute_test_metric>
%matplotlib inline
roc_auc_score(train.TARGET,GPIII(train))<compute_test_metric>
train=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv')
 test=pd.read_csv('/kaggle/input//covid19-global-forecasting-week-2/test.csv' )
roc_auc_score(train.TARGET,GP(train))<save_to_csv>
le = preprocessing.LabelEncoder()
train["Country_Region"]=le.fit_transform(train["Country_Region"])
 test["Country_Region"]=le.transform(test["Country_Region"])
 train.drop(["Id"],inplace=True,axis=1)
 test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int)
 train["Date"] = train["Date"].apply(lambda x: x.replace("-",""))
train["Date"] = train["Date"].astype(int)
 train["Province_State"].fillna("a",inplace=True)
 test["Province_State"].fillna("a",inplace=True)
 train["Province_State"]=le.fit_transform(train["Province_State"])
 test["Province_State"]=le.transform(test["Province_State"])
Submission = pd.DataFrame({ 'SK_ID_CURR': ID,'TARGET': GP(test ).values })
 Submission.to_csv("sample_submission.csv", index=False )<load_from_csv>
model_conf = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.3, max_delta_step=0, max_depth=19,
min_child_weight=1, monotone_constraints=None,
n_estimators=1000, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None)
 model_fat = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.3, max_delta_step=0, max_depth=18,
min_child_weight=1, monotone_constraints=None,
n_estimators=1000, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None )
sns.set(style="whitegrid", color_codes=True)
 np.random.seed(sum(map(ord, "categorical")))
 print(os.listdir(".. /input"))
application_train = pd.read_csv(".. /input/application_train.csv")
 application_test = pd.read_csv(".. /input/application_test.csv")
 bureau = pd.read_csv(".. /input/bureau.csv")
 bureau_balance = pd.read_csv(".. /input/bureau_balance.csv")
 credit_card_balance = pd.read_csv(".. /input/credit_card_balance.csv")
 installments_payments = pd.read_csv(".. /input/installments_payments.csv")
 previous_application = pd.read_csv(".. /input/previous_application.csv")
 POS_CASH_balance = pd.read_csv(".. /input/POS_CASH_balance.csv" )<set_options>
def train_function() :
train_tmp = pd.DataFrame(train)
 y_conf = train_tmp["ConfirmedCases"]
y_fat = train_tmp["Fatalities"]
X_fat = train_tmp.drop(["Fatalities"],axis=1)
 X_conf = train_tmp.drop(["ConfirmedCases","Fatalities"],axis=1)
 model_fat.fit(X_fat,y_fat)
 model_conf.fit(X_conf,y_conf )
sns.set(rc={'figure.figsize':(14.7,8.27)} )<categorify>
def test_fat(conf_list):
test_tmp = test.drop(["ForecastId"],axis=1)
 test_tmp["ConfirmedCases"] = np.array(conf_list)
 pr = model_fat.predict(test_tmp)
 tmp_pr = []
for i in pr:
if i < 0:
tmp_pr.append(0)
 continue
tmp_pr.append(int(i))
pr_fat = tmp_pr
for i in range(1,len(tmp_pr)) :
if tmp_pr[i] < tmp_pr[i-1]:
tmp_pr[i] = tmp_pr[i-1]
return tmp_pr
bureau_cat = [f_ for f_ in bureau.columns if bureau[f_].dtype == 'object']
dummy_bureau = pd.get_dummies(bureau, columns=bureau_cat)
 dummy_bureau.head()<categorify>
def test_conf() :
test_tmp = test.drop(["ForecastId"],axis=1)
 pr = model_conf.predict(test_tmp)
 tmp_pr = []
for i in pr:
if i < 0:
tmp_pr.append(0)
 continue
tmp_pr.append(int(i))
pr_conf = tmp_pr
for i in range(1,len(tmp_pr)) :
if tmp_pr[i] < tmp_pr[i-1]:
tmp_pr[i] = tmp_pr[i-1]
return tmp_pr
bureau_balance_cat = [f_ for f_ in bureau_balance.columns if bureau_balance[f_].dtype == 'object']
dummy_bureau_balance = pd.get_dummies(bureau_balance, columns=bureau_balance_cat)
 dummy_bureau_balance.head()<groupby>
conf_list = test_conf()
fat_list = test_fat(conf_list )
avg_bureau_balance = dummy_bureau_balance.groupby('SK_ID_BUREAU' ).mean()
avg_bureau_balance.head()<merge>
submission = pd.DataFrame()
submission["ForecastId"] = np.array(test["ForecastId"])
 submission["ConfirmedCases"] = np.array(conf_list)
 submission["Fatalities"] = np.array(fat_list )
bureau_all = dummy_bureau.merge(right=avg_bureau_balance.reset_index() , how='left', on='SK_ID_BUREAU', suffixes=('', '_balance_'))
bureau_all.head()<categorify>
model_conf2 = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.3, max_delta_step=0, max_depth=19,
min_child_weight=1, monotone_constraints=None,
n_estimators=1000, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None)
 model_fat2 = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.3, max_delta_step=0, max_depth=18,
min_child_weight=1, monotone_constraints=None,
n_estimators=1000, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None )
bureau_per_curr = bureau_all[['SK_ID_CURR', 'SK_ID_BUREAU']].groupby('SK_ID_CURR' ).count()
bureau_per_curr.head(10)
 bureau_all['SK_ID_BUREAU'] = bureau_all['SK_ID_CURR'].map(bureau_per_curr['SK_ID_BUREAU'] )<groupby>
def train_function2() :
train_tmp = pd.DataFrame(train)
 y_conf = train_tmp["ConfirmedCases"]
y_fat = train_tmp["Fatalities"]
X_fat = train_tmp.drop(["Fatalities"],axis=1)
 X_conf = train_tmp.drop(["ConfirmedCases"],axis=1)
 model_fat2.fit(X_fat,y_fat)
 model_conf2.fit(X_conf,y_conf )
avg_bureau = bureau_all.groupby('SK_ID_CURR' ).mean()
avg_bureau.head(10 )<drop_column>
def test_fat2(conf_list):
test_tmp = test.drop(["ForecastId"],axis=1)
 test_tmp["ConfirmedCases"] = np.array(conf_list)
 pr = model_fat2.predict(test_tmp)
 tmp_pr = []
for i in pr:
if i < 0:
tmp_pr.append(0)
 continue
tmp_pr.append(int(i))
pr_fat = tmp_pr
for i in range(1,len(tmp_pr)) :
if tmp_pr[i] < tmp_pr[i-1]:
tmp_pr[i] = tmp_pr[i-1]
return tmp_pr
del avg_bureau_balance, dummy_bureau, dummy_bureau_balance, bureau_all<categorify>
def test_conf2(fat_list):
test_tmp = test.drop(["ForecastId"],axis=1)
 test_tmp["Fatalities"] = np.array(fat_list)
 pr = model_conf2.predict(test_tmp)
 tmp_pr = []
for i in pr:
if i < 0:
tmp_pr.append(0)
 continue
tmp_pr.append(int(i))
pr_conf = tmp_pr
for i in range(1,len(tmp_pr)) :
if tmp_pr[i] < tmp_pr[i-1]:
tmp_pr[i] = tmp_pr[i-1]
return tmp_pr
credit_card_balance_cat = [f_ for f_ in credit_card_balance.columns if credit_card_balance[f_].dtype == 'object']
dummy_credit_card_balance = pd.get_dummies(credit_card_balance, columns=credit_card_balance_cat)
 dummy_credit_card_balance.head()<groupby>
conf_list2 = test_conf2(fat_list)
 fat_list2 = test_fat(conf_list )
credit_card_per_curr = dummy_credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
dummy_credit_card_balance['SK_ID_PREV'] = dummy_credit_card_balance['SK_ID_CURR'].map(credit_card_per_curr['SK_ID_PREV'])
 avg_credit_card = dummy_credit_card_balance.groupby('SK_ID_CURR' ).mean()
avg_credit_card.head()<drop_column>
submission2 = pd.DataFrame()
submission2["ForecastId"] = np.array(test["ForecastId"])
 submission2["ConfirmedCases"] = np.array(conf_list2)
 submission2["Fatalities"] = np.array(fat_list2 )
del dummy_credit_card_balance<categorify>
submission2.to_csv("submission.csv",index = False )