Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<find_best_model_class>
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
def learning_curve_plotter(Model, X, y, params_1, params_2, step=50):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)
 fig, ax = plt.subplots(1, 2, sharey=True, figsize=(16, 7))
for i,(name, params)in enumerate([params_1, params_2]):
train_score = []
val_score = []
for j in range(100, len(X_train), step):
model = Model(**params ).fit(X_train[:j], y_train[:j])
 y_train_preds = model.predict(X_train[:j])
 y_test_preds = model.predict(X_test)
 train_score.append(np.sqrt(mean_squared_error(y_train[:j], y_train_preds)))
 val_score.append(np.sqrt(mean_squared_error(y_test, y_test_preds)))
 ax[i].plot(train_score, 'r-', label='Training error')
 ax[i].plot(val_score, 'b-', label='Validation error')
 ax[i].set_title(f'{name}', fontsize=18)
 ax[i].set_xlabel('Training set size', fontsize=15, labelpad=10)
 ax[i].set_ylabel('Root mean squared log error', fontsize=15, labelpad=10)
 ax[i].legend()
plt.show()<define_search_space>
covid_cases = pd.read_csv('/kaggle/input/novel-corona-virus-2019-dataset/covid_19_data.csv')
 covid_cases.head()
param_grid_ridge = {'alpha':0.1 * np.arange(1, 70)}<choose_model_class>
training_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/train.csv")
 testing_data = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/test.csv" )
grid_ridge = GridSearchCV(Ridge() , param_grid_ridge,
cv=RepeatedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='neg_root_mean_squared_error', verbose=2, n_jobs=-1 )<train_model>
print(training_data.isnull().sum())
 print(testing_data.isnull().sum())
 print(training_data.dtypes)
 print(testing_data.dtypes)
 training_data['Province_State'].fillna("",inplace = True)
 testing_data['Province_State'].fillna("",inplace = True )
grid_ridge.fit(X_train_fe, y_train )<define_search_space>
country_list = covid_cases['Country/Region'].unique()
country_grouped_covid = covid_cases[0:1]
for country in country_list:
test_data = covid_cases['Country/Region'] == country
test_data = covid_cases[test_data]
country_grouped_covid = pd.concat([country_grouped_covid, test_data], axis=0)
 country_grouped_covid.reset_index(drop=True)
 country_grouped_covid.head()
country_grouped_covid.drop('Last Update', axis=1, inplace=True)
 country_grouped_covid['Province/State'].replace(np.nan, "Not Reported", inplace=True)
 country_grouped_covid.head()
best_params_ridge = {'alpha': 3.8000000000000003}<define_search_space>
latest_data = country_grouped_covid['ObservationDate'] == '04/13/2020'
country_data = country_grouped_covid[latest_data]
country_list = country_data['Country/Region'].unique()
print("The total number of countries with COVID-19 Confirmed cases = {}".format(country_list.size))
param_grid_lasso = {'alpha': 0.0001 * np.arange(1, 100)}<choose_model_class>
py.init_notebook_mode(connected=True)
 formated_gdf = covid_cases.groupby(['ObservationDate', 'Country/Region'])['Confirmed', 'Deaths', 'Recovered'].max()
formated_gdf = formated_gdf.reset_index()
formated_gdf['Date'] = pd.to_datetime(formated_gdf['ObservationDate'])
 formated_gdf['Date'] = formated_gdf['Date'].dt.strftime('%m/%d/%Y')
 formated_gdf['log_ConfirmedCases'] = np.log(formated_gdf.Confirmed + 1)
 formated_gdf['log_Fatalities'] = np.log(formated_gdf.Deaths + 1 )
grid_lasso = GridSearchCV(Lasso() , param_grid_lasso,
cv=RepeatedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='neg_root_mean_squared_error', verbose=2, n_jobs=-1 )<train_model>
py.init_notebook_mode(connected=True)
 formated_gdf = covid_cases.groupby(['ObservationDate', 'Country/Region'])['Confirmed', 'Deaths', 'Recovered'].max()
formated_gdf = formated_gdf.reset_index()
formated_gdf['Date'] = pd.to_datetime(formated_gdf['ObservationDate'])
 formated_gdf['Date'] = formated_gdf['Date'].dt.strftime('%m/%d/%Y')
 formated_gdf['log_ConfirmedCases'] = np.log(formated_gdf.Confirmed + 1)
 formated_gdf['log_Fatalities'] = np.log(formated_gdf.Deaths + 1 )
grid_lasso.fit(X_train_fe, y_train )<define_search_space>
folder_name = '/kaggle/input/covcsd-covid19-countries-statistical-dataset/'
file_type = 'csv'
seperator =','
dataframe = pd.concat([pd.read_csv(f, sep=seperator)for f in glob.glob(folder_name + "/*."+file_type)],ignore_index=True,sort=False )
best_params_lasso = {'alpha': 0.0002}<define_search_space>
covid_data = dataframe[['Date', 'State', 'Country', 'Cumulative_cases', 'Cumulative_death',
'Daily_cases', 'Daily_death', 'Latitude', 'Longitude', 'Temperature',
'Min_temperature', 'Max_temperature', 'Wind_speed', 'Precipitation',
'Fog_Presence', 'Population', 'Population Density/km', 'Median_Age',
'Sex_Ratio', 'Age%_65+', 'Hospital Beds/1000', 'Available Beds/1000',
'Confirmed Cases/1000', 'Lung Patients(F)', 'Lung Patients(M)',
'Life Expectancy(M)', 'Life Expectancy(F)', 'Total_tests_conducted',
'Out_Travels(mill.) ', 'In_travels(mill.) ', 'Domestic_Travels(mill.) ']]
param_grid_elastic = {'alpha': [0.0001, 0.001, 0.01, 0.1, 0.5, 1, 5, 10],
'l1_ratio':0.01 * np.arange(10)}<choose_model_class>
training_data['Country_Region'] = training_data['Country_Region'] + ' ' + training_data['Province_State']
testing_data['Country_Region'] = testing_data['Country_Region'] + ' ' + testing_data['Province_State']
del training_data['Province_State']
del testing_data['Province_State']
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
training_data.Date = training_data.Date.apply(split_date)
 testing_data.Date = testing_data.Date.apply(split_date )
