Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<count_missing_values>
sns.set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_scale=1, color_codes=True)
train.isna().sum(axis=0), test.isna().sum(axis=0 )<drop_column>
data= pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv" )
train.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True)
 test.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True)
 train.info, test.info<define_variables>
data["Date"] = data["Date"].apply(lambda x: x.replace("-",""))
data["Date"] = data["Date"].astype(int )
women = train.loc[train.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )<define_variables>
data = data.drop(['Province/State'],axis=1)
 data = data.dropna()
data.isnull().sum()
men = train.loc[train.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )<define_variables>
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()
embarked_s = train.loc[train.Embarked == 'S']["Survived"]
rate_embarked_s = sum(embarked_s)/len(embarked_s)
 print("% of embarked people from Southampton who survived:", rate_embarked_s )<filter>
x = data[['Lat', 'Long', 'Date']]
y1 = data[['ConfirmedCases']]
y2 = data[['Fatalities']]
x_test = test[['Lat', 'Long', 'Date']]
embarked_c = train.loc[train.Embarked == 'C']["Survived"]
rate_embarked_c = sum(embarked_c)/len(embarked_c)
 print("% of embarked people from Cherbourg who survived:", rate_embarked_c )<filter>
Tree_model = DecisionTreeClassifier(criterion='entropy' )
embarked_q = train.loc[train.Embarked == 'Q']["Survived"]
rate_embarked_q = sum(embarked_q)/len(embarked_q)
 print("% of embarked people from Queenstown who survived:", rate_embarked_q )<drop_column>
Tree_model.fit(x,y1)
 pred1 = Tree_model.predict(x_test)
 pred1 = pd.DataFrame(pred1)
 pred1.columns = ["ConfirmedCases_prediction"]
train.loc[:,'FamSize'] = train.loc[:,'SibSp'] + train.loc[:,'Parch']
test.loc[:,'FamSize'] = train.loc[:,'SibSp'] + train.loc[:,'Parch']
train = train.drop(['Parch', 'SibSp'], axis = 1)
 test = test.drop(['Parch', 'SibSp'], axis = 1 )<categorify>
Tree_model.fit(x,y2)
 pred2 = Tree_model.predict(x_test)
 pred2 = pd.DataFrame(pred2)
 pred2.columns = ["Death_prediction"]
age_imputed = train.groupby(['Pclass', 'Sex'] ).Age.transform('mean')
 train.Age.fillna(age_imputed, inplace=True )<categorify>
Sub = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv")
 Sub.columns
sub_new = Sub[["ForecastId"]]
age_imputed = test.groupby(['Pclass', 'Sex'] ).Age.transform('mean')
 test.Age.fillna(age_imputed, inplace=True )<count_values>
OP = pd.concat([pred1,pred2,sub_new],axis=1)
 OP.head()
OP.columns = ['ConfirmedCases', 'Fatalities', 'ForecastId']
OP = OP[['ForecastId','ConfirmedCases', 'Fatalities']]
train['Embarked'].value_counts()<correct_missing_values>
OP["ConfirmedCases"] = OP["ConfirmedCases"].astype(int)
 OP["Fatalities"] = OP["Fatalities"].astype(int)
train.Embarked.fillna('S', inplace=True )<data_type_conversions>
regressor = DecisionTreeRegressor(random_state = 0 )
test.Fare = test.Fare.fillna(test.Fare.mean() )<count_missing_values>
regressor.fit(x,y1)
 pred_r1 = regressor.predict(x_test)
 pred_r1 = pd.DataFrame(pred_r1)
 pred_r1.columns = ["ConfirmedCases_prediction"]
train.isna().sum(axis=0), test.isna().sum(axis=0 )<categorify>
regressor.fit(x,y2)
 pred_r2 = regressor.predict(x_test)
 pred_r2 = pd.DataFrame(pred_r2)
 pred_r2.columns = ["Death_prediction"]
gender_mapping = {'male':1, 'female':0}
embarked_mapping = {'S':0, 'C':1, 'Q':2}
train['Sex'] = train['Sex'].map(gender_mapping)
 train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Sex'] = test['Sex'].map(gender_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping )<prepare_x_and_y>
OP_dr = pd.concat([sub_new,pred_r1,pred_r2],axis=1)
 OP_dr.head()
OP_dr.columns = [ 'ForecastId','ConfirmedCases', 'Fatalities']
y_train = train.Survived.values<prepare_x_and_y>
OP_dr["ConfirmedCases"] = OP_dr["ConfirmedCases"].astype(int)
 OP_dr["Fatalities"] = OP_dr["Fatalities"].astype(int)
 OP_dr.head()
target = train['Survived'].values<drop_column>
rand_reg = RandomForestRegressor()
train = train.drop(['Survived'], axis=1 )<train_on_grid>
rand_reg.fit(x,y1)
 pred_ra1 = rand_reg.predict(x_test)
 pred_ra1 = pd.DataFrame(pred_ra1)
 pred_ra1.columns = ["ConfirmedCases_prediction"]
lr_model = LogisticRegression(random_state=10, max_iter = 1000)
 logit_params = {
"C": [1, 3, 10, 20, 30, 40],
"solver": ["lbfgs", "liblinear"]
}
logit_gs = GridSearchCV(lr_model, logit_params, scoring="accuracy", cv = 5, n_jobs=4)
 logit_gs.fit(train, y_train )<find_best_score>
rand_reg.fit(x,y2)
 pred_ra2 = rand_reg.predict(x_test)
 pred_ra2 = pd.DataFrame(pred_ra2)
 pred_ra2.columns = ["Death_prediction"]
print(logit_gs.best_score_ )<train_on_grid>
OP_ra = pd.concat([sub_new,pred_ra1,pred_ra2],axis=1)
 OP_ra.head()
OP_ra.columns = [ 'ForecastId','ConfirmedCases', 'Fatalities']
rf_model = RandomForestClassifier()
rf_params ={
'bootstrap': [True, False],
'max_depth': [10, None],
'max_features': ['auto', 'sqrt'],
'min_samples_leaf': [1, 2, 4],
'min_samples_split': [2, 5, 10],
'n_estimators': [5, 10, 15, 20, 25, 30]}
rf_gs = GridSearchCV(rf_model, rf_params, scoring='accuracy', cv=8, n_jobs=4)
 rf_gs.fit(train, y_train )<find_best_score>
OP_ra["ConfirmedCases"] = OP_ra["ConfirmedCases"].astype(int)
 OP_ra["Fatalities"] = OP_ra["Fatalities"].astype(int)
 OP_ra.head()
<train_on_grid><EOS> 
OP_ra.to_csv("submission.csv",index=False )