Kaggle competition: COVID19 Global Forecasting (Week 2)
doc = [x for x in train.columns if 'FLAG_DOC' in x]
connection = ['FLAG_MOBIL', 'FLAG_EMP_PHONE', 'FLAG_WORK_PHONE',
'FLAG_CONT_MOBILE', 'FLAG_PHONE', 'FLAG_EMAIL',]
le = LabelEncoder()
categorical = ['CODE_GENDER','FLAG_OWN_CAR','FLAG_OWN_REALTY','NAME_EDUCATION_TYPE',
'NAME_FAMILY_STATUS',
'FLAG_MOBIL','FLAG_EMP_PHONE','FLAG_CONT_MOBILE','FLAG_EMAIL','FLAG_WORK_PHONE',
'OCCUPATION_TYPE','ORGANIZATION_TYPE_v2',
'NAME_INCOME_TYPE','NAME_HOUSING_TYPE','NAME_TYPE_SUITE',
'NAME_CONTRACT_TYPE']
for i in categorical:
train[i.lower() ] = le.fit_transform(train[i].fillna("NA"))
test[i.lower() ] = le.transform(test[i].fillna("NA"))
for df in [train,test]:
df['ratio_credit_annity'] = df['AMT_CREDIT']/df['AMT_ANNUITY']
df['ratio_credit_goods'] = df['AMT_CREDIT']/df['AMT_GOODS_PRICE']
df['ratio_min_annuity'] = df['AMT_INCOME_TOTAL']/df['min_amt_annuity']
df['ratio_max_annuity'] = df['AMT_INCOME_TOTAL']/df['max_amt_annuity']
df['ratio_mean_annuity'] = df['AMT_INCOME_TOTAL']/df['mean_amt_annuity']
tmp = df[df['NAME_CONTRACT_TYPE'] == "Revolving loans"].index
df['ratio_credit_goods'].iloc[tmp] = np.nan
df['ratio_credit_annity'].replace(20, np.nan, inplace = True)
 df['doc'] = df[doc].mean(axis=1)
 df['count_null_cash_loans'].replace(np.nan, 0, inplace = True)
 df['count_null_revolving_loans'].replace(np.nan, 0, inplace = True)
 df['ratio_cntpay_cur_mean'] = df['ratio_credit_annity']/df['mean_cntpay']
df['ratio_cntpay_cur_min'] = df['ratio_credit_annity']/df['min_cntpay']
df['ratio_cntpay_cur_max'] = df['ratio_credit_annity']/df['max_cntpay']
df['delta_bureau_HC'] = df['max_day_lastdue'] - df['max_endate_bureau']
df['frequency_bureau'] =(df['max_endate_bureau'] - df['min_endate_bureau'])/(df['count_active_bureau_v2'])
 df['frequency_bureau'].replace(0, np.nan)
 df['sum_delta_install_credit_curr'] = df['AMT_CREDIT'] + df['sum_delta_paid_install']
