Kaggle competition: COVID19 Global Forecasting (Week 1)
X = df[input_cols]
Y1 = df[output_cols[0]]
Y2 = df[output_cols[1]]<prepare_x_and_y>
train_df = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv")
 test_df = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv" )
x_test = test[input_cols]<normalization>
country_list = train_df['Country/Region'].unique()
number_list = list(range(len(country_list)))
 country_num_map = dict(zip(country_list, number_list))
<normalization>
train_df['Country_id'] = train_df['Country/Region'].map(country_num_map)
 test_df['Country_id'] = test_df['Country/Region'].map(country_num_map )
pca = PCA(n_components=3)
 pca.fit(X)
 X_pca_scaled = pca.transform(X)
 x_test_pca_scaled = pca.transform(x_test )<split>
from datetime import datetime
X_trainval, X_test, y1_trainval, y1_test, y2_trainval, y2_test = train_test_split(X_pca_scaled, Y1, Y2, random_state = 0)
 X_train, X_valid, y1_train, y1_valid, y2_train, y2_valid = train_test_split(X_trainval, y1_trainval, y2_trainval, random_state = 1)
 print("훈련 세트의 크기: {}, 검증 세트의 크기: {}, 테스트 세트의 크기: {}".format(X_train.shape[0], X_valid.shape[0], X_test.shape[0]))<train_on_grid>
train_df["Date_date"] = train_df["Date"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))
test_df["Date_date"] = test_df["Date"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))
kfold = KFold(n_splits=5, shuffle = True, random_state = 0)
 best_score = 0
for max_features in [0.5, 0.6, 0.7, 0.8, 0.9, 1]:
rf = RandomForestClassifier(n_estimators = 200, criterion = 'entropy', max_features = max_features)
 scores = cross_val_score(rf, X_trainval, y1_trainval, cv=kfold)
 score = np.mean(scores)
 if score > best_score:
best_score = score
best_parameters = {'max_features': max_features}
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y1_trainval)
 test_score = rf.score(X_test, y1_test)
 print("검증 세트에서 최고 점수: {:.2f}".format(best_score))
print("최적 매개변수: ", best_parameters)
 print("최적 매개변수에서 테스트 세트 점수: {:.2f}".format(test_score))<train_model>
train_df['Date_day'] = train_df["Date_date"].apply(lambda x: x.day)
 train_df['Date_month'] = train_df["Date_date"].apply(lambda x: x.month)
 train_df['Date_weekofyear'] = train_df["Date_date"].apply(lambda x: x.week)
 train_df['Date_dayofweek'] = train_df["Date_date"].apply(lambda x: x.dayofweek)
 train_df['Date_dayofyear'] = train_df["Date_date"].apply(lambda x: x.dayofyear )
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y1_trainval )<compute_test_metric>
test_df['Date_day'] = test_df["Date_date"].apply(lambda x: x.day)
 test_df['Date_month'] = test_df["Date_date"].apply(lambda x: x.month)
 test_df['Date_weekofyear'] = test_df["Date_date"].apply(lambda x: x.week)
 test_df['Date_dayofweek'] = test_df["Date_date"].apply(lambda x: x.dayofweek)
 test_df['Date_dayofyear'] = test_df["Date_date"].apply(lambda x: x.dayofyear )
print("훈련 세트 정확도: {:.3f}".format(rf.score(X_trainval, y1_trainval)))
 print("테스트 세트 정확도: {:.3f}".format(rf.score(X_test, y1_test)) )<predict_on_test>
train_df["Date"] = train_df["Date"].apply(lambda x: x.replace("-",""))
train_df["Date"] = train_df["Date"].astype(int )
pred1 = rf.predict(x_test_pca_scaled )<train_on_grid>
test_df["Date"] = test_df["Date"].apply(lambda x: x.replace("-",""))
test_df["Date"] = test_df["Date"].astype(int )
kfold = KFold(n_splits=5, shuffle = True, random_state = 0)
 best_score = 0
for max_features in [0.5, 0.6, 0.7, 0.8, 0.9, 1]:
rf = RandomForestClassifier(n_estimators = 200, criterion = 'entropy', max_features = max_features)
 scores = cross_val_score(rf, X_trainval, y2_trainval, cv=kfold)
 score = np.mean(scores)
 if score > best_score:
best_score = score
best_parameters = {'max_features': max_features}
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y2_trainval)
 test_score = rf.score(X_test, y2_test)
 print("검증 세트에서 최고 점수: {:.2f}".format(best_score))
print("최적 매개변수: ", best_parameters)
 print("최적 매개변수에서 테스트 세트 점수: {:.2f}".format(test_score))<train_model>
pop_df = pd.read_csv('/kaggle/input/additional/population.csv')
 gdp_df = pd.read_csv('/kaggle/input/additional/GDP.csv')
 age_df = pd.read_csv('/kaggle/input/additional/median_age.csv')
 hosbed_df = pd.read_csv('/kaggle/input/additional/hospital_beds.csv')
 deathrate_df = pd.read_csv('/kaggle/input/additional/death_rate.csv' )
