Kaggle competition: COVID19 Global Forecasting (Week 4)
print('stack_gen')
 stack_gen_model = stack_gen.fit(np.array(X), np.array(train_labels))
print('lightgbm')
 lgb_model_full_data = lightgbm.fit(X, train_labels)
 print('xgboost')
 xgb_model_full_data = xgboost.fit(X, train_labels)
 print('catoost')
 catboost_model_full_data = catboost.fit(X, train_labels)
 print('Svr')
 svr_model_full_data = svr.fit(X, train_labels)
 print('Ridge')
 ridge_model_full_data = ridge.fit(X, train_labels)
 print("Lasso")
 lasso_model_full_data = lasso.fit(X, train_labels)
 print("Elasticnet")
 elasticnet_model_full_data = elasticnet.fit(X, train_labels)
 print("kernelridge")
 kernelridge_model_full_data = kernelridge.fit(X, train_labels)
 print('RandomForest')
 rf_model_full_data = rf.fit(X, train_labels)
 print('GradientBoosting')
 gbr_model_full_data = gbr.fit(X, train_labels )<predict_on_test>
%matplotlib inline
def blended_predictions(X):
return(( 0.1 * lgb_model_full_data.predict(X)) 
(0.1 * ridge_model_full_data.predict(X)) + \ 
(0.2 * svr_model_full_data.predict(X)) + \ 
(0.1 * gbr_model_full_data.predict(X)) + \ 
(0.1 * xgb_model_full_data.predict(X)) + \ 
(0.05 * rf_model_full_data.predict(X)) + \ 
(0.05 * lasso_model_full_data.predict(X)) + \ 
(0.05 * elasticnet_model_full_data.predict(X)) + \ 
(0.05 * kernelridge_model_full_data.predict(X)) + \ 
(0.35 * stack_gen_model.predict(np.array(X)))
 +(0.05 * catboost_model_full_data.predict(X)))
 blended_score = rmsle(train_labels, blended_predictions(X))
scores['blended'] =(blended_score, 0)
 print('RMSLE score on train data:')
 print(blended_score)
 sns.set_style("white")
 fig = plt.figure(figsize=(24, 12))
ax = sns.pointplot(x=list(scores.keys()), y=[score for score, _ in scores.values() ], markers=['o'], linestyles=['-'])
 for i, score in enumerate(scores.values()):
ax.text(i, score[0] + 0.002, '{:.6f}'.format(score[0]), horizontalalignment='left', size='large', color='black', weight='semibold')
 plt.ylabel('Score(RMSE)', size=20, labelpad=12.5)
 plt.xlabel('Model', size=20, labelpad=12.5)
 plt.tick_params(axis='x', labelsize=13.5)
 plt.tick_params(axis='y', labelsize=12.5)
 plt.title('Scores of Models', size=20)
 plt.show()
print("Done")
<set_options>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
warnings.filterwarnings('ignore' )<load_from_csv>
columns = ['ln(No_of_Days)', 'Country_Region','ConfirmedCases','Fatalities']
test_set_columns = ['ln(No_of_Days)', 'Country_Region']
%%time
train_df = pd.read_csv(PATH+"train.csv")
 test_df = pd.read_csv(PATH+"test.csv" )<count_missing_values>