df['strenght_income'] = df['sum_delta_install_credit_curr']/df['AMT_INCOME_TOTAL']
df['sum_notfinish'] = df['sum_notfinish_cash_loans'] + df['sum_notfinish_consumer_loans']
df['ratio_income_notfinish'] = df['sum_notfinish']/df['AMT_CREDIT']
df['connection'] = df[connection].mean(axis=1)
 df['living'] = df[['REG_REGION_NOT_LIVE_REGION',
'REG_REGION_NOT_WORK_REGION', 'LIVE_REGION_NOT_WORK_REGION',
'REG_CITY_NOT_LIVE_CITY', 'REG_CITY_NOT_WORK_CITY',
'LIVE_CITY_NOT_WORK_CITY']].mean(axis=1)
 predictors = ['EXT_SOURCE_1','EXT_SOURCE_2','EXT_SOURCE_3','CNT_CHILDREN',
'AMT_INCOME_TOTAL','AMT_CREDIT','AMT_ANNUITY','AMT_GOODS_PRICE',
'CNT_FAM_MEMBERS', 'DAYS_BIRTH','DAYS_EMPLOYED','DAYS_REGISTRATION', 'DAYS_ID_PUBLISH',
'DAYS_LAST_PHONE_CHANGE',
'OBS_30_CNT_SOCIAL_CIRCLE',
'DEF_30_CNT_SOCIAL_CIRCLE',
'OWN_CAR_AGE','REGION_POPULATION_RELATIVE',
'AMT_REQ_CREDIT_BUREAU_YEAR',
'REGION_RATING_CLIENT','REGION_RATING_CLIENT_W_CITY','TOTALAREA_MODE','APARTMENTS_AVG',
'ratio_credit_annity','ratio_credit_goods','doc','connection',
'min_amt_app','max_amt_app','mean_amt_app',
'min_amt_app_v1','max_amt_app_v1','mean_amt_app_v1',
'min_amt_card','max_amt_card','mean_amt_card',
'min_amt_app_fail','max_amt_app_fail','mean_amt_app_fail',
'min_amt_app_v1_fail','max_amt_app_v1_fail','mean_amt_app_v1_fail',
'min_amt_card_fail','max_amt_card_fail','mean_amt_card_fail',
'min_amt_annuity', 'max_amt_annuity', 'mean_amt_annuity',
'min_cntpay', 'max_cntpay', 'mean_cntpay',
'min_day_decision','max_day_decision',
'min_day_termination','max_day_termination',
'max_day_lastdue',
'min_firstdraw', 'max_firstdraw',
'min_firstdraw_decision', 'max_firstdraw_decision', 'mean_firstdraw_decision',
'min_firstdraw_firstdue', 'max_firstdraw_firstdue', 'mean_firstdraw_firstdue',
'min_firstdraw_lastdue', 'max_firstdraw_lastdue', 'mean_firstdraw_lastdue',
'max_day_decision_fail',
'count_notfinish_revolving_loans','count_notfinish_cash_loans','count_notfinish_consumer_loans',
'min_sooner', 'max_sooner', 'mean_sooner',
'min_seller', 'max_seller', 'mean_seller',
'sum_notfinish',
'min_amt_goods_v1','max_amt_goods_v1','mean_amt_goods_v1',
'1st_recent_app','1st_recent_credit','1st_recent_card',
'1st_recent_app_fail','1st_recent_credit_fail','1st_recent_card_fail',
'1st_recent_ratedown','1st_recent_ratedown_fail',
'1st_recent_cntpay',
'ratio_cntpay_cur_mean',
'count_cash_loans','count_consumer_loans','count_clear_reason',
'count_middle', 'count_low_normal', 'count_high', 'count_low_action',
'ratio_middle', 'ratio_low_normal', 'ratio_high', 'ratio_low_action',
'count_active_bureau','count_closed_bureau',
'count_active_bureau_v2',
'min_active_credit_bureau','max_active_credit_bureau',
'mean_active_credit_bureau',
'min_closed_credit_bureau','max_closed_credit_bureau',
'mean_closed_credit_bureau',
'min_active_credit_bureau_v1','max_active_credit_bureau_v1',
'mean_active_credit_bureau_v1',
'min_active_credit_bureau_v2','max_active_credit_bureau_v2',
'mean_active_credit_bureau_v2',
'min_active_credit_bureau_v3','max_active_credit_bureau_v3',
'mean_active_credit_bureau_v3',
'max_endate_bureau',
'1st_endate_bureau',
'max_endatefact_bureau',
'min_deltaendate_bureau','max_deltaendate_bureau','mean_deltaendate_bureau',
'min_duration_bureau','max_duration_bureau','mean_duration_bureau',
'min_sooner_bureau','max_sooner_bureau','mean_sooner_bureau',
'min_annuity_bureau','max_annuity_bureau','mean_annuity_bureau',
'min_debt_bureau','max_debt_bureau','mean_debt_bureau','sum_debt_bureau',
'min_debt_bureau_v1','max_debt_bureau_v1','mean_debt_bureau_v1',
'min_limit_bureau','max_limit_bureau','mean_limit_bureau',
'min_overdue_bureau','max_overdue_bureau','mean_overdue_bureau',
'min_ratio_debt_credit_bureau','max_ratio_debt_credit_bureau','mean_ratio_debt_credit_bureau',
'min_delta_num_install','max_delta_num_install','mean_delta_num_install',
'min_ratio_num_install','max_ratio_num_install','mean_ratio_num_install',
'min_max_version_install','max_max_version_install','mean_max_version_install',
'min_ratio_paid_install','max_ratio_paid_install','mean_ratio_paid_install',
'sum_delta_paid_install','sum_delta_install_credit_curr',
'min_max_num_install','max_max_num_install','mean_max_num_install',
'count_small_payment','count_late_payment_0','count_late_payment_10','count_late_payment_20','count_late_payment_30',
'min_max_ratio_balance_limit_credit','max_max_ratio_balance_limit_credit','mean_max_ratio_balance_limit_credit',
] +\
[i.lower() for i in categorical]
categorical = [i.lower() for i in categorical]<split>
PATH_WEEK2 = '/kaggle/input/covid19-global-forecasting-week-2'
df_Train = pd.read_csv(f'{PATH_WEEK2}/train.csv')
 df_test = pd.read_csv(f'{PATH_WEEK2}/test.csv' )
