Kaggle competition: COVID19 Global Forecasting (Week 2)
metadata[(metadata.type == 'categorical')&(metadata.preserve)].index<create_dataframe>
import plotly.graph_objects as go
import matplotlib.pyplot as plt
from tqdm import tqdm
import time
from datetime import datetime
from pathlib import Path
from sklearn import preprocessing
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from keras.callbacks import EarlyStopping
from sklearn.preprocessing import StandardScaler, MinMaxScaler
pd.DataFrame({'count' : metadata.groupby(['category'])['category'].size() } ).reset_index()<create_dataframe>
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")
 train.tail()
pd.DataFrame({'count' : metadata.groupby(['use', 'type'])['use'].size() } ).reset_index()<count_values>
mask = train['Date'].max()
world_cum_confirmed = sum(train[train['Date'] == mask].ConfirmedCases)
 world_cum_fatal = sum(train[train['Date'] == mask].Fatalities )
vars_with_missing = []
for feature in trainset.columns:
missings = trainset[trainset[feature] == -1][feature].count()
if missings > 0:
vars_with_missing.append(feature)
 missings_perc = missings/trainset.shape[0]
print('Variable {} has {} records({:.2%})with missing values'.format(feature, missings, missings_perc))
print('In total, there are {} variables with missing values'.format(len(vars_with_missing)) )<drop_column>
print('Number of Countires are: ', len(train['Country_Region'].unique()))
print('Training dataset ends at: ', mask)
 print('Number of cumulative confirmed cases worldwide are: ', world_cum_confirmed)
 print('Number of cumulative fatal cases worldwide are: ', world_cum_fatal )
col_to_drop = trainset.columns[trainset.columns.str.startswith('ps_calc_')]
trainset = trainset.drop(col_to_drop, axis=1)
 testset = testset.drop(col_to_drop, axis=1 )<drop_column>
cum_per_country = train[train['Date'] == mask].groupby(['Date','Country_Region'] ).sum().sort_values(['ConfirmedCases'], ascending=False)
 cum_per_country[:10]
vars_to_drop = ['ps_car_03_cat', 'ps_car_05_cat']
trainset.drop(vars_to_drop, inplace=True, axis=1)
 testset.drop(vars_to_drop, inplace=True, axis=1)
 metadata.loc[(vars_to_drop),'keep'] = False 
<categorify>
date = train['Date'].unique()
cc_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().ConfirmedCases
ft_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().Fatalities
cc_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().ConfirmedCases
ft_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().Fatalities
cc_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().ConfirmedCases
ft_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().Fatalities
cc_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().ConfirmedCases
ft_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().Fatalities
cc_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().ConfirmedCases
ft_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().Fatalities
fig = go.Figure()
fig.add_trace(go.Scatter(x=date, y=cc_us, name='US'))
fig.add_trace(go.Scatter(x=date, y=cc_ity, name='Italy'))
fig.add_trace(go.Scatter(x=date, y=cc_spn, name='Spain'))
fig.add_trace(go.Scatter(x=date, y=cc_gmn, name='Germany'))
fig.add_trace(go.Scatter(x=date, y=cc_frc, name='France'))
fig.update_layout(title="Plot of Cumulative Cases for Top 5 countires(except China)",
xaxis_title="Date",
yaxis_title="Cases")
 fig.update_xaxes(nticks=30)
 fig.show()
def add_noise(series, noise_level):
return series *(1 + noise_level * np.random.randn(len(series)))
 def target_encode(trn_series=None,
tst_series=None,
target=None,
min_samples_leaf=1,
smoothing=1,
noise_level=0):

 assert len(trn_series)== len(target)
 assert trn_series.name == tst_series.name
temp = pd.concat([trn_series, target], axis=1)
 averages = temp.groupby(by=trn_series.name)[target.name].agg(["mean", "count"])
 smoothing = 1 /(1 + np.exp(-(averages["count"] - min_samples_leaf)/ smoothing))