grid_elastic = GridSearchCV(ElasticNet() , param_grid_elastic,
cv=RepeatedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='neg_root_mean_squared_error', verbose=2, n_jobs=-1 )<train_model>
year = []
month = []
day = []
for i in training_data.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 training_data['Year'] = year
training_data['Month'] = month
training_data['Day'] = day
del training_data['Date']
grid_elastic.fit(X_train_fe, y_train )<init_hyperparams>
year = []
month = []
day = []
for i in testing_data.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 testing_data['Year'] = year
testing_data['Month'] = month
testing_data['Day'] = day
del testing_data['Date']
del training_data['Id']
del testing_data['ForecastId']
del testing_data['Year']
del training_data['Year']
best_params_elastic = {'alpha': 0.001, 'l1_ratio': 0.09}<define_search_space>
latest_data = covid_data['Date'] == '30-03-2020'
country_data_detailed = covid_data[latest_data]
country_data_detailed.drop(['Daily_cases','Daily_death','Latitude','Longitude'],axis=1,inplace=True)
 country_data_detailed.head(3 )
param_grid_svr = {'kernel': ['rbf'],
'degree':[1],
'epsilon':[0.001, 0.01, 0.008, 0.013],
'C':[0.1, 0.5, 1, 20, 25],
'gamma':[0.0004, 0.0006, 0.0007, 0.0008]}<choose_model_class>
country_data_detailed.replace('Not Reported',np.nan,inplace=True)
 country_data_detailed.replace('N/A',np.nan,inplace=True)
 country_data_detailed.head(3 )
grid_svr = GridSearchCV(SVR() , param_grid_svr,
cv=RepeatedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='neg_root_mean_squared_error', verbose=2, n_jobs=-1 )<train_model>
country_data_detailed['Lung Patients(F)'].replace('Not reported',np.nan,inplace=True)
 country_data_detailed['Lung Patients(F)'] = country_data_detailed['Lung Patients(F)'].astype("float" )
grid_svr.fit(X_train_fe, y_train )<init_hyperparams>
temperature_data = pd.read_csv('/kaggle/input/covcsd-covid19-countries-statistical-dataset/temperature_data.csv')
 temperature_data.head()
best_params_svr = {'C': 25, 'degree': 1, 'epsilon': 0.008, 'gamma': 0.0004, 'kernel': 'rbf'}<define_search_space>
sample = temperature_dataset['Temperature'].sample(n=250)
 test = temperature_dataset['Temperature']
stat, p = ttest_ind(sample, test)
 print('Statistics=%.3f, p=%.3f' %(stat, p))
param_grid_extra = {'max_depth': [8, 11],
'max_samples':[0.6, 0.8, None],
'max_features':[0.5, 0.8, None],
'n_estimators': [200, 600, 1000]}<choose_model_class>
training_data['ConfirmedCases'] = training_data['ConfirmedCases'].apply(int)
 training_data['Fatalities'] = training_data['Fatalities'].apply(int)
 cases = training_data.ConfirmedCases
fatalities = training_data.Fatalities
del training_data['ConfirmedCases']
del training_data['Fatalities']
lb = LabelEncoder()
training_data['Country_Region'] = lb.fit_transform(training_data['Country_Region'])
 testing_data['Country_Region'] = lb.transform(testing_data['Country_Region'])
 scaler = MinMaxScaler()
x_train = scaler.fit_transform(training_data.values)
 x_test = scaler.transform(testing_data.values )
grid_extra = GridSearchCV(ExtraTreesRegressor() , param_grid_extra,
cv=RepeatedKFold(n_splits=10, n_repeats=2, random_state=42),
scoring='neg_root_mean_squared_error', verbose=2, n_jobs=-1 )<train_model>
rf = XGBRegressor(n_estimators = 1500 , max_depth = 15, learning_rate=0.1)
 rf.fit(x_train,cases)
 cases_pred = rf.predict(x_test)
 rf = XGBRegressor(n_estimators = 1500 , max_depth = 15, learning_rate=0.1)
 rf.fit(x_train,fatalities)
 fatalities_pred = rf.predict(x_test )
grid_extra.fit(X_train_fe, y_train )<init_hyperparams>
cases_pred = np.around(cases_pred)
 fatalities_pred = np.around(fatalities_pred)
 cases_pred[cases_pred < 0] = 0
fatalities_pred[fatalities_pred < 0] = 0
best_params_extra = {'max_depth': 8, 'max_features': 0.5, 'max_samples': 0.8, 'n_estimators': 200}<define_search_space>
submission_dataset = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
 submission_dataset['ConfirmedCases'] = cases_pred
submission_dataset['Fatalities'] = fatalities_pred
submission_dataset.head()
param_grid_random = {'max_depth': [8, 11],
'max_samples':[0.6, 0.8, None],
'max_features':[0.5, 0.7, None],
'n_estimators': [200, 500, 1000]}<choose_model_class>
submission_dataset.to_csv("submission.csv" , index = False )
<train_model><EOS> 
submission_dataset.to_csv("submission.csv" , index = False )