def extract_features(df):
df['month'] = df['Date'].apply(lambda x: int(x.split(' ')[0].split('-')[1]))
df['day'] = df['Date'].apply(lambda x: int(x.split(' ')[0].split('-')[2]))
df['is_weekend'] =(( df.Date.astype('datetime64[ns]' ).dt.dayofweek)// 4 == 1 ).astype(float)
 df['weekday'] = df.Date.astype('datetime64[ns]' ).dt.dayofweek
df['is_holyday'] = df.apply(lambda row: 1 if(row['month']==1 and row['day']==1)or(row['month']==7 and row['day']==4)or(row['month']==11 and row['day']==11)or(row['month']==12 and row['day']==25)or(row['month']==1 and row['day'] >= 15 and row['day'] <= 21 and row['weekday'] == 0)or(row['month']==2 and row['day'] >= 15 and row['day'] <= 21 and row['weekday'] == 0)or(row['month']==5 and row['day'] >= 25 and row['day'] <= 31 and row['weekday'] == 0)or(row['month']==9 and row['day'] >= 1 and row['day'] <= 7 and row['weekday'] == 0)or(row['month']==10 and row['day'] >= 8 and row['day'] <= 14 and row['weekday'] == 0)or(row['month']==11 and row['day'] >= 22 and row['day'] <= 28 and row['weekday'] == 3)else 0, axis=1)
 df['is_day_before_holyday'] = df.apply(lambda row: 1 if(row['month']==12 and row['day']==31)or(row['month']==7 and row['day']==3)or(row['month']==11 and row['day']==10)or(row['month']==12 and row['day']==24)or(row['month']==1 and row['day'] >= 14 and row['day'] <= 20 and row['weekday'] == 6)or(row['month']==2 and row['day'] >= 14 and row['day'] <= 20 and row['weekday'] == 6)or(row['month']==5 and row['day'] >= 24 and row['day'] <= 30 and row['weekday'] == 6)or(( row['month']==9 and row['day'] >= 1 and row['day'] <= 6)or(row['month']==8 and row['day'] == 31)and row['weekday'] == 6)or(row['month']==10 and row['day'] >= 7 and row['day'] <= 13 and row['weekday'] == 6)or(row['month']==11 and row['day'] >= 21 and row['day'] <= 27 and row['weekday'] == 2)else 0, axis=1)
def missing_data(data):
total = data.isnull().sum()
percent =(data.isnull().sum() /data.isnull().count() *100)
 tt = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 types = []
for col in data.columns:
dtype = str(data[col].dtype)
 types.append(dtype)
 tt['Types'] = types
return(np.transpose(tt))<count_missing_values>
extract_features(train_df)
 extract_features(test_df )
%%time
missing_data(train_df )<count_missing_values>
for i in range(len(train_df)) :
if train_df["Province_State"][i] != '':
train_df["Country_Region"][i] = train_df["Province_State"][i] + "(" + str(train_df["Country_Region"][i])+ ")"
for i in range(len(test_df)) :
if test_df["Province_State"][i] != '':
test_df["Country_Region"][i] = test_df["Province_State"][i] + "(" + str(test_df["Country_Region"][i])+ ")"
train_df.drop(columns = "Province_State", inplace=True)
 test_df.drop(columns = "Province_State", inplace=True)
%%time
missing_data(test_df )<count_values>
i = 0
for value in train_df["Country_Region"].unique() :
if i < len(train_df):
j = 1
while(train_df["Country_Region"][i] == value):
train_df["day"][i] = j
j += 1; i += 1
if i == len(train_df):
break
i = 0
for value in test_df["Country_Region"].unique() :
if i < len(test_df):
j = 72
while(test_df["Country_Region"][i] == value):
test_df["day"][i] = j
j += 1; i += 1
if i == len(test_df):
break
train_df.rename(columns = {"day" : "No_of_Days"}, inplace = True)
 test_df.rename(columns = {"day" : "No_of_Days"}, inplace = True)
 train_df['ln(No_of_Days)'] = np.log1p(train_df['No_of_Days'])
 test_df['ln(No_of_Days)'] = np.log1p(test_df['No_of_Days'] )
print("There are {}% target values with 1".format(100 * train_df["target"].value_counts() [1]/train_df.shape[0]))<sort_values>
last_date = train_df.No_of_Days.max()
df_countries = train_df[train_df['No_of_Days']==last_date]
df_countries = df_countries.groupby('Country_Region', as_index=False)['ConfirmedCases','Fatalities'].sum()
df_countries = df_countries.nlargest(10,'ConfirmedCases')
 df_trend = train_df.groupby(['No_of_Days','Country_Region'], as_index=False)['ConfirmedCases','Fatalities'].sum()
df_trend = df_trend.merge(df_countries, on='Country_Region')
 df_trend.drop(['ConfirmedCases_y','Fatalities_y'],axis=1, inplace=True)
 df_trend.rename(columns={'Country_Region':'Country', 'ConfirmedCases_x':'Cases', 'Fatalities_x':'Deaths'}, inplace=True)
 df_trend['ln(Cases)'] = np.log1p(df_trend['Cases'])
 df_trend['ln(Deaths)'] = np.log1p(df_trend['Deaths'] )
%%time
correlations = train_df[features].corr().abs().unstack().sort_values(kind="quicksort" ).reset_index()
correlations = correlations[correlations['level_0'] != correlations['level_1']]
correlations.head(10 )<count_unique_values>
train_df['ConfirmedCases'] = np.log1p(train_df['ConfirmedCases'])
 train_df['Fatalities'] = np.log1p(train_df['Fatalities'] )
%%time
features = train_df.columns.values[2:202]
unique_max_train = []
unique_max_test = []
for feature in features:
values = train_df[feature].value_counts()
unique_max_train.append([feature, values.max() , values.idxmax() ])
 values = test_df[feature].value_counts()
unique_max_test.append([feature, values.max() , values.idxmax() ] )<feature_engineering>
df_train = train_df[columns]
df_test = test_df[test_set_columns]
%%time
idx = features = train_df.columns.values[2:202]
for df in [test_df, train_df]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurtosis(axis=1)
 df['med'] = df[idx].median(axis=1 )<train_model>
submission = []
for country in df_train.Country_Region.unique() :
df_train1 = df_train[df_train["Country_Region"]==country]
cases = np.array(df_train1.ConfirmedCases)
 fatalities = np.array(df_train1.Fatalities)
 del df_train1['ConfirmedCases']
del df_train1['Fatalities']
lb = LabelEncoder()
df_train1['Country_Region'] = lb.fit_transform(df_train1['Country_Region'])
 scaler = MinMaxScaler()
train = scaler.fit_transform(df_train1.values)
 X_train, y_train = train, cases
param_dict = {
'n_estimators': [1500, 2000, 2200, 2500, 3000],
'max_depth': [10,15,20, 25, 30],
'min_child_weight': [1,2,3,4, 5, 6, 7, 8, 9, 10],
'learning_rate': [0.05, 0.1,0.15, 0.2, 0.3, 0.5],
'gamma': [0.0, 0.1, 0.2, 0.3, 0.4]
}
model_xgb_c = XGBRegressor(gamma = 0.4, learning_rate = 0.05, max_depth = 10, min_child_weight= 7, n_estimators = 1500, random_state = 37)
 polynomial_features_c= PolynomialFeatures(degree=2)
 X_train_poly = polynomial_features_c.fit_transform(X_train)
 steps1 = [('poly_features', PolynomialFeatures(4, interaction_only=True)) , 
('lg', LinearRegression())]
pipeline1 = Pipeline(steps=steps1)
 pipeline1.fit(X_train, y_train)
 x_train_cas = []
for i in range(len(X_train)) :
x = list(X_train[i])
 x.append(y_train[i])
 x_train_cas.append(x)
 x_train_cas[0]
model2 = XGBRegressor(gamma = 0.4,
learning_rate = 0.05,
max_depth = 10,
min_child_weight= 7,
n_estimators = 1500, random_state = 37)
 steps2 = [('poly_features', PolynomialFeatures(4, interaction_only=True)) , 
('XGBoost', LinearRegression())]
pipeline2 = Pipeline(steps=steps2)
 pipeline2.fit(np.array(x_train_cas), fatalities)
 df_test1 = test_df[(test_df["Country_Region"]==country)]
df_test1['Country_Region'] = lb.transform(df_test1['Country_Region'])
 ForecastId = df_test1.ForecastId.values
df_test1 = df_test1[test_set_columns]
df_test1_scaled = scaler.transform(df_test1.values)
 cases_pred = pipeline1.predict(df_test1_scaled)
 cases_pred[cases_pred < 0] = 0
x_test_cas = []
for i in range(len(df_test1_scaled)) :
x = list(df_test1_scaled[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]
fatalities_pred = pipeline2.predict(np.array(x_test_cas))
fatalities_pred[fatalities_pred < 0] = 0
for i in range(len(cases_pred)) :
d = {'ForecastId':ForecastId[i], 'ConfirmedCases':np.around(np.expm1(cases_pred[i]),decimals = 0), 'Fatalities':np.around(np.expm1(fatalities_pred[i]),decimals = 0)}
submission.append(d)
print('Train and test columns: {} {}'.format(len(train_df.columns), len(test_df.columns)) )<define_variables>
df_submit = pd.DataFrame(submission)
 df_submit.to_csv(r'submission.csv', index=False )
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']<init_hyperparams>
output_df = pd.merge(test_df, df_submit, on='ForecastId')
<train_model><EOS> 
output_df[output_df['Country_Region'] == 'India']