prior = target.mean()
averages[target.name] = prior *(1 - smoothing)+ averages["mean"] * smoothing
averages.drop(["mean", "count"], axis=1, inplace=True)
 ft_trn_series = pd.merge(
 trn_series.to_frame(trn_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=trn_series.name,
how='left')['average'].rename(trn_series.name + '_mean' ).fillna(prior)
 ft_trn_series.index = trn_series.index
ft_tst_series = pd.merge(
 tst_series.to_frame(tst_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=tst_series.name,
how='left')['average'].rename(trn_series.name + '_mean' ).fillna(prior)
 ft_tst_series.index = tst_series.index
return add_noise(ft_trn_series, noise_level), add_noise(ft_tst_series, noise_level )<categorify>
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train['Country_Region'] = train['Country_Region'].astype(str)
 test['Country_Region'] = test['Country_Region'].astype(str)
train_encoded, test_encoded = target_encode(trainset["ps_car_11_cat"],
testset["ps_car_11_cat"],
target=trainset.target,
min_samples_leaf=100,
smoothing=10,
noise_level=0.01)
 trainset['ps_car_11_cat_te'] = train_encoded
trainset.drop('ps_car_11_cat', axis=1, inplace=True)
 metadata.loc['ps_car_11_cat','keep'] = False
testset['ps_car_11_cat_te'] = test_encoded
testset.drop('ps_car_11_cat', axis=1, inplace=True )<define_variables>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
train['Province_State'].fillna(EMPTY_VAL, inplace=True)
 train['Province_State'] = train.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1 )
desired_apriori=0.10
idx_0 = trainset[trainset.target == 0].index
idx_1 = trainset[trainset.target == 1].index
nb_0 = len(trainset.loc[idx_0])
 nb_1 = len(trainset.loc[idx_1])
 undersampling_rate =(( 1-desired_apriori)*nb_1)/(nb_0*desired_apriori)
 undersampled_nb_0 = int(undersampling_rate*nb_0)
 print('Rate to undersample records with target=0: {}'.format(undersampling_rate))
print('Number of records with target=0 after undersampling: {}'.format(undersampled_nb_0))
undersampled_idx = shuffle(idx_0, random_state=314, n_samples=undersampled_nb_0)
 idx_list = list(undersampled_idx)+ list(idx_1)
 trainset = trainset.loc[idx_list].reset_index(drop=True )<categorify>
le = preprocessing.LabelEncoder()
train['country_encoder'] = le.fit_transform(train['Country_Region'])
 train['date_int'] = train['Date'].apply(lambda x: datetime.strftime(x, '%m%d')).astype(int)
 test['country_encoder'] = le.transform(test['Country_Region'])
 test['date_int'] = test['Date'].apply(lambda x: datetime.strftime(x, '%m%d')).astype(int )
trainset = trainset.replace(-1, np.nan)
 testset = testset.replace(-1, np.nan )<categorify>
le = preprocessing.LabelEncoder()
train['province_encoder'] = le.fit_transform(train['Province_State'])
 test['province_encoder'] = le.transform(test['Province_State'] )
cat_features = [a for a in trainset.columns if a.endswith('cat')]
for column in cat_features:
temp = pd.get_dummies(pd.Series(trainset[column]))
trainset = pd.concat([trainset,temp],axis=1)
 trainset = trainset.drop([column],axis=1)
 for column in cat_features:
temp = pd.get_dummies(pd.Series(testset[column]))
testset = pd.concat([testset,temp],axis=1)
 testset = testset.drop([column],axis=1 )<drop_column>
start_time = time.time()
country = train['Country_Region'].drop_duplicates()
train_df = train.copy()
train_df.rename(columns={'Date': 'date', 'ConfirmedCases': 'cc_cases', 'Fatalities': 'ft_cases', 'Country_Region': 'country', 'Province_State': 'province'}, inplace=True)
 lags = np.arange(1,8,1)
 with tqdm(total = len(list(train_df['date'].unique())))as pbar:
for d in train_df['date'].drop_duplicates() :
for i in country:
province = train_df[train_df['country'] == i]['province'].drop_duplicates()
for j in province:
mask =(train_df['date'] == d)&(train_df['country'] == i)&(train_df['province'] == j)
 for lag in lags:
mask_org =(train_df['date'] ==(d - pd.Timedelta(days=lag)))&(train_df['country'] == i)&(train_df['province'] == j)
 try:
train_df.loc[mask, 'cc_cases_' + str(lag)] = train_df.loc[mask_org, 'cc_cases'].values
except:
train_df.loc[mask, 'cc_cases_' + str(lag)] = 0
try:
train_df.loc[mask, 'ft_cases_' + str(lag)] = train_df.loc[mask_org, 'ft_cases'].values
except:
train_df.loc[mask, 'ft_cases_' + str(lag)] = 0
pbar.update(1)
 print('Time spent for building features is {} minutes'.format(round(( time.time() -start_time)/60,1)) )
id_test = testset['id'].values
target_train = trainset['target'].values
trainset = trainset.drop(['target','id'], axis = 1)
 testset = testset.drop(['id'], axis = 1 )<train_model>
train_df.to_csv(Path('/kaggle/working', 'train_df.csv'))
train_df = pd.read_csv(Path('/kaggle/working/', 'train_df.csv'), index_col = 0, parse_dates = ['date'])
 train_df[train_df['country'] == 'Italy'].tail()
print("Train dataset(rows, cols):",trainset.values.shape, "
Test dataset(rows, cols):",testset.values.shape )<predict_on_test>
def split_train_val(df, val_ratio):
val_len = int(len(df)* val_ratio)
 train_set = df[:-val_len]
val_set = df[-val_len:]
return train_set, val_set
class Ensemble(object):
def __init__(self, n_splits, stacker, base_models):
self.n_splits = n_splits
self.stacker = stacker
self.base_models = base_models
def fit_predict(self, X, y, T):
X = np.array(X)
 y = np.array(y)
 T = np.array(T)
 folds = list(StratifiedKFold(n_splits=self.n_splits, shuffle=True, random_state=314 ).split(X, y))
S_train = np.zeros(( X.shape[0], len(self.base_models)))
 S_test = np.zeros(( T.shape[0], len(self.base_models)))
 for i, clf in enumerate(self.base_models):
S_test_i = np.zeros(( T.shape[0], self.n_splits))
for j,(train_idx, test_idx)in enumerate(folds):
X_train = X[train_idx]
y_train = y[train_idx]
X_holdout = X[test_idx]
print("Base model %d: fit %s model | fold %d" %(i+1, str(clf ).split('(')[0], j+1))
clf.fit(X_train, y_train)
 cross_score = cross_val_score(clf, X_train, y_train, cv=3, scoring='roc_auc')
 print("cross_score [roc-auc]: %.5f [gini]: %.5f" %(cross_score.mean() , 2*cross_score.mean() -1))
y_pred = clf.predict_proba(X_holdout)[:,1]
S_train[test_idx, i] = y_pred
S_test_i[:, j] = clf.predict_proba(T)[:,1]
S_test[:, i] = S_test_i.mean(axis=1)
 results = cross_val_score(self.stacker, S_train, y, cv=3, scoring='roc_auc')
 print("Stacker score [gini]: %.5f" %(2 * results.mean() - 1))
self.stacker.fit(S_train, y)
 res = self.stacker.predict_proba(S_test)[:,1]
return res<init_hyperparams>
test_fixed_cols = ['ForecastId', 'Province_State', 'Country_Region', 'Date']
fixed_cols = ['Id', 'province', 'country', 'date']
output_cols = ['cc_cases', 'ft_cases']
input_cols = list(set(train_df.columns.to_list())- set(fixed_cols)- set(output_cols))
print('output columns are ', output_cols)
 print('input columns are ', input_cols)
 X = train_df[input_cols]
y = train_df[output_cols]
lgb_params1 = {}
lgb_params1['learning_rate'] = 0.02
lgb_params1['n_estimators'] = 650
lgb_params1['max_bin'] = 10
lgb_params1['subsample'] = 0.8
lgb_params1['subsample_freq'] = 10
lgb_params1['colsample_bytree'] = 0.8
lgb_params1['min_child_samples'] = 500
lgb_params1['seed'] = 314
lgb_params1['num_threads'] = 4
lgb_params2 = {}
lgb_params2['n_estimators'] = 1090
lgb_params2['learning_rate'] = 0.02
lgb_params2['colsample_bytree'] = 0.3
lgb_params2['subsample'] = 0.7
lgb_params2['subsample_freq'] = 2
lgb_params2['num_leaves'] = 16
lgb_params2['seed'] = 314
lgb_params2['num_threads'] = 4
lgb_params3 = {}
lgb_params3['n_estimators'] = 1100
lgb_params3['max_depth'] = 4
lgb_params3['learning_rate'] = 0.02
lgb_params3['seed'] = 314
lgb_params3['num_threads'] = 4
xgb_params = {}
xgb_params['objective'] = 'binary:logistic'
xgb_params['learning_rate'] = 0.04
xgb_params['n_estimators'] = 490
xgb_params['max_depth'] = 4
xgb_params['subsample'] = 0.9
xgb_params['colsample_bytree'] = 0.9
xgb_params['min_child_weight'] = 10
xgb_params['num_threads'] = 4<choose_model_class>
cc_input = ['cc_cases_1', 'cc_cases_2', 'cc_cases_3', 'cc_cases_4', 'cc_cases_5', 'cc_cases_6', 'cc_cases_7', 'country_encoder', 'province_encoder', 'date_int']
ft_input = ['ft_cases_1', 'ft_cases_2', 'ft_cases_3', 'ft_cases_4', 'ft_cases_5', 'ft_cases_6', 'ft_cases_7', 'country_encoder', 'province_encoder', 'date_int']
cc_output = ['cc_cases']
ft_output = ['ft_cases']
X_cc = X[cc_input]
X_ft = X[ft_input]
y_cc = y[cc_output]
y_ft = y[ft_output]
train_X_cc, val_X_cc = split_train_val(df = X_cc, val_ratio = 0.1)
 train_y_cc, val_y_cc = split_train_val(df = y_cc, val_ratio = 0.1)
 train_X_ft, val_X_ft = split_train_val(df = X_ft, val_ratio = 0.1)
 train_y_ft, val_y_ft = split_train_val(df = y_ft, val_ratio = 0.1 )
lgb_model1 = LGBMClassifier(**lgb_params1)
 lgb_model2 = LGBMClassifier(**lgb_params2)
 lgb_model3 = LGBMClassifier(**lgb_params3)
 xgb_model = XGBClassifier(**xgb_params)
 log_model = LogisticRegression()<choose_model_class>
idx = np.random.RandomState(seed=42 ).permutation(train_X_cc.index)
 train_X_cc = train_X_cc.reindex(idx)
 train_y_cc = train_y_cc.reindex(idx)
 train_X_ft = train_X_ft.reindex(idx)
 train_y_ft = train_y_ft.reindex(idx)
stack = Ensemble(n_splits=3,
stacker = log_model,
base_models =(lgb_model1, lgb_model2, lgb_model3, xgb_model))<predict_on_test>
y_prediction = stack.fit_predict(trainset, target_train, testset )<save_to_csv>
submission = pd.DataFrame()
submission['id'] = id_test
submission['target'] = y_prediction
submission.to_csv('stacked.csv', index=False )<import_modules>
X_train_cc = train_X_cc.to_numpy()
X_val_cc = val_X_cc.to_numpy()
X_train_ft = train_X_ft.to_numpy()
X_val_ft = val_X_ft.to_numpy()
y_train_cc = train_y_cc.to_numpy()
y_val_cc = val_y_cc.to_numpy()
y_train_ft = train_y_ft.to_numpy()
y_val_ft = val_y_ft.to_numpy()
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from sklearn.model_selection import KFold,StratifiedKFold
from numba import jit<compute_train_metric>
def root_mean_squared_log_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(K.log(y_pred + 1)- K.log(y_true + 1))))
@jit
def eval_gini(y_true, y_prob):
y_true = np.asarray(y_true)
 y_true = y_true[np.argsort(y_prob)]
