Kaggle competition: COVID19 Global Forecasting (Week 3)
submission_df.to_csv('submission.csv', index=False )<set_options>
from pandas_profiling import ProfileReport
pd.set_option('max_columns', None)
 plt.style.use('fivethirtyeight')
 %matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 print("Libraries imported!" )<train_model>
xtrain = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/train.csv')
 xtest = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/test.csv')
 xsubmission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/submission.csv')
 xtrain.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtest.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtrain.rename(columns={'Province_State':'State'}, inplace=True)
 xtest.rename(columns={'Province_State':'State'}, inplace=True)
 xtrain.State=xtrain.State.fillna('NA')
 xtest.State=xtest.State.fillna('NA')
 xtrain['Date'] = pd.to_datetime(xtrain['Date'], infer_datetime_format=True)
 xtest['Date'] = pd.to_datetime(xtest['Date'], infer_datetime_format=True)
 for j in range(14):
train_lag=xtrain.groupby(['Country','State'] ).shift(periods=j+1)
 xtrain['lag_'+str(j+1)+'_ConfirmedCases']=train_lag['ConfirmedCases'].fillna(0)
 xtrain['lag_'+str(j+1)+'_Fatalities']=train_lag['Fatalities'].fillna(0)
class BaseModel(object):

 def __init__(self, train_df, test_df, target, features, categoricals=[],
n_splits=3, cv_method="KFold", group=None, task="regression",
parameter_tuning=False, scaler=None, verbose=True):
self.train_df = train_df
self.test_df = test_df
self.target = target
self.features = features
self.n_splits = n_splits
self.categoricals = categoricals
self.cv_method = cv_method
self.group = group
self.task = task
self.parameter_tuning = parameter_tuning
self.scaler = scaler
self.cv = self.get_cv()
self.verbose = verbose
self.params = self.get_params()
self.y_pred, self.score, self.model, self.oof, self.y_val, self.fi_df = self.fit()
def train_model(self, train_set, val_set):
raise NotImplementedError
def get_params(self):
raise NotImplementedError
def convert_dataset(self, x_train, y_train, x_val, y_val):
raise NotImplementedError
def convert_x(self, x):
return x
def calc_metric(self, y_true, y_pred):
if self.task == "classification":
return log_loss(y_true, y_pred)
 elif self.task == "regression":
return np.sqrt(mean_squared_error(y_true, y_pred))
def get_cv(self):
if self.cv_method == "KFold":
cv = KFold(n_splits=self.n_splits, shuffle=True, random_state=42)
 return cv.split(self.train_df)
 elif self.cv_method == "StratifiedKFold":
cv = StratifiedKFold(n_splits=self.n_splits, shuffle=True, random_state=42)
 return cv.split(self.train_df, self.train_df[self.target])
 elif self.cv_method == "TimeSeriesSplit":
cv = TimeSeriesSplit(max_train_size=None, n_splits=self.n_splits)
 return cv.split(self.train_df)
 elif self.cv_method == "GroupKFold":
cv = GroupKFold(n_splits=self.n_splits, shuffle=True, random_state=42)
 return cv.split(self.train_df, self.train_df[self.target], self.group)
 elif self.cv_method == "StratifiedGroupKFold":
cv = StratifiedGroupKFold(n_splits=self.n_splits, shuffle=True, random_state=42)
 return cv.split(self.train_df, self.train_df[self.target], self.group)
 def fit(self):
oof_pred = np.zeros(( self.train_df.shape[0],))
y_vals = np.zeros(( self.train_df.shape[0],))
y_pred = np.zeros(( self.test_df.shape[0],))
if self.group is not None:
if self.group in self.features:
self.features.remove(self.group)
 if self.group in self.categoricals:
self.categoricals.remove(self.group)
 fi = np.zeros(( self.n_splits, len(self.features)))
 if self.scaler is not None:
numerical_features = [f for f in self.features if f not in self.categoricals]
self.train_df[numerical_features] = self.train_df[numerical_features].fillna(self.train_df[numerical_features].median())
 self.test_df[numerical_features] = self.test_df[numerical_features].fillna(self.test_df[numerical_features].median())
 self.train_df[self.categoricals] = self.train_df[self.categoricals].fillna(self.train_df[self.categoricals].mode().iloc[0])
 self.test_df[self.categoricals] = self.test_df[self.categoricals].fillna(self.test_df[self.categoricals].mode().iloc[0])
 if self.scaler == "MinMax":
scaler = MinMaxScaler()
elif self.scaler == "Standard":
scaler = StandardScaler()
df = pd.concat([self.train_df[numerical_features], self.test_df[numerical_features]], ignore_index=True)
 scaler.fit(df[numerical_features])
 x_test = self.test_df.copy()
