Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<split>
from fastai.tabular import *
X = train_df.copy()
X_test_full = test_df.copy()
X.dropna(axis=0, subset=['Fatalities'], inplace=True)
 y = X.Fatalities
X.drop(['Fatalities'], axis=1, inplace=True)
 X_train_full, X_valid_full, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2,
random_state=0)
 low_cardinality_cols = [cname for cname in X_train_full.columns if X_train_full[cname].nunique() < 10 and
X_train_full[cname].dtype == "object"]
numeric_cols = [cname for cname in X_train_full.columns if X_train_full[cname].dtype in ['int64', 'float64']]
my_cols = low_cardinality_cols + numeric_cols
X_train = X_train_full[my_cols].copy()
X_valid = X_valid_full[my_cols].copy()
X_test = X_test_full[my_cols].copy()
X_train = pd.get_dummies(X_train)
 X_valid = pd.get_dummies(X_valid)
 X_test = pd.get_dummies(X_test)
 X_train, X_valid = X_train.align(X_valid, join='left', axis=1)
 X_train, X_test = X_train.align(X_test, join='left', axis=1 )<choose_model_class>
path = Path('/kaggle/input/covid19-global-forecasting-week-1/')
 path.ls()
model2 = RandomForestClassifier(n_estimators=150, max_depth=4, random_state=1)
 model = GradientBoostingClassifier(random_state=1)
 model3 = DecisionTreeClassifier(random_state=1)
 model = XGBRegressor()
model_1 = RandomForestClassifier(n_estimators=50, random_state=0)
 model_2 = RandomForestClassifier(n_estimators=100, random_state=0)
 model_3 = RandomForestClassifier(n_estimators=200, min_samples_split=20, random_state=0)
 model_4 = RandomForestClassifier(n_estimators=300, max_depth=6, random_state=1)
 model.fit(X_train, y_train)
 y_predictions = model.predict(X_valid)
 print('model accuracy score',model.score(X_valid,y_valid))<prepare_x_and_y>
sample_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv")
 train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv" )
y_test=y_valid
X_test=X_valid<train_model>
Full_data = pd.merge(test, train, on=['Lat','Long','Date','Country/Region','Province/State'])
 Full_data.rename(columns={'Province/State':'Province'}, inplace=True)
 Full_data.rename(columns={'Country/Region':'Country'}, inplace=True)
 Full_data.rename(columns={'ConfirmedCases':'Confirmed'}, inplace=True)
 Full_data.head()
model2.fit(X_train,y_train)
 print(f'Model test accuracy: {model2.score(X_test, y_test)*100:.3f}%')
 model3.fit(X_train,y_train)
 print(f'Model test accuracy: {model3.score(X_test, y_test)*100:.3f}%' )<train_model>
train.rename(columns={'Province/State':'Province'}, inplace=True)
 train.rename(columns={'Country/Region':'Country'}, inplace=True)
 train.rename(columns={'ConfirmedCases':'Confirmed'}, inplace=True )
model_1.fit(X_train,y_train)
 print(f'Model test accuracy: {model_1.score(X_test, y_test)*100:.3f}%')
 model_2.fit(X_train,y_train)
 print(f'Model test accuracy: {model_2.score(X_test, y_test)*100:.3f}%')
 model_3.fit(X_train,y_train)
 print(f'Model test accuracy: {model_3.score(X_test, y_test)*100:.3f}%')
 model_4.fit(X_train,y_train)
 print(f'Model test accuracy: {model_4.score(X_test, y_test)*100:.3f}%' )<choose_model_class>
test.rename(columns={'Province/State':'Province'}, inplace=True)
 test.rename(columns={'Country/Region':'Country'}, inplace=True )
regressor = DecisionTreeRegressor(random_state = 0 )<prepare_x_and_y>
bridge_types =('Lat', 'Date', 'Province', 'Country', 'Long', 'Confirmed',
'ForecastId', 'Id')
 countries = pd.DataFrame(train, columns=['Country'])
 labelencoder = LabelEncoder()
train['Countries'] = labelencoder.fit_transform(train['Country'])
 train['Countries'].head()