rf = RandomForestClassifier(**best_parameters, criterion = 'entropy')
 rf.fit(X_trainval, y2_trainval )<compute_test_metric>
pop_df = pop_df.rename(columns={'name': 'Country', 'pop2019': 'Population', 'area': 'Area'})
 pop_df = pop_df[['Country', 'Population', 'Area', 'Density']]
gdp_df = gdp_df.rename(columns={'Country Name': 'Country', '2018': 'GDP'})
 age_df = age_df.rename(columns={'Place': 'Country', 'Median': 'Median_Age'})
 age_df = age_df[['Country', 'Median_Age']]
hosbed_df = hosbed_df.rename(columns={'Country Name': 'Country', 'bed': 'Hospital_Beds'})
 hosbed_df = hosbed_df[['Country', 'Hospital_Beds']]
deathrate_df = deathrate_df.rename(columns={'name': 'Country', 'Rate': 'Death_Rate'} )
print("훈련 세트 정확도: {:.3f}".format(rf.score(X_trainval, y2_trainval)))
 print("테스트 세트 정확도: {:.3f}".format(rf.score(X_test, y2_test)) )<predict_on_test>
for df in [pop_df, gdp_df, age_df, hosbed_df, deathrate_df]:
train_df = train_df.merge(df, left_on='Country/Region', right_on='Country', how='left' )
pred2 = rf.predict(x_test_pca_scaled )<save_to_csv>
train_df = train_df.drop(columns=['Id', 'Province/State', 'Country_x', 'Country_y',
'Country_x', 'Country_y', 'Country'] )
output = pd.DataFrame({ 'ForecastId' : ids, 'ConfirmedCases': pred1,'Fatalities':pred2 })
 output
output.to_csv('submission.csv', index=False )<set_options>
train_df = train_df.fillna(train_df.mean() )
sns.set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_scale=1, color_codes=True)
<load_from_csv>
for col in train_df.columns:
count_na = train_df[col].isna().sum()
fill_rate = 1 - count_na / len(train_df)
 print(col + ': ' + str(fill_rate))
data= pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv" )<data_type_conversions>
df_tmp = train_df.copy()
df_tmp = df_tmp.drop_duplicates(subset='Country/Region', keep='first')
 df_tmp = df_tmp[['Country/Region', 'Population', 'Area', 'Density', 'GDP', 'Median_Age', 'Hospital_Beds',
'Death_Rate']]
test_df = test_df.merge(df_tmp, on='Country/Region', how='left' )
data["Date"] = data["Date"].apply(lambda x: x.replace("-",""))
data["Date"] = data["Date"].astype(int )<count_missing_values>
cols_1 = ['Country_id', 'Lat', 'Long', 'Date_day', 'Date_month', 'Date_weekofyear', 'Date_dayofyear']
cols_2 = ['Country_id', 'Lat', 'Long', 'Date_day', 'Date_month', 'Date_weekofyear', 'Date_dayofyear',
'Population', 'Density', 'GDP', 'Median_Age', 'Hospital_Beds', 'Death_Rate']
data = data.drop(['Province/State'],axis=1)
 data = data.dropna()
data.isnull().sum() 
<load_from_csv>
x1 = train_df[cols_2]
x2 = train_df[cols_2]
y1 = train_df['ConfirmedCases']
y2 = train_df['Fatalities']
x_test_1 = test_df[cols_2]
x_test_2 = test_df[cols_2]
test = pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv")
 test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int)
 test["Lat"] = test["Lat"].fillna(12.5211)
 test["Long"] = test["Long"].fillna(69.9683)
 test.isnull().sum() 
<prepare_x_and_y>
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
x = data[['Lat', 'Long', 'Date']]
y1 = data[['ConfirmedCases']]
y2 = data[['Fatalities']]
x_test = test[['Lat', 'Long', 'Date']] 
<choose_model_class>
dt_r = DecisionTreeRegressor(random_state = 0)
 rf = RandomForestRegressor(random_state = 0 )
Tree_model = DecisionTreeClassifier(criterion='entropy' )<predict_on_test>
dt_r.fit(x1,y1)
 pred1 = dt_r.predict(x_test_1)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]
Tree_model.fit(x,y1)
 pred1 = Tree_model.predict(x_test)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]<predict_on_test>
dt_r.fit(x2,y2)
 pred2 = dt_r.predict(x_test_2)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"]
Tree_model.fit(x,y2)
 pred2 = Tree_model.predict(x_test)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"]<load_from_csv>
sub_df = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/submission.csv" )
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Sub.columns
sub_new = Sub[["ForecastId"]]<concatenate>
sub_new_df = sub_df[["ForecastId"]]
OP = pd.concat([pred1,pred2,sub_new_df],axis=1)
 OP.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
OP = OP[['ForecastId','ConfirmedCases', 'Fatalities']]
OP["ConfirmedCases"] = OP["ConfirmedCases"].astype(int)
 OP["Fatalities"] = OP["Fatalities"].astype(int )
<data_type_conversions><EOS> 
OP.to_csv("submission.csv",index=False )