x_test[numerical_features] = scaler.transform(x_test[numerical_features])
 x_test = [np.absolute(x_test[i])for i in self.categoricals] + [x_test[numerical_features]]
else:
x_test = self.test_df[self.features]
for fold,(train_idx, val_idx)in enumerate(self.cv):
x_train, x_val = self.train_df.loc[train_idx, self.features], self.train_df.loc[val_idx, self.features]
y_train, y_val = self.train_df.loc[train_idx, self.target], self.train_df.loc[val_idx, self.target]
if self.scaler is not None:
x_train[numerical_features] = scaler.transform(x_train[numerical_features])
 x_val[numerical_features] = scaler.transform(x_val[numerical_features])
 x_train = [np.absolute(x_train[i])for i in self.categoricals] + [x_train[numerical_features]]
x_val = [np.absolute(x_val[i])for i in self.categoricals] + [x_val[numerical_features]]
train_set, val_set = self.convert_dataset(x_train, y_train, x_val, y_val)
 model, importance = self.train_model(train_set, val_set)
 fi[fold, :] = importance
conv_x_val = self.convert_x(x_val)
 y_vals[val_idx] = y_val
oof_pred[val_idx] = model.predict(conv_x_val ).reshape(oof_pred[val_idx].shape)
 x_test = self.convert_x(x_test)
 y_pred += model.predict(x_test ).reshape(y_pred.shape)/ self.n_splits
print('Partial score of fold {} is: {}'.format(fold, self.calc_metric(y_val, oof_pred[val_idx])))
 fi_df = pd.DataFrame()
for n in np.arange(self.n_splits):
tmp = pd.DataFrame()
tmp["features"] = self.features
tmp["importance"] = fi[n, :]
tmp["fold"] = n
fi_df = pd.concat([fi_df, tmp], ignore_index=True)
 gfi = fi_df[["features", "importance"]].groupby(["features"] ).mean().reset_index()
fi_df = fi_df.merge(gfi, on="features", how="left", suffixes=('', '_mean'))
loss_score = self.calc_metric(self.train_df[self.target], oof_pred)
 if self.verbose:
print('Our oof loss score is: ', loss_score)
 return y_pred, loss_score, model, oof_pred, y_vals, fi_df
def plot_feature_importance(self, rank_range=[1, 50]):
fig, ax = plt.subplots(1, 1, figsize=(10, 20))
sorted_df = self.fi_df.sort_values(by = "importance_mean", ascending=False ).reset_index().iloc[self.n_splits *(rank_range[0]-1): self.n_splits * rank_range[1]]
sns.barplot(data=sorted_df, x ="importance", y ="features", orient='h')
 ax.set_xlabel("feature importance")
 ax.spines['top'].set_visible(False)
 ax.spines['right'].set_visible(False)
 return sorted_df<train_model>
xtrain.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtest.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtrain.rename(columns={'Province_State':'State'}, inplace=True)
 xtest.rename(columns={'Province_State':'State'}, inplace=True)
 xtrain['Date'] = pd.to_datetime(xtrain['Date'], infer_datetime_format=True)
 xtest['Date'] = pd.to_datetime(xtest['Date'], infer_datetime_format=True)
 xtrain.info()
xtest.info()
for_y=xtest.merge(xtrain,on=['State','Country','Date'],how='inner')
 y1_xTrain = xtrain.ConfirmedCases
y1_xTrain.head()
y1_xTest = for_y.ConfirmedCases
y1_xTest.head()
y2_xTrain = xtrain.Fatalities
y2_xTrain.head()
y2_xTest = for_y.Fatalities
y2_xTest.head()
EMPTY_VAL = "NA"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
class LgbModel(BaseModel):

 def train_model(self, train_set, val_set):
verbosity = 100 if self.verbose else 0
model = lgb.train(self.params, train_set, num_boost_round = 5000, valid_sets=[train_set, val_set], verbose_eval=verbosity)
 fi = model.feature_importance(importance_type="gain")
 return model, fi
def convert_dataset(self, x_train, y_train, x_val, y_val):
train_set = lgb.Dataset(x_train, y_train, categorical_feature=self.categoricals)
 val_set = lgb.Dataset(x_val, y_val, categorical_feature=self.categoricals)
 return train_set, val_set