NFOLDS = 5
kf = StratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=2018)
 pred_test_full = 0
params = {
'boosting': 'gbdt',
'objective': 'binary',
'metric': 'auc',
'learning_rate': 0.01,
'num_leaves': 40,
'max_depth': 7,
'colsample_bytree': 0.15,
'seed': 101
}
res = []
idx = 0
for dev_index, val_index in kf.split(train, train['TARGET'].values):
dev, valid = train.loc[dev_index,:], train.loc[val_index,:]
dtrain = lgb.Dataset(dev[predictors].values, label=dev['TARGET'].values,
feature_name=predictors,
categorical_feature=categorical 
)
 dvalid = lgb.Dataset(valid[predictors].values, label=valid['TARGET'].values,
feature_name=predictors,
categorical_feature=categorical 
)
 print("Training the model...")
 lgb_model = lgb.train(params,
dtrain,
valid_sets=[dtrain, dvalid],
valid_names=['train','valid'],
num_boost_round= 30000,
early_stopping_rounds=500,
verbose_eval=100,
feval=None)
 oof = pd.DataFrame()
oof['id'] = valid['SK_ID_CURR'].values
oof['target'] = valid['TARGET'].values
oof['preds'] = lgb_model.predict(valid[predictors],num_iteration=lgb_model.best_iteration)
 res.append(oof)
 pred_test_full += lgb_model.predict(test[predictors],num_iteration=lgb_model.best_iteration)
 sub = pd.read_csv(".. /input/sample_submission.csv")
 sub['TARGET'] = pred_test_full/NFOLDS
sub.to_csv("sub_lgb.csv", index=False)
 res = pd.concat(res, ignore_index=True)
 res.to_csv("oof_lgb.csv", index=False)
 print(roc_auc_score(res['target'], res['preds'])) 
<merge>
PATH_POPULATION = '/kaggle/input/population-by-country-2020'
df_Population = pd.read_csv(f'{PATH_POPULATION}/population_by_country_2020.csv' )
tmp = install.groupby(['SK_ID_PREV','NUM_INSTALMENT_NUMBER'])['DAYS_INSTALMENT'].count().reset_index()
tmp = tmp[tmp['DAYS_INSTALMENT'] > 1]
tmp.columns = ['SK_ID_PREV','NUM_INSTALMENT_NUMBER','count_dup']
install = install.merge(tmp, on = ['SK_ID_PREV','NUM_INSTALMENT_NUMBER'], how='left')
 tmp = install[install['count_dup'] > 1]<drop_column>
df_Train.iloc[np.r_[0:5, -6:-1], :]
install.drop(['count_dup_x','count_dup_y'], axis=1, inplace = True )<sort_values>
df_test.iloc[np.r_[0:5, -6:-1], :]
tmp.sort_values(by=['SK_ID_PREV','NUM_INSTALMENT_NUMBER','DAYS_ENTRY_PAYMENT'] )<count_values>
df_Population.iloc[np.r_[0:5, -6:-1], :]
tmp['SK_ID_CURR'].value_counts()<import_modules>
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 )
import matplotlib.pyplot as plt
import lightgbm as lgb
import gc
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import confusion_matrix,precision_recall_curve,auc,roc_auc_score,roc_curve,recall_score,classification_report
from sklearn.preprocessing import LabelEncoder<load_from_csv>
import plotly.graph_objects as go
import plotly.express as px
import matplotlib.pyplot as plt
app_train = pd.read_csv('.. /input/application_train.csv')
 print('Training data shape: ', app_train.shape)
 app_train.head()<load_from_csv>
df_Population.rename(columns={'Country(or dependency)':'Country'}, inplace=True )
app_test = pd.read_csv('.. /input/application_test.csv')
 print('Testing data shape: ', app_test.shape)
 app_test.head()<count_values>