x = train[['Lat', 'Long','Countries','dayofweek','month','dayofyear','weekofyear']]
y1 = train[['Confirmed']]
y2 = train[['Fatalities']]
x_test = test[['Lat', 'Long','Countries','dayofweek','month','dayofyear','weekofyear']]<predict_on_test>
regressor.fit(x,y1)
 predict_1 = regressor.predict(x_test)
 predict_1 = pd.DataFrame(predict_1)
 predict_1.columns = ["Confirmed_predict"]<predict_on_test>
train['Date']= pd.to_datetime(train['Date'])
 test['Date']= pd.to_datetime(test['Date'] )
regressor.fit(x,y2)
 predict_2 = regressor.predict(x_test)
 predict_2 = pd.DataFrame(predict_2)
 predict_2.columns = ["Death_prediction"]
predict_2.head()<load_from_csv>
train = train.set_index(['Date'])
 test = test.set_index(['Date'] )
Samle_submission = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Samle_submission.columns
submission = Samle_submission[["ForecastId"]]<concatenate>
def create_time_features(df):

 df['date'] = df.index
df['hour'] = df['date'].dt.hour
df['dayofweek'] = df['date'].dt.dayofweek
df['quarter'] = df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear
df['dayofmonth'] = df['date'].dt.day
df['weekofyear'] = df['date'].dt.weekofyear
X = df[['hour','dayofweek','quarter','month','year',
'dayofyear','dayofmonth','weekofyear']]
return X
Final_submission = pd.concat([predict_1,predict_2,submission],axis=1)
 Final_submission.head()<data_type_conversions>
train.drop("date", axis=1, inplace=True)
 test.drop("date", axis=1, inplace=True )
Final_submission.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
Final_submission = Final_submission[['ForecastId','ConfirmedCases', 'Fatalities']]
Final_submission["ConfirmedCases"] = Final_submission["ConfirmedCases"].astype(int)
 Final_submission["Fatalities"] = Final_submission["Fatalities"].astype(int )<save_to_csv>
test['Countries'] = labelencoder.fit_transform(test['Country'])
Final_submission.to_csv("submission.csv",index=False)
 print('Model ready for submission!' )<train_model>
train.isnull().sum()
<set_options>
train.isnull().sum()
%matplotlib inline<load_from_csv>
train.drop(['Country'], axis=1, inplace=True)
 train.drop(['Province'], axis=1, inplace=True )
filename = ".. /input/covid19-global-forecasting-week-1/train.csv"
dt = pd.read_csv(filename)
<load_from_csv>
test.drop(['Country'], axis=1, inplace=True)
 test.drop(['Province'], axis=1, inplace=True )
series = read_csv('.. /input/covid19-global-forecasting-week-1/train.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 series.plot()
pyplot.show()<load_from_csv>
train_procent=int(((len(Full_data)) /100)*50)
 test_procent=int(((len(Full_data)) /100)*50)
 train_df=Full_data.loc[train_procent:]
test_df=Full_data.loc[:test_procent]
filename = ".. /input/hubei-china-data/Hubei_China_2.csv"
df = pd.read_csv(filename)
<load_from_csv>
X = train_df.copy()
X_test_full = test_df.copy()
X.dropna(axis=0, subset=['Fatalities'], inplace=True)
 y = X.Fatalities
X.drop(['Fatalities'], axis=1, inplace=True)
 X_train_full, X_valid_full, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2,
random_state=0)
 low_cardinality_cols = [cname for cname in X_train_full.columns if X_train_full[cname].nunique() < 10 and
X_train_full[cname].dtype == "object"]
numeric_cols = [cname for cname in X_train_full.columns if X_train_full[cname].dtype in ['int64', 'float64']]
my_cols = low_cardinality_cols + numeric_cols
X_train = X_train_full[my_cols].copy()
X_valid = X_valid_full[my_cols].copy()
X_test = X_test_full[my_cols].copy()
X_train = pd.get_dummies(X_train)
 X_valid = pd.get_dummies(X_valid)
 X_test = pd.get_dummies(X_test)
 X_train, X_valid = X_train.align(X_valid, join='left', axis=1)
 X_train, X_test = X_train.align(X_test, join='left', axis=1 )