def get_params(self):
params = {
'num_leaves': 127,
'min_data_in_leaf': 50,
'max_depth': -1,
'learning_rate': 0.005,
"boosting_type": "gbdt",
"bagging_seed": 11,
"verbosity": -1,
'random_state': 42,
}
if self.task == "regression":
params["objective"] = "regression"
params["metric"] = "rmse"
elif self.task == "classification":
params["objective"] = "binary"
params["metric"] = "binary_logloss"
if self.parameter_tuning == True:
def objective(trial):
train_x, test_x, train_y, test_y = train_test_split(self.train_df[self.features],
self.train_df[self.target],
test_size=0.3, random_state=42)
 dtrain = lgb.Dataset(train_x, train_y, categorical_feature=self.categoricals)
 dtest = lgb.Dataset(test_x, test_y, categorical_feature=self.categoricals)
 hyperparams = {'num_leaves': trial.suggest_int('num_leaves', 24, 1024),
'boosting_type': 'gbdt',
'objective': params["objective"],
'metric': params["metric"],
'max_depth': trial.suggest_int('max_depth', 4, 16),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 20),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.4, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.4, 1.0),
'bagging_freq': trial.suggest_int('bagging_freq', 1, 7),
'min_child_samples': trial.suggest_int('min_child_samples', 5, 100),
'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-8, 10.0),
'lambda_l2': trial.suggest_loguniform('lambda_l2', 1e-8, 10.0),
'early_stopping_rounds': 100
}
model = lgb.train(hyperparams, dtrain, valid_sets=dtest, verbose_eval=500)
 pred = model.predict(test_x)
 if self.task == "classification":
return log_loss(test_y, pred)
 elif self.task == "regression":
return np.sqrt(mean_squared_error(test_y, pred))
study = optuna.create_study(direction='minimize')
 study.optimize(objective, n_trials=50)
 print('Number of finished trials: {}'.format(len(study.trials)))
 print('Best trial:')
 trial = study.best_trial
print(' Value: {}'.format(trial.value))
print(' Params: ')
 for key, value in trial.params.items() :
print(' {}: {}'.format(key, value))
params = trial.params
params["learning_rate"] = 0.001
plot_optimization_history(study)
 return params<train_model>
X_xTrain = xtrain.copy()
X_xTrain['State'].fillna(EMPTY_VAL, inplace=True)
 X_xTrain['State'] = X_xTrain.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_xTrain.loc[:, 'Date_ACT'] = X_xTrain.Date
X_xTrain.loc[:, 'Date'] = X_xTrain.Date.dt.strftime("%m%d")
 X_xTrain["Date"] = X_xTrain["Date"].astype(int)
 print(X_xTrain.head())
 X_xTest = for_y.copy()
X_xTest['State'].fillna(EMPTY_VAL, inplace=True)
 X_xTest['State'] = X_xTest.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_xTest.loc[:, 'Date'] = X_xTest.Date.dt.strftime("%m%d")
 X_xTest["Date"] = X_xTest["Date"].astype(int)
 print(X_xTest.head() )
class CatbModel(BaseModel):

 def train_model(self, train_set, val_set):
verbosity = 100 if self.verbose else 0
if self.task == "regression":
model = CatBoostRegressor(**self.params)
 elif self.task == "classification":
model = CatBoostClassifier(**self.params)
 model.fit(train_set['X'], train_set['y'], eval_set=(val_set['X'], val_set['y']),
verbose=verbosity, cat_features=self.categoricals)
 return model, model.get_feature_importance()
def convert_dataset(self, x_train, y_train, x_val, y_val):
train_set = {'X': x_train, 'y': y_train}
val_set = {'X': x_val, 'y': y_val}
return train_set, val_set
def get_params(self):
params = { 'task_type': "CPU",
'learning_rate': 0.01,
'iterations': 1000,
'random_seed': 42,
'use_best_model': True
}
if self.task == "regression":
params["loss_function"] = "RMSE"
elif self.task == "classification":
params["loss_function"] = "Logloss"
return params<normalization>
lec = preprocessing.LabelEncoder()
les = preprocessing.LabelEncoder()
X_xTrain.Country = lec.fit_transform(X_xTrain.Country)
 X_xTrain['State'] = les.fit_transform(X_xTrain['State'])
 print(X_xTrain.head())
 X_xTest.Country = lec.transform(X_xTest.Country)
 X_xTest['State'] = les.transform(X_xTest['State'])
 print(X_xTest.head())
 xtrain.loc[xtrain.Country == 'Afghanistan', :]
print(xtest.tail() )
class Mish(Layer):
def __init__(self, **kwargs):
super(Mish, self ).__init__(**kwargs)
 def build(self, input_shape):
super(Mish, self ).build(input_shape)
 def call(self, x):