renameCountryNames = {
"Congo(Brazzaville)": "Congo",
"Congo(Kinshasa)": "Congo",
"Cote d'Ivoire": "Côte d'Ivoire",
"Czechia": "Czech Republic(Czechia)",
"Korea, South": "South Korea",
"Saint Kitts and Nevis": "Saint Kitts & Nevis",
"Saint Vincent and the Grenadines": "St.Vincent & Grenadines",
"Taiwan*": "Taiwan",
"US": "United States"
}
app_train['TARGET'].value_counts()
print('The proportion of label 1 is %.2f' %(sum(app_train['TARGET']==1)/app_train.shape[0]*100), '%' )<count_missing_values>
df_Train.replace({'Country': renameCountryNames}, inplace=True)
 df_test.replace({'Country': renameCountryNames}, inplace=True )
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = df.isnull().sum() * 100 / df.shape[0]
mis_val_table = pd.concat([mis_val, mis_val_percent], axis = 1)
 mis_val_table_rename_columns = mis_val_table.rename(columns = {0: 'Missing Values', 1: 'Percentage'})
 mis_val_table_rename_columns = mis_val_table_rename_columns[
mis_val_table_rename_columns.iloc[:, 1]!=0].sort_values('Percentage', ascending=False ).round(1)
 print('The total dataframe has ' + str(df.shape[1])+ ' columns')
 print('There are ' + str(mis_val_table_rename_columns.shape[0])+ ' columns')
 return mis_val_table_rename_columns<count_missing_values>
df_Population.loc[df_Population['Med.Age']=='N.A.', 'Med.Age'] = df_Population.loc[df_Population['Med.Age']!='N.A.', 'Med.Age'].mode() [0]
df_Population.loc[df_Population['Urban Pop %']=='N.A.', 'Urban Pop %'] = df_Population.loc[df_Population['Urban Pop %']!='N.A.', 'Urban Pop %'].mode() [0]
df_Population.loc[df_Population['Fert.Rate']=='N.A.', 'Fert.Rate'] = df_Population.loc[df_Population['Fert.Rate']!='N.A.', 'Fert.Rate'].mode() [0]
df_Population.loc[:, 'Migrants(net)'] = df_Population.loc[:, 'Migrants(net)'].fillna(0)
 df_Population['Yearly Change'] = df_Population['Yearly Change'].str.rstrip('%')
 df_Population['World Share'] = df_Population['World Share'].str.rstrip('%')
 df_Population['Urban Pop %'] = df_Population['Urban Pop %'].str.rstrip('%')
 df_Population = df_Population.astype({"Net Change": int,"Density(P/Km²)": int,"Population(2020)": int,"Land Area(Km²)": int,"Yearly Change": float,"Urban Pop %": int,"Fert.Rate": float,"Med.Age": int,"World Share": float, "Migrants(net)": float,})
 df_Population = df_Population.append(pd.Series(['Diamond Princess', 3500, 0, 0, 0, 0, 0.0, 1, 30, 0, 0.0], index=df_Population.columns), ignore_index=True )
missing_values = missing_values_table(app_train)
 missing_values.head(20 )<count_values>
df_Population[df_Population['Population(2020)'] <= 5000]
app_train.dtypes.value_counts()<count_unique_values>
df_Train = df_Train.merge(df_Population, how='left', left_on='Country', right_on='Country')
 df_test = df_test.merge(df_Population, how='left', left_on='Country', right_on='Country' )
app_train.select_dtypes('object' ).apply(pd.Series.nunique, axis=0 )<count_values>
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 )
app_test.dtypes.value_counts()<categorify>
MIN_TEST_DATE = df_test.Date.min()
app_train = pd.get_dummies(app_train)
 app_test = pd.get_dummies(app_test)
 print('Training Features shape: ', app_train.shape)
 print('Testing Features shape: ', app_test.shape )<train_model>
df_train = df_Train.loc[df_Train.Date < MIN_TEST_DATE, :]
train_labels = app_train['TARGET']
app_train, app_test = app_train.align(app_test, join = 'inner', axis = 1)
 app_train['TARGET'] = train_labels
print('Training Features shape: ', app_train.shape)
 print('Testing Features shape: ', app_test.shape )<train_model>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