ntrue = 0
gini = 0
delta = 0
n = len(y_true)
 for i in range(n-1, -1, -1):
y_i = y_true[i]
ntrue += y_i
gini += y_i * delta
delta += 1 - y_i
gini = 1 - 2 * gini /(ntrue *(n - ntrue))
return gini
def gini_xgb(preds, dtrain):
labels = dtrain.get_label()
preds -= preds.min()
preds / preds.max()
gini_score = -eval_gini(labels, preds)
 return [('gini', gini_score)]
def add_noise(series, noise_level):
return series *(1 + noise_level * np.random.randn(len(series)))
 def target_encode(trn_series=None,
val_series=None,
tst_series=None,
target=None,
min_samples_leaf=1,
smoothing=1,
noise_level=0):

 assert len(trn_series)== len(target)
 assert trn_series.name == tst_series.name
temp = pd.concat([trn_series, target], axis=1)
 averages = temp.groupby(by=trn_series.name)[target.name].agg(["mean", "count"])
 smoothing = 1 /(1 + np.exp(-(averages["count"] - min_samples_leaf)/ smoothing))
prior = target.mean()
averages[target.name] = prior *(1 - smoothing)+ averages["mean"] * smoothing
averages.drop(["mean", "count"], axis=1, inplace=True)
 ft_trn_series = pd.merge(
 trn_series.to_frame(trn_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=trn_series.name,
how='left')['average'].rename(trn_series.name + '_mean' ).fillna(prior)
 ft_trn_series.index = trn_series.index
ft_val_series = pd.merge(
 val_series.to_frame(val_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=val_series.name,
how='left')['average'].rename(trn_series.name + '_mean' ).fillna(prior)
 ft_val_series.index = val_series.index
ft_tst_series = pd.merge(
 tst_series.to_frame(tst_series.name),
averages.reset_index().rename(columns={'index': target.name, target.name: 'average'}),
on=tst_series.name,
how='left')['average'].rename(trn_series.name + '_mean' ).fillna(prior)
 ft_tst_series.index = tst_series.index
return add_noise(ft_trn_series, noise_level), add_noise(ft_val_series, noise_level), add_noise(ft_tst_series, noise_level )<load_from_csv>
def LSTM_model(n_1, input_dim, output_dim):
model = Sequential()
model.add(LSTM(n_1,input_shape=(1, input_dim), activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(output_dim, activation='relu'))
model.compile(loss=root_mean_squared_log_error, optimizer='adam')
 print(model.summary())
 return model
strdirectory = '.. /input/'
train = pd.read_csv(strdirectory+'train.csv')
 test = pd.read_csv(strdirectory+'test.csv')
 test.insert(1,'target',0)
 print(train.shape)
 print(test.shape)
 x = pd.concat([train,test])
 x = x.reset_index(drop=True)
 unwanted = x.columns[x.columns.str.startswith('ps_calc_')]
x.drop(unwanted,inplace=True,axis=1)
 x.loc[:,'ps_reg_03'] = pd.cut(x['ps_reg_03'], 50,labels=False)
 x.loc[:,'ps_car_12'] = pd.cut(x['ps_car_12'], 50,labels=False)
 x.loc[:,'ps_car_13'] = pd.cut(x['ps_car_13'], 50,labels=False)
 x.loc[:,'ps_car_14'] = pd.cut(x['ps_car_14'], 50,labels=False)
 x.loc[:,'ps_car_15'] = pd.cut(x['ps_car_15'], 50,labels=False)
 test = x.iloc[train.shape[0]:].copy()
train = x.iloc[:train.shape[0]].copy() 
<categorify>
model_cc = LSTM_model(4, X_train_cc.shape[-1], y_train_cc.shape[-1])
 model_ft = LSTM_model(4, X_train_ft.shape[-1], y_train_ft.shape[-1])
 early_stop = EarlyStopping(monitor='loss', patience=5, verbose=0, mode='min' )
features = train.columns[2:]
ranktestpreds = np.zeros(test.shape[0])
 kf = KFold(n_splits=5,shuffle=True,random_state=2017)
 for i,(train_index, test_index)in enumerate(kf.split(list(train.index))):
print('Fold: ',i)
 myfeatures = list(features[:])
 blindtrain = train.iloc[test_index].copy()
vistrain = train.iloc[train_index].copy()
mytest = test.copy()
for column in features:
vis, blind, tst = target_encode(trn_series=vistrain[column],
val_series=blindtrain[column],
tst_series=mytest[column],
target=vistrain.target,
min_samples_leaf=200,
smoothing=10,
noise_level=0)
 vistrain['te_' + column] = vis
blindtrain['te_' + column] = blind
mytest['te_' + column] = tst
myfeatures = myfeatures + list(['te_' + column])
 clf = XGBClassifier(n_estimators=2000,
objective="rank:pairwise",
learning_rate = 0.04,
max_depth = 5,
min_child_weight = 9,
subsample = 0.8,
colsample_bytree = 0.8,
reg_alpha = 10.4,
reg_lambda = 0.59,
seed = 2017,
nthread = 8,
silent = 1)
 eval_set=[(blindtrain[myfeatures],blindtrain.target)]
model = clf.fit(vistrain[myfeatures], vistrain.target,
eval_set=eval_set,
eval_metric=gini_xgb,
early_stopping_rounds=70,
verbose=False)
 print(" Best N trees = ", model.best_ntree_limit)
 print(" Best gini = ", model.best_score)
 trainpreds = model.predict_proba(blindtrain[myfeatures],ntree_limit=model.best_ntree_limit)[:,1]
print(" Best gini = ", eval_gini(blindtrain.target,trainpreds))
ranktestpreds += model.predict_proba(mytest[myfeatures],ntree_limit=model.best_ntree_limit)[:,1]
ranktestpreds /= 5
ranktestpreds -= ranktestpreds.min()
ranktestpreds /= ranktestpreds.max() 
<categorify>
print('Start model training')
 start_time = time.time()
history_cc = model_cc.fit(X_train_cc, y_train_cc, epochs = 100,validation_data =(X_val_cc, y_val_cc), verbose = 2, callbacks=[early_stop])
 model_cc.save("model_cc.h5")
 print('Time spent for model training is {} minutes'.format(round(( time.time() -start_time)/60,1)) )
xgbtestpreds = np.zeros(test.shape[0])
 kf = KFold(n_splits=5,shuffle=True,random_state=2017)
 for i,(train_index, test_index)in enumerate(kf.split(list(train.index))):
print('Fold: ',i)
 myfeatures = list(features[:])
 blindtrain = train.iloc[test_index].copy()
vistrain = train.iloc[train_index].copy()
mytest = test.copy()
for column in features:
vis, blind, tst = target_encode(trn_series=vistrain[column],
val_series=blindtrain[column],
tst_series=mytest[column],
target=vistrain.target,
min_samples_leaf=200,
smoothing=10,
noise_level=0)
 vistrain['te_' + column] = vis
blindtrain['te_' + column] = blind
mytest['te_' + column] = tst
myfeatures = myfeatures + list(['te_' + column])
 clf = XGBClassifier(n_estimators=2000,
learning_rate = 0.04,
max_depth = 5,
min_child_weight = 9,
subsample = 0.8,
colsample_bytree = 0.8,
reg_alpha = 10.4,
reg_lambda = 0.59,
seed = 2017,
nthread = 8,
silent = 1)
 eval_set=[(blindtrain[myfeatures],blindtrain.target)]
model = clf.fit(vistrain[myfeatures], vistrain.target,
eval_set=eval_set,
eval_metric=gini_xgb,
early_stopping_rounds=70,
verbose=False)
 print(" Best N trees = ", model.best_ntree_limit)
 print(" Best gini = ", model.best_score)
 trainpreds = model.predict_proba(blindtrain[myfeatures],ntree_limit=model.best_ntree_limit)[:,1]
print(" Best gini = ", eval_gini(blindtrain.target,trainpreds))
xgbtestpreds += model.predict_proba(mytest[myfeatures],ntree_limit=model.best_ntree_limit)[:,1]
xgbtestpreds /= 5
xgbtestpreds -= xgbtestpreds.min()
xgbtestpreds /= xgbtestpreds.max()<create_dataframe>
print('Start model training')
 start_time = time.time()
history_ft = model_ft.fit(X_train_ft, y_train_ft, epochs = 100,validation_data =(X_val_ft, y_val_ft), verbose = 2, callbacks=[early_stop])
 model_ft.save("model_ft.h5")
 print('Time spent for model training is {} minutes'.format(round(( time.time() -start_time)/60,1)) )
rankdata = pd.DataFrame()
rankdata['xgbnormal'] = xgbtestpreds
rankdata['xgbrank'] = ranktestpreds<save_to_csv>
yhat_val_cc = model_cc.predict(X_val_cc)
 print(yhat_val_cc )
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub.target =(rankdata.xgbnormal.rank() +rankdata.xgbrank.rank())
 sub.target -= sub.target.min()
sub.target /= sub.target.max()
sub.to_csv('xgbsubmission.csv', index = False )<define_variables>
yhat_val_ft = model_cc.predict(X_val_ft)
 print(yhat_val_ft )
USE_RGF_INSTEAD = True
MAX_XGB_ROUNDS = 400
OPTIMIZE_XGB_ROUNDS = False
XGB_LEARNING_RATE = 0.07
XGB_EARLY_STOPPING_ROUNDS = 50<import_modules>
start_time = time.time()
test['Country_Region'] = test['Country_Region'].astype(str)
 test['Province_State'] = test['Province_State'].astype(str)
 country = test['Country_Region'].drop_duplicates()
adj_input_cols = [e for e in input_cols if e not in('province_encoder', 'country_encoder', 'date_int')]
test_df = test.copy().join(pd.DataFrame(columns = adj_input_cols + output_cols))
test_df.rename(columns={'Date': 'date', 'Country_Region': 'country', 'Province_State': 'province'}, inplace=True)
 lags = np.arange(1,8,1)
 test_overlap_mask =(test_df['date'] <= train_df['date'].max())
 train_overlap_mask =(train_df['date'] >= test_df['date'].min())
 test_df.loc[test_overlap_mask, input_cols + output_cols] = train_df.loc[train_overlap_mask, input_cols + output_cols].values
pred_dt_range = pd.date_range(start = train_df['date'].max() + pd.Timedelta(days=1), end = test_df['date'].max() , freq = '1D')
 with tqdm(total = len(pred_dt_range)) as pbar:
for d in pred_dt_range:
for i in country:
province = test_df[test_df['country'] == i]['province'].drop_duplicates()
for j in province:
mask =(test_df['date'] == d)&(test_df['country'] == i)&(test_df['province'] == j)
 for lag in lags:
mask_org =(test_df['date'] ==(d - pd.Timedelta(days=lag)))&(test_df['country'] == i)&(test_df['province'] == j)
 try:
test_df.loc[mask, 'cc_cases_' + str(lag)] = test_df.loc[mask_org, 'cc_cases'].values
except:
test_df.loc[mask, 'cc_cases_' + str(lag)] = 0
try:
test_df.loc[mask, 'ft_cases_' + str(lag)] = test_df.loc[mask_org, 'ft_cases'].values
except:
test_df.loc[mask, 'ft_cases_' + str(lag)] = 0
test_X = test_df.loc[mask, input_cols]
test_X_cc = test_X[cc_input]
X_test_cc= test_X_cc
X_test_cc = X_test_cc.to_numpy().reshape(X_test_cc.shape[0], 1, X_test_cc.shape[1])
 next_cc = model_cc.predict(X_test_cc)
 next_cc_scaled = next_cc
test_X_ft = test_X[ft_input]
X_test_ft = test_X_ft
X_test_ft = X_test_ft.to_numpy().reshape(X_test_ft.shape[0], 1, X_test_ft.shape[1])
 next_ft = model_cc.predict(X_test_ft)
 next_ft_scaled = next_ft
test_df.loc[mask, 'cc_cases'] = next_cc_scaled
test_df.loc[mask, 'ft_cases'] = next_ft_scaled
pbar.update(1)
 print('Time spent for building features is {} minutes'.format(round(( time.time() -start_time)/60,1)) )
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from rgf.sklearn import RGFClassifier
from numba import jit
import time
import gc
import subprocess
import glob<compute_train_metric>
submission = pd.DataFrame()
submission['ForecastId'] = test_df['ForecastId']
submission['ConfirmedCases'] = test_df['cc_cases']
submission['Fatalities'] = test_df['ft_cases']
<categorify><EOS> 
submission.to_csv("submission.csv",index=False )