return x * K.tanh(K.softplus(x))
def compute_output_shape(self, input_shape):
return input_shape
class LayerNormalization(keras.layers.Layer):
def __init__(self,
center=True,
scale=True,
epsilon=None,
gamma_initializer='ones',
beta_initializer='zeros',
gamma_regularizer=None,
beta_regularizer=None,
gamma_constraint=None,
beta_constraint=None,
**kwargs):

 super(LayerNormalization, self ).__init__(**kwargs)
 self.supports_masking = True
self.center = center
self.scale = scale
if epsilon is None:
epsilon = K.epsilon() * K.epsilon()
self.epsilon = epsilon
self.gamma_initializer = keras.initializers.get(gamma_initializer)
 self.beta_initializer = keras.initializers.get(beta_initializer)
 self.gamma_regularizer = keras.regularizers.get(gamma_regularizer)
 self.beta_regularizer = keras.regularizers.get(beta_regularizer)
 self.gamma_constraint = keras.constraints.get(gamma_constraint)
 self.beta_constraint = keras.constraints.get(beta_constraint)
 self.gamma, self.beta = None, None
def get_config(self):
config = {
'center': self.center,
'scale': self.scale,
'epsilon': self.epsilon,
'gamma_initializer': keras.initializers.serialize(self.gamma_initializer),
'beta_initializer': keras.initializers.serialize(self.beta_initializer),
'gamma_regularizer': keras.regularizers.serialize(self.gamma_regularizer),
'beta_regularizer': keras.regularizers.serialize(self.beta_regularizer),
'gamma_constraint': keras.constraints.serialize(self.gamma_constraint),
'beta_constraint': keras.constraints.serialize(self.beta_constraint),
}
base_config = super(LayerNormalization, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
def compute_mask(self, inputs, input_mask=None):
return input_mask
def build(self, input_shape):
shape = input_shape[-1:]
if self.scale:
self.gamma = self.add_weight(
 shape=shape,
initializer=self.gamma_initializer,
regularizer=self.gamma_regularizer,
constraint=self.gamma_constraint,
name='gamma', 
)
 if self.center:
self.beta = self.add_weight(
 shape=shape,
initializer=self.beta_initializer,
regularizer=self.beta_regularizer,
constraint=self.beta_constraint,
name='beta', 
)
 super(LayerNormalization, self ).build(input_shape)
 def call(self, inputs, training=None):
mean = K.mean(inputs, axis=-1, keepdims=True)
 variance = K.mean(K.square(inputs - mean), axis=-1, keepdims=True)
 std = K.sqrt(variance + self.epsilon)
 outputs =(inputs - mean)/ std
if self.scale:
outputs *= self.gamma
if self.center:
outputs += self.beta
return outputs<train_model>
filterwarnings('ignore')
 le = preprocessing.LabelEncoder()
countries = X_xTrain.Country.unique()
class NeuralNetworkModel(BaseModel):

 def train_model(self, train_set, val_set):
inputs = []
embeddings = []
embedding_out_dim = self.params['embedding_out_dim']
n_neuron = self.params['hidden_units']
for i in self.categoricals:
input_ = Input(shape=(1,))
embedding = Embedding(int(np.absolute(self.train_df[i] ).max() + 1), embedding_out_dim, input_length=1 )(input_)
 embedding = Reshape(target_shape=(embedding_out_dim,))(embedding)
 inputs.append(input_)
 embeddings.append(embedding)
 input_numeric = Input(shape=(len(self.features)- len(self.categoricals),))
embedding_numeric = Dense(n_neuron )(input_numeric)
 embedding_numeric = Mish()(embedding_numeric)
 inputs.append(input_numeric)
 embeddings.append(embedding_numeric)
 x = Concatenate()(embeddings)
 for i in np.arange(self.params['hidden_layers'] - 1):
x = Dense(n_neuron //(2 *(i+1)) )(x)
 x = Mish()(x)
 x = Dropout(self.params['hidden_dropout'] )(x)
 x = LayerNormalization()(x)
 if self.task == "regression":
out = Dense(1, activation="linear", name = "out" )(x)
 loss = "mse"
elif self.task == "classification":
out = Dense(1, activation='sigmoid', name = 'out' )(x)
 loss = "binary_crossentropy"
model = Model(inputs=inputs, outputs=out)
 model.compile(loss=loss, optimizer=Adam(lr=1e-04, beta_1=0.9, beta_2=0.999, decay=1e-04))
er = EarlyStopping(patience=10, min_delta=1e-4, restore_best_weights=True, monitor='val_loss')
 ReduceLR = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')
 model.fit(train_set['X'], train_set['y'], callbacks=[er, ReduceLR],
epochs=self.params['epochs'], batch_size=self.params['batch_size'],
validation_data=[val_set['X'], val_set['y']])
 fi = np.zeros(len(self.features))
return model, fi
def convert_dataset(self, x_train, y_train, x_val, y_val):
train_set = {'X': x_train, 'y': y_train}
val_set = {'X': x_val, 'y': y_val}
return train_set, val_set
def get_params(self):

 params = {
'input_dropout': 0.0,
'hidden_layers': 2,
'hidden_units': 128,
'embedding_out_dim': 4,
'hidden_activation': 'relu',
'hidden_dropout': 0.05,
'batch_norm': 'before_act',
'optimizer': {'type': 'adam', 'lr': 0.001},
'batch_size': 128,
'epochs': 80
}
return params<load_from_csv>
data_dict = {}
for i in glob.glob('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/WDataFiles_Stage1/*'):
name = i.split('/')[-1].split('.')[0]
if name != 'WTeamSpellings':
data_dict[name] = pd.read_csv(i)
 else:
data_dict[name] = pd.read_csv(i, encoding='cp1252' )<feature_engineering>
xout = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 k_log_col=['lag_'+str(j+1)+'_ConfirmedCases' for j in range(14)]+['lag_'+str(j+1)+'_Fatalities' for j in range(14)]
params = {'min_child_weight':[4,5], 'gamma':[i/10.0 for i in range(3,6)], 'subsample':[i/10.0 for i in range(6,8)],
'colsample_bytree':[i/10.0 for i in range(6,8)], 'max_depth': [3,4,5],'n_estimators':[500,1000],'learning_rate': [.03, 0.05,.07],
'objective':['reg:squaredlogerror']}
params = {'min_child_weight':[4,5], 'max_depth': [6],'n_estimators':[1000]}
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)))
 X_xTrain_CS = X_xTrain.loc[:, ['State', 'Country', 'Date','Date_ACT', 'ConfirmedCases', 'Fatalities']+k_log_col]