app_train['DAYS_EMPLOYED_ANOM'] = app_train['DAYS_EMPLOYED'] == 365243
app_train['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True)
 app_test['DAYS_EMPLOYED_ANOM'] = app_test['DAYS_EMPLOYED'] == 365243
app_test['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True)
 print('Training Features shape: ', app_train.shape)
 print('Testing Features shape: ', app_test.shape )<feature_engineering>
X_Train = df_train.copy()
X_Train['State'].fillna(EMPTY_VAL, inplace=True)
 X_Train['State'] = X_Train.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Train['year'] = X_Train['Date'].dt.year
X_Train['month'] = X_Train['Date'].dt.month
X_Train['week'] = X_Train['Date'].dt.week
X_Train['day'] = X_Train['Date'].dt.day
X_Train['dayofweek'] = X_Train['Date'].dt.dayofweek
X_Train.loc[:, 'Date'] = X_Train.Date.dt.strftime("%m%d")
 X_Train["Date"] = X_Train["Date"].astype(int)
 X_Train.head()
app_train_domain = app_train.copy()
app_test_domain = app_test.copy()
app_train_domain['CREDIT_INCOME_PERCENT'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_INCOME_TOTAL']
app_train_domain['ANNUITY_INCOME_PERCENT'] = app_train_domain['AMT_ANNUITY'] / app_train_domain['AMT_INCOME_TOTAL']
app_train_domain['CREDIT_TERM'] = app_train_domain['AMT_ANNUITY'] / app_train_domain['AMT_CREDIT']
app_train_domain['DAYS_EMPLOYED_PERCENT'] = app_train_domain['DAYS_EMPLOYED'] / app_train_domain['DAYS_BIRTH']
app_test_domain['CREDIT_INCOME_PERCENT'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_INCOME_TOTAL']
app_test_domain['ANNUITY_INCOME_PERCENT'] = app_test_domain['AMT_ANNUITY'] / app_test_domain['AMT_INCOME_TOTAL']
app_test_domain['CREDIT_TERM'] = app_test_domain['AMT_ANNUITY'] / app_test_domain['AMT_CREDIT']
app_test_domain['DAYS_EMPLOYED_PERCENT'] = app_test_domain['DAYS_EMPLOYED'] / app_test_domain['DAYS_BIRTH']
print('Domain Training Features shape: ', app_train_domain.shape)
 print('Domain Testing Features shape: ', app_test_domain.shape )<load_from_csv>
X_Test = df_test.copy()
X_Test['State'].fillna(EMPTY_VAL, inplace=True)
 X_Test['State'] = X_Test.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Test['year'] = X_Test['Date'].dt.year
X_Test['month'] = X_Test['Date'].dt.month
X_Test['week'] = X_Test['Date'].dt.week
X_Test['day'] = X_Test['Date'].dt.day
X_Test['dayofweek'] = X_Test['Date'].dt.dayofweek
X_Test.loc[:, 'Date'] = X_Test.Date.dt.strftime("%m%d")
 X_Test["Date"] = X_Test["Date"].astype(int)
 X_Test.head()
bureau = pd.read_csv('.. /input/bureau.csv')
 bureau.head()<groupby>
le = preprocessing.LabelEncoder()
previous_loan_counts = bureau.groupby('SK_ID_CURR', as_index=False)['SK_ID_BUREAU'].count().rename(columns = {'SK_ID_BUREAU': 'previous_loan_counts'})
 previous_loan_counts.head() 
<create_dataframe>
X_Train.Country = le.fit_transform(X_Train.Country)
 X_Train['State'] = le.fit_transform(X_Train['State'])
 X_Train.head()
def agg_numeric(df, group_var, df_name):

 for col in df:
if col != group_var and 'SK_ID' in col:
df = df.drop(columns = col)
 group_ids = df[group_var]
numeric_df = df.select_dtypes('number')
 numeric_df[group_var] = group_ids
agg = numeric_df.groupby(group_var ).agg(['count', 'mean', 'max', 'min', 'sum'] ).reset_index()
columns = [group_var]
for var in agg.columns.levels[0]:
if var != group_var:
for stat in agg.columns.levels[1][:-1]:
columns.append('%s_%s_%s' %(df_name, var, stat))
agg.columns = columns
return agg
bureau_agg = agg_numeric(bureau.drop(columns = ['SK_ID_BUREAU']), group_var = 'SK_ID_CURR', df_name = 'bureau')
 bureau_agg.head()<create_dataframe>
X_Test.Country = le.fit_transform(X_Test.Country)
 X_Test['State'] = le.fit_transform(X_Test['State'])
 X_Test.head()
def count_categorical(df, group_var, df_name):

 categorical = pd.get_dummies(df.select_dtypes('object'))
categorical[group_var] = df[group_var]
categorical = categorical.groupby(group_var ).agg(['sum', 'mean'])
 column_names = []
for var in categorical.columns.levels[0]:
for stat in ['count', 'count_norm']:
column_names.append('%s_%s_%s' %(df_name, var, stat))
categorical.columns = column_names
return categorical
bureau_counts = count_categorical(bureau, group_var = 'SK_ID_CURR', df_name = 'bureau')
 bureau_counts.head()<merge>
df_train.loc[df_train.Country == 'Afghanistan', :]
train = app_train_domain.merge(previous_loan_counts, on = 'SK_ID_CURR', how = 'left')
 train['previous_loan_counts'] = train['previous_loan_counts'].fillna(0)
 test = app_test_domain.merge(previous_loan_counts, on = 'SK_ID_CURR', how = 'left')
 test['previous_loan_counts'] = test['previous_loan_counts'].fillna(0 )<merge>
X_Train.iloc[3990:4020]
train = train.merge(bureau_agg, on = 'SK_ID_CURR', how = 'left')
 test = test.merge(bureau_agg, on = 'SK_ID_CURR', how = 'left' )<merge>
filterwarnings('ignore' )
train = train.merge(bureau_counts, on = 'SK_ID_CURR', how = 'left')
 test = test.merge(bureau_counts, on = 'SK_ID_CURR', how = 'left' )<train_model>
print('Before align train.shape: ', train.shape)
 print('Before align test.shape: ', test.shape)
 train_labels = train['TARGET']
train, test = train.align(test, join = 'inner', axis = 1)
 train['TARGET'] = train_labels
print('After align train.shape: ', train.shape)
 print('After align test.shape: ', test.shape )<load_from_csv>
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn import metrics
bureau_balance = pd.read_csv('.. /input/bureau_balance.csv')
 bureau_balance.head()<categorify>
def applyMLA(model, X_train, X_test, y_train, y_test):
start_time = time.time()
model.fit(X_train, y_train)
 end_time = time.time()
time2 = end_time-start_time
predictions = model.predict(X_test)
 RMSE_test = np.sqrt(metrics.mean_squared_error(y_test, predictions))
return [RMSE_test, time2]
bureau_balance_agg = agg_numeric(bureau_balance, group_var = 'SK_ID_BUREAU', df_name = 'bureau_balance')
 bureau_balance_agg.head()<groupby>
le = preprocessing.LabelEncoder()
bureau_balance_counts = count_categorical(bureau_balance, group_var = 'SK_ID_BUREAU', df_name = 'bureau_balance')
 bureau_balance_counts.head()<merge>
countries = X_Train.Country.unique().tolist()
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in countries:
states = X_Train.loc[X_Train.Country == country, :].State.unique().tolist()
for state in states:
X_Train_CS = X_Train.loc[(X_Train.Country == country)&(X_Train.State == state), :]
y1_Train_CS = X_Train_CS.loc[:, 'ConfirmedCases']
y2_Train_CS = X_Train_CS.loc[:, 'Fatalities']
X_Train_CS.drop(columns=['Id', 'ConfirmedCases', 'Fatalities'], axis=1, inplace=True)
 X_Train_CS_PCA = X_Train_CS
X_Test_CS = X_Test.loc[(X_Test.Country == country)&(X_Test.State == state), :]
X_Test_CS_Id = X_Test_CS.loc[:, 'ForecastId']
X_Test_CS.drop(columns=['ForecastId'], axis=1, inplace=True)
 X_Test_CS_PCA = X_Test_CS
model1 = XGBRegressor(n_estimators=1250)
 model1.fit(X_Train_CS_PCA, y1_Train_CS)
 y1_pred = model1.predict(X_Test_CS_PCA)
 model2 = XGBRegressor(n_estimators=1000)
 model2.fit(X_Train_CS_PCA, y2_Train_CS)
 y2_pred = model2.predict(X_Test_CS_PCA)
 df = pd.DataFrame({'ForecastId': X_Test_CS_Id, 'ConfirmedCases': y1_pred, 'Fatalities': y2_pred})
 df_out = pd.concat([df_out, df], axis=0)
bureau_by_loan = bureau_balance_agg.merge(bureau_balance_counts, right_index = True, left_on = 'SK_ID_BUREAU', how = 'outer' )<merge>
df_out.ForecastId = df_out.ForecastId.astype('int' )
bureau_by_loan = bureau_by_loan.merge(bureau[['SK_ID_BUREAU', 'SK_ID_CURR']], on = 'SK_ID_BUREAU', how = 'left' )<drop_column>
df_out.to_csv('submission.csv', index=False )