series = read_csv('.. /input/hubei-china-data/Hubei_China_2.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 series.plot()
pyplot.show()<load_from_csv>
model2 = RandomForestClassifier(n_estimators=150, max_depth=4, random_state=1)
 model = GradientBoostingClassifier(random_state=1)
 model3 = DecisionTreeClassifier(random_state=1)
 model = XGBRegressor()
model_1 = RandomForestClassifier(n_estimators=50, random_state=0)
 model_2 = RandomForestClassifier(n_estimators=100, random_state=0)
 model_3 = RandomForestClassifier(n_estimators=200, min_samples_split=20, random_state=0)
 model_4 = RandomForestClassifier(n_estimators=300, max_depth=6, random_state=1)
 model.fit(X_train, y_train)
 y_predictions = model.predict(X_valid)
 print('model accuracy score',model.score(X_valid,y_valid))
filename = ".. /input/daytoday-china-hubei/DayToDay.csv"
df = pd.read_csv(filename )<load_from_csv>
y_test=y_valid
X_test=X_valid
series = read_csv('.. /input/daytoday-china-hubei/DayToDay.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 series.plot()
pyplot.show()<load_from_csv>
model2.fit(X_train,y_train)
 print(f'Model test accuracy: {model2.score(X_test, y_test)*100:.3f}%')
 model3.fit(X_train,y_train)
 print(f'Model test accuracy: {model3.score(X_test, y_test)*100:.3f}%' )
filename = ".. /input/submissioncsv/submission.csv"
df = pd.read_csv(filename)
<load_from_csv>
model_1.fit(X_train,y_train)
 print(f'Model test accuracy: {model_1.score(X_test, y_test)*100:.3f}%')
 model_2.fit(X_train,y_train)
 print(f'Model test accuracy: {model_2.score(X_test, y_test)*100:.3f}%')
 model_3.fit(X_train,y_train)
 print(f'Model test accuracy: {model_3.score(X_test, y_test)*100:.3f}%')
 model_4.fit(X_train,y_train)
 print(f'Model test accuracy: {model_4.score(X_test, y_test)*100:.3f}%' )
series = read_csv('.. /input/submissioncsv/submission.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 series.plot()
pyplot.show()<load_from_csv>
regressor = DecisionTreeRegressor(random_state = 0 )
series = read_csv('.. /input/submissioncsv/submission.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 lag_plot(series )<save_to_csv>
x = train[['Lat', 'Long','Countries','dayofweek','month','dayofyear','weekofyear']]
y1 = train[['Confirmed']]
y2 = train[['Fatalities']]
x_test = test[['Lat', 'Long','Countries','dayofweek','month','dayofyear','weekofyear']]
Submission.to_csv('submission.csv',index=False )<load_from_csv>
regressor.fit(x,y1)
 predict_1 = regressor.predict(x_test)
 predict_1 = pd.DataFrame(predict_1)
 predict_1.columns = ["Confirmed_predict"]
cleaned_data = pd.read_csv('.. /input/corona-virus-report/covid_19_clean_complete.csv', parse_dates=['Date'])
 cleaned_data.rename(columns={'ObservationDate': 'date',
'Province/State':'state',
'Country/Region':'country',
'Last Update':'last_updated',
'Confirmed': 'confirmed',
'Deaths':'deaths',
'Recovered':'recovered'
}, inplace=True)
 cases = ['confirmed', 'deaths', 'recovered', 'active']
cleaned_data['active'] = cleaned_data['confirmed'] - cleaned_data['deaths'] - cleaned_data['recovered']
cleaned_data['country'] = cleaned_data['country'].replace('Mainland China', 'China')
 cleaned_data[['state']] = cleaned_data[['state']].fillna('')
 cleaned_data[cases] = cleaned_data[cases].fillna(0)
 cleaned_data.rename(columns={'Date':'date'}, inplace=True)
 data = cleaned_data
display(data.head())
 display(data.info() )<load_from_csv>
regressor.fit(x,y2)
 predict_2 = regressor.predict(x_test)
 predict_2 = pd.DataFrame(predict_2)
 predict_2.columns = ["Death_prediction"]
predict_2.head()
def p2f(x):

 try:
return float(x.strip('%')) /100
except:
return np.nan
def age2int(x):

 try:
return int(x)
 except:
return np.nan
def fert2float(x):

 try:
return float(x)
 except:
return np.nan
countries_df = pd.read_csv(".. /input/population-by-country-2020/population_by_country_2020.csv", converters={'Urban Pop %':p2f,
'Fert.Rate':fert2float,
'Med.Age':age2int})
 countries_df.rename(columns={'Country(or dependency)': 'country',
'Population(2020)' : 'population',
'Density(P/Km²)' : 'density',
'Fert.Rate' : 'fertility',
'Med.Age' : "age",
'Urban Pop %' : 'urban_percentage'}, inplace=True)
 countries_df['country'] = countries_df['country'].replace('United States', 'US')
 countries_df = countries_df[["country", "population", "density", "fertility", "age", "urban_percentage"]]
countries_df.head()<merge>
Samle_submission = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Samle_submission.columns
submission = Samle_submission[["ForecastId"]]
data = pd.merge(data, countries_df, on='country' )<data_type_conversions>
Final_submission = pd.concat([predict_1,predict_2,submission],axis=1)
 Final_submission.head()
formated_gdf = data.groupby(['date', 'country'])['confirmed', 'population'].max()
formated_gdf = formated_gdf.reset_index()
formated_gdf['date'] = pd.to_datetime(formated_gdf['date'])
 formated_gdf['date'] = formated_gdf['date'].dt.strftime('%m/%d/%Y')
 formated_gdf['infectionRate'] = round(( formated_gdf['confirmed']/formated_gdf['population'])*100, 8)
 fig = px.scatter_geo(formated_gdf, locations="country", locationmode='country names',
color="infectionRate", size='infectionRate', hover_name="country",
range_color= [0, 0.2],
projection="natural earth", animation_frame="date",
title='COVID-19: Spread Over Time(Normalized by Country Population)', color_continuous_scale="portland")
 fig.show()<data_type_conversions>
Final_submission.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
Final_submission = Final_submission[['ForecastId','ConfirmedCases', 'Fatalities']]
Final_submission["ConfirmedCases"] = Final_submission["ConfirmedCases"].astype(int)
 Final_submission["Fatalities"] = Final_submission["Fatalities"].astype(int )
formated_gdf = data.groupby(['date', 'country'])['confirmed', 'deaths'].max()
formated_gdf = formated_gdf.reset_index()
formated_gdf['date'] = pd.to_datetime(formated_gdf['date'])
 formated_gdf['date'] = formated_gdf['date'].dt.strftime('%m/%d/%Y')
 formated_gdf['mortalityRate'] = round(( formated_gdf['deaths']/formated_gdf['confirmed'])*100, 2)
 fig = px.scatter_geo(formated_gdf.fillna(0), locations="country", locationmode='country names',
color="mortalityRate", size='mortalityRate', hover_name="country",
range_color= [0, 10],
projection="natural earth", animation_frame="date",
title='COVID-19: Mortality Rate in % by country', color_continuous_scale="portland")
 fig.show()<load_from_csv>
Final_submission.to_csv("submission.csv",index=False)
 print('Model ready for submission!' )
icu_df = pd.read_csv(".. /input/hospital-beds-by-country/API_SH.MED.BEDS.ZS_DS2_en_csv_v2_887506.csv")
 icu_df['Country Name'] = icu_df['Country Name'].replace('United States', 'US')
 icu_df['Country Name'] = icu_df['Country Name'].replace('Russian Federation', 'Russia')
 icu_df['Country Name'] = icu_df['Country Name'].replace('Iran, Islamic Rep.', 'Iran')
 icu_df['Country Name'] = icu_df['Country Name'].replace('Egypt, Arab Rep.', 'Egypt')
 icu_df['Country Name'] = icu_df['Country Name'].replace('Venezuela, RB', 'Venezuela')
 data['country'] = data['country'].replace('Czechia', 'Czech Republic')
<feature_engineering>