y1_xTrain_CS = X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['ConfirmedCases']-X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['lag_1_ConfirmedCases']
y2_xTrain_CS = X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['Fatalities']-X_xTrain_CS[X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'] >0]['lag_1_Fatalities']
X_xTrain_CS1=X_xTrain_CS[(X_xTrain_CS['ConfirmedCases'] - X_xTrain_CS['lag_1_ConfirmedCases'])>0].loc[:, ['State', 'Country']+k_log_col].reset_index(drop=True)
 oh1=preprocessing.OneHotEncoder(sparse=False ).fit(X_xTrain_CS.Country.values.reshape(X_xTrain_CS.Country.shape[0],1))
oh2=preprocessing.OneHotEncoder(sparse=False ).fit(X_xTrain_CS.State.values.reshape(X_xTrain_CS.Country.shape[0],1))
all_val=oh1.transform(X_xTrain_CS1.Country.values.reshape(X_xTrain_CS1.Country.shape[0],1))
col=['cnty_'+str(k)for k in range(all_val.shape[1])]
X_xTrain_CS1=pd.concat([X_xTrain_CS1,pd.DataFrame(all_val,columns=col)],axis=1)
 all_val_State=oh2.transform(X_xTrain_CS1.State.values.reshape(X_xTrain_CS1.State.shape[0],1))
col_State=['state_'+str(k)for k in range(all_val_State.shape[1])]
X_xTrain_CS1=pd.concat([X_xTrain_CS1,pd.DataFrame(all_val_State,columns=col_State)],axis=1)
 X_xTest_CS = X_xTest.loc[:, ['State', 'Country', 'Date', 'ForecastId']+k_log_col]
y1_xTest_CS = for_y.loc[:, 'ConfirmedCases']-for_y.loc[:, 'lag_1_ConfirmedCases']
y2_xTest_CS = for_y.loc[:, 'Fatalities']-for_y.loc[:, 'lag_1_Fatalities']
X_xTest_CS_Id = X_xTest_CS.loc[:, 'ForecastId']
X_xTest_CS1 = X_xTest_CS.loc[:, ['State', 'Country']+k_log_col]
all_val=oh1.transform(X_xTest_CS1.Country.values.reshape(X_xTest_CS1.Country.shape[0],1))
col=['cnty_'+str(k)for k in range(all_val.shape[1])]
X_xTest_CS1=pd.concat([X_xTest_CS1,pd.DataFrame(all_val,columns=col)],axis=1)
 all_val_State=oh2.transform(X_xTest_CS1.State.values.reshape(X_xTest_CS1.State.shape[0],1))
col_State=['state_'+str(k)for k in range(all_val_State.shape[1])]
X_xTest_CS1=pd.concat([X_xTest_CS1,pd.DataFrame(all_val_State,columns=col_State)],axis=1)
data_dict['WNCAATourneySeeds']['Seed'] = data_dict['WNCAATourneySeeds']['Seed'].apply(lambda x: int(x[1:3]))
data_dict[fname].head()<load_from_csv>
xgb1 = XGBRegressor(nthread=-1,n_jobs=-1)
 grid = GridSearchCV(xgb1, params)
 grid.fit(X_xTrain_CS1, y1_xTrain_CS)
test = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/WSampleSubmissionStage1_2020.csv')
 print(test.shape)
 test.head()<feature_engineering>
print(r2_score(y1_xTest_CS, np.where(grid.best_estimator_.predict(X_xTest_CS1)<0,0,grid.best_estimator_.predict(X_xTest_CS1))))
print(RMSLE(np.where(y1_xTest_CS<=0,0.001,y1_xTest_CS), np.where(grid.best_estimator_.predict(X_xTest_CS1)<=0,0.001,grid.best_estimator_.predict(X_xTest_CS1))))
xgb2 = XGBRegressor(nthread=-1,n_jobs=-1)
 grid1 = GridSearchCV(xgb2, params)
 grid1.fit(X_xTrain_CS1, y2_xTrain_CS)
 print(r2_score(y2_xTest_CS, np.where(grid1.best_estimator_.predict(X_xTest_CS1)<0,0,grid1.best_estimator_.predict(X_xTest_CS1))))
print(RMSLE(np.where(y2_xTest_CS<=0,0.001,y2_xTest_CS), np.where(grid1.best_estimator_.predict(X_xTest_CS1)<=0,0.001,grid1.best_estimator_.predict(X_xTest_CS1))))
print(r2_score(y2_xTest_CS, grid1.best_estimator_.predict(X_xTest_CS1)))
 print(RMSLE(y2_xTest_CS, grid1.best_estimator_.predict(X_xTest_CS1)))
 y2_xpred = grid1.best_estimator_.predict(X_xTest_CS1)
test = test.drop(['Pred'], axis=1)
 test['Season'] = test['ID'].apply(lambda x: int(x.split('_')[0]))
test['WTeamID'] = test['ID'].apply(lambda x: int(x.split('_')[1]))
test['LTeamID'] = test['ID'].apply(lambda x: int(x.split('_')[2]))
test.head()<merge>
X_xTrain_CS1_bk=X_xTrain_CS1.copy()
X_xTrain_CS12=X_xTrain_CS.copy()
all_val=oh1.transform(X_xTrain_CS12.Country.values.reshape(X_xTrain_CS12.Country.shape[0],1))
col=['cnty_'+str(k)for k in range(all_val.shape[1])]
X_xTrain_CS12=pd.concat([X_xTrain_CS12,pd.DataFrame(all_val,columns=col)],axis=1)
 all_val_State=oh2.transform(X_xTrain_CS12.State.values.reshape(X_xTrain_CS12.State.shape[0],1))
col_State=['state_'+str(k)for k in range(all_val_State.shape[1])]
X_xTrain_CS12=pd.concat([X_xTrain_CS12,pd.DataFrame(all_val_State,columns=col_State)],axis=1)
 X_xTrain_CS12=X_xTrain_CS12[X_xTrain_CS1.columns].copy()
X_xTrain_CS12['Date_ACT']=X_xTrain_CS['Date_ACT']
X_xTrain_CS12['Fatalities']=X_xTrain_CS['Fatalities']
X_xTrain_CS12['ConfirmedCases']=X_xTrain_CS['ConfirmedCases']
gameCities = pd.merge(data_dict['WGameCities'], data_dict['Cities'], how='left', on=['CityID'])
 cols_to_use = gameCities.columns.difference(train.columns ).tolist() + ["Season", "WTeamID", "LTeamID"]
train = train.merge(gameCities[cols_to_use], how="left", on=["Season", "WTeamID", "LTeamID"])
 train.head()
cols_to_use = data_dict["WSeasons"].columns.difference(train.columns ).tolist() + ["Season"]
train = train.merge(data_dict["WSeasons"][cols_to_use], how="left", on=["Season"])
 train.head()
cols_to_use = data_dict["WTeams"].columns.difference(train.columns ).tolist()
train = train.merge(data_dict["WTeams"][cols_to_use], how="left", left_on=["WTeamID"], right_on=["TeamID"])
 train.drop(['TeamID'], axis=1, inplace=True)
 train = train.merge(data_dict["WTeams"][cols_to_use], how="left", left_on=["LTeamID"], right_on=["TeamID"], suffixes=('_W', '_L'))
train.drop(['TeamID'], axis=1, inplace=True)
 print(train.shape)
 train.head()<merge>
for k in range(60):
X_xTrain_CS12
prev=X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ]
lt=[ 'lag_1_ConfirmedCases','lag_2_ConfirmedCases', 'lag_3_ConfirmedCases',
'lag_4_ConfirmedCases', 'lag_5_ConfirmedCases', 'lag_6_ConfirmedCases',
'lag_7_ConfirmedCases', 'lag_8_ConfirmedCases', 'lag_9_ConfirmedCases',
'lag_10_ConfirmedCases', 'lag_11_ConfirmedCases',
'lag_12_ConfirmedCases', 'lag_13_ConfirmedCases',
'lag_14_ConfirmedCases', 'lag_1_Fatalities','lag_2_Fatalities',
'lag_3_Fatalities', 'lag_4_Fatalities', 'lag_5_Fatalities',
'lag_6_Fatalities', 'lag_7_Fatalities', 'lag_8_Fatalities',
'lag_9_Fatalities', 'lag_10_Fatalities', 'lag_11_Fatalities',
'lag_12_Fatalities', 'lag_13_Fatalities', 'lag_14_Fatalities']
lt1=['ConfirmedCases','lag_1_ConfirmedCases', 'lag_2_ConfirmedCases', 'lag_3_ConfirmedCases',
'lag_4_ConfirmedCases', 'lag_5_ConfirmedCases', 'lag_6_ConfirmedCases',
'lag_7_ConfirmedCases', 'lag_8_ConfirmedCases', 'lag_9_ConfirmedCases',
'lag_10_ConfirmedCases', 'lag_11_ConfirmedCases',
'lag_12_ConfirmedCases', 'lag_13_ConfirmedCases',
'Fatalities', 'lag_1_Fatalities', 'lag_2_Fatalities',
'lag_3_Fatalities', 'lag_4_Fatalities', 'lag_5_Fatalities',
'lag_6_Fatalities', 'lag_7_Fatalities', 'lag_8_Fatalities',
'lag_9_Fatalities', 'lag_10_Fatalities', 'lag_11_Fatalities',
'lag_12_Fatalities', 'lag_13_Fatalities']
prev[lt]=X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ][lt1]
cc=grid.best_estimator_.predict(X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ][ ['State', 'Country']+k_log_col+col+col_State])
 prev['ConfirmedCases']=prev['lag_1_ConfirmedCases']+np.where(cc<=0,0.001,cc)
 Fl=grid1.best_estimator_.predict(X_xTrain_CS12[X_xTrain_CS12['Date_ACT']==X_xTrain_CS12.Date_ACT.max() ][ ['State', 'Country']+k_log_col+col+col_State])
 prev['Fatalities']=prev['lag_1_Fatalities']+np.where(Fl<=0,0.001,Fl)
 prev['Date_ACT']=X_xTrain_CS12.Date_ACT.max() + datetime.timedelta(days=1)
 prev.index=prev.index+1
X_xTrain_CS12=X_xTrain_CS12.append(prev)
cols_to_use = data_dict['WNCAATourneySeeds'].columns.difference(train.columns ).tolist() + ['Season']
train = train.merge(data_dict['WNCAATourneySeeds'][cols_to_use].drop_duplicates(subset=["Season","TeamID"]),
how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'])
 train.drop(['TeamID'], axis=1, inplace=True)
 train = train.merge(data_dict['WNCAATourneySeeds'][cols_to_use].drop_duplicates(subset=["Season","TeamID"]),
how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], suffixes=('_W', '_L'))
train.drop(['TeamID'], axis=1, inplace=True)
 print(train.shape)
 train.head()<merge>
X_xTrain_CS2=X_xTrain_CS12.copy()
cols_to_use = gameCities.columns.difference(test.columns ).tolist() + ["Season", "WTeamID", "LTeamID"]
test = test.merge(gameCities[cols_to_use].drop_duplicates(subset=["Season", "WTeamID", "LTeamID"]),
how="left", on=["Season", "WTeamID", "LTeamID"])
 del gameCities
gc.collect()
test.head()
cols_to_use = data_dict["WSeasons"].columns.difference(test.columns ).tolist() + ["Season"]
test = test.merge(data_dict["WSeasons"][cols_to_use].drop_duplicates(subset=["Season"]),
how="left", on=["Season"])
 test.head()
cols_to_use = data_dict["WTeams"].columns.difference(test.columns ).tolist()
test = test.merge(data_dict["WTeams"][cols_to_use].drop_duplicates(subset=["TeamID"]),
how="left", left_on=["WTeamID"], right_on=["TeamID"])
 test.drop(['TeamID'], axis=1, inplace=True)
 test = test.merge(data_dict["WTeams"][cols_to_use].drop_duplicates(subset=["TeamID"]),
how="left", left_on=["LTeamID"], right_on=["TeamID"], suffixes=('_W', '_L'))
test.drop(['TeamID'], axis=1, inplace=True)
 test.head()
cols_to_use = data_dict['WNCAATourneySeeds'].columns.difference(test.columns ).tolist() + ['Season']
test = test.merge(data_dict['WNCAATourneySeeds'][cols_to_use].drop_duplicates(subset=["Season","TeamID"]),
how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'])
 test.drop(['TeamID'], axis=1, inplace=True)
 test = test.merge(data_dict['WNCAATourneySeeds'][cols_to_use].drop_duplicates(subset=["Season","TeamID"]),
how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], suffixes=('_W', '_L'))
test.drop(['TeamID'], axis=1, inplace=True)
 print(test.shape)
 test.head()<drop_column>
X_xTrain_CS2['Country_1']=lec.inverse_transform(X_xTrain_CS2['Country'])
 X_xTrain_CS2['State_1']=les.inverse_transform(X_xTrain_CS2['State'] )
not_exist_in_test = [c for c in train.columns.values.tolist() if c not in test.columns.values.tolist() ]
print(not_exist_in_test)
 train = train.drop(not_exist_in_test, axis=1)
 train.head()<groupby>
xtest = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/test.csv')
 xsubmission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-3/submission.csv')
 xtest.rename(columns={'Country_Region':'Country'}, inplace=True)
 xtest.rename(columns={'Province_State':'State'}, inplace=True)
 EMPTY_VAL = "NA"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
xtest['Date'] = pd.to_datetime(xtest['Date'], infer_datetime_format=True)
 xtest['State'].fillna(EMPTY_VAL, inplace=True)
 xtest['State'] = xtest.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 xtest
team_win_score = regularSeason.groupby(['Season', 'WTeamID'] ).agg({'WScore':['sum', 'count', 'var']} ).reset_index()
team_win_score.columns = [' '.join(col ).strip() for col in team_win_score.columns.values]
team_loss_score = regularSeason.groupby(['Season', 'LTeamID'] ).agg({'LScore':['sum', 'count', 'var']} ).reset_index()
team_loss_score.columns = [' '.join(col ).strip() for col in team_loss_score.columns.values]
del regularSeason
gc.collect()<merge>
xtest1=xtest.merge(X_xTrain_CS2,left_on=['State','Country','Date'],right_on=['State_1','Country_1','Date_ACT'],how='inner')
 xtest1
train = pd.merge(train, team_win_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'WTeamID'])
 train = pd.merge(train, team_loss_score, how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'LTeamID'])
 train = pd.merge(train, team_loss_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'LTeamID'])
 train = pd.merge(train, team_win_score, how='left', left_on=['Season', 'LTeamID_x'], right_on=['Season', 'WTeamID'])
 train.drop(['LTeamID_y', 'WTeamID_y'], axis=1, inplace=True)
 train.head()<merge>
xtest1[(xtest1['Country_x']=='US')&(xtest1['State_x']=='New York')][['Date','ConfirmedCases','Fatalities']]
test = pd.merge(test, team_win_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'WTeamID'])
 test = pd.merge(test, team_loss_score, how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'LTeamID'])
 test = pd.merge(test, team_loss_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'LTeamID'])
 test = pd.merge(test, team_win_score, how='left', left_on=['Season', 'LTeamID_x'], right_on=['Season', 'WTeamID'])
 test.drop(['LTeamID_y', 'WTeamID_y'], axis=1, inplace=True)
 test.head()<feature_engineering>
xtest1[(xtest1['Country_x']=='India')&(xtest1['State_x']=='India')][['Date','ConfirmedCases','Fatalities']]
def preprocess(df):
df['x_score'] = df['WScore sum_x'] + df['LScore sum_y']
df['y_score'] = df['WScore sum_y'] + df['LScore sum_x']
df['x_count'] = df['WScore count_x'] + df['LScore count_y']
df['y_count'] = df['WScore count_y'] + df['WScore count_x']
df['x_var'] = df['WScore var_x'] + df['LScore count_y']
df['y_var'] = df['WScore var_y'] + df['WScore var_x']
return df
train = preprocess(train)
 test = preprocess(test )<drop_column>
xtest1[['Fatalities','ConfirmedCases','ForecastId']].to_csv('submission.csv', index=False)
 print("Submission file Created....." )