Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
plt.style.use('seaborn')
train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv" )<drop_column>
train_df=pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df=pd.read_csv('/kaggle/input/titanic/test.csv')
 print(train_df.head(5))
print("
")
 print(test_df.head(5))
train = train[['Province_State','Country_Region','Date','ConfirmedCases','Fatalities']]
train.head()<count_missing_values>
train_df["Pclass"].replace(1, "Upper", inplace=True)
 train_df["Pclass"].replace(2, "Middle", inplace=True)
 train_df["Pclass"].replace(3, "Lower", inplace=True)
 train_df["Age"].fillna(np.nanmedian(train_df["Age"]), inplace=True)
 bins = [0,10,20,30,40,50,60,70,80,100]
train_df["Age_bin"] = pd.cut(train_df['Age'], bins)
 train_df["with_family"] =(train_df["SibSp"] + train_df["Parch"])>0
train_df["Embarked"].fillna("S", inplace=True)
 train_df["Cabin"].fillna("Unknown", inplace=True)
 train_df["Title"] = train_df.Name.apply(lambda x: x.split(",")[1].split(".")[0].strip())
 title_list = ["Mr", "Miss", "Mrs", "Master", "Dr", "Rev", "Col"]
train_df.loc[~train_df["Title"].isin(title_list), "Title"] = "NA"
for i in range(0, len(train_df)) :
train_df.at[i, "Deck"] = " ".join(re.findall("[a-zA-Z]+", train_df.at[i, "Cabin"]))
train_df["Deck"].replace("B B", "B", inplace=True)
 train_df["Deck"].replace("B B B", "B", inplace=True)
 train_df["Deck"].replace("B B B B", "B", inplace=True)
 train_df["Deck"].replace("C C", "C", inplace=True)
 train_df["Deck"].replace("D D", "D", inplace=True)
 train_df["Deck"].replace("C C C", "C", inplace=True)
 train_df["Deck"].replace("F G", "F", inplace=True)
 train_df["Deck"].replace("F E", "E", inplace=True)
 train_df["Deck"].replace("T", "Unknown", inplace=True )
print("Any missing sample in training set:",train.isnull().values.any())
 print("Any missing sample in test set:",test.isnull().values.any() , "
" )<groupby>
train_df_pre = train_df.drop(columns=["Name", "Ticket", "Cabin"])
 test_df_pre = test_df.drop(columns=[ "Name", "Ticket", "Cabin"])
 num_attribs = ["Age", "SibSp", "Parch", "Fare"]
cat_attribs = ["Pclass", "Embarked", "Deck", "Sex", "Title", "with_family", "Age_bin"]
col_transformer = ColumnTransformer([ 
("num", StandardScaler() , num_attribs), 
("cat", OneHotEncoder() , cat_attribs),
],
remainder="passthrough")
 train_array_transformed = col_transformer.fit_transform(train_df_pre)
 train_df_transformed = pd.DataFrame(data=train_array_transformed)
 column_names = num_attribs + list(col_transformer.named_transformers_['cat'].get_feature_names())+ ["PassengerId"] + ["Survived"]
train_df_transformed.columns = column_names
test_array_transformed = col_transformer.fit_transform(test_df_pre)
 test_df_transformed = pd.DataFrame(data=test_array_transformed)
 column_names = num_attribs + list(col_transformer.named_transformers_['cat'].get_feature_names())+ ["PassengerId"]
test_df_transformed.columns = column_names
ConfirmedCases_date_US = train[train['Country_Region']=='US'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_US = train[train['Country_Region']=='US'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_US = ConfirmedCases_date_US.join(fatalities_date_US)
 ConfirmedCases_date_China = train[train['Country_Region']=='China'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_China = train[train['Country_Region']=='China'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_China = ConfirmedCases_date_China.join(fatalities_date_China)
 ConfirmedCases_date_Italy = train[train['Country_Region']=='Italy'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Italy = train[train['Country_Region']=='Italy'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Italy = ConfirmedCases_date_Italy.join(fatalities_date_Italy)
 ConfirmedCases_date_Australia = train[train['Country_Region']=='Australia'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Australia = train[train['Country_Region']=='Australia'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Australia = ConfirmedCases_date_Australia.join(fatalities_date_Australia)
 plt.figure(figsize=(15,10))
plt.subplot(2, 2, 1)
 total_date_US.plot(ax=plt.gca() , title='US')
 plt.ylabel("Confirmed cases", size=13)
 plt.subplot(2, 2, 2)
 total_date_China.plot(ax=plt.gca() , title='China')
 plt.subplot(2, 2, 3)
 total_date_Italy.plot(ax=plt.gca() , title='Italy')
 plt.ylabel("Confirmed cases", size=13)
 plt.subplot(2, 2, 4)
 total_date_Australia.plot(ax=plt.gca() , title='Australia')
<merge>
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 lr = LogisticRegression(max_iter=2000)
 cv_lr = cross_val_score(lr, X_train, y_train, cv=3)
 print(cv_lr)
 print("mean accuracy: " + str(cv_lr.mean()))
lr.fit(X_train, y_train)
 lr.score(X_train, y_train)
 y_test = lr.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )
ConfirmedCases_date_Indonesia = train[train['Country_Region']=='Indonesia'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Indonesia = train[train['Country_Region']=='Indonesia'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Indonesia = ConfirmedCases_date_Indonesia.join(fatalities_date_Indonesia)
 ConfirmedCases_date_Malaysia = train[train['Country_Region']=='Malaysia'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Malaysia = train[train['Country_Region']=='Malaysia'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Malaysia = ConfirmedCases_date_Malaysia.join(fatalities_date_Malaysia)
 ConfirmedCases_date_Thailand = train[train['Country_Region']=='Thailand'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Thailand = train[train['Country_Region']=='Thailand'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Thailand = ConfirmedCases_date_Thailand.join(fatalities_date_Thailand)
 ConfirmedCases_date_Singapore = train[train['Country_Region']=='Singapore'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Singapore = train[train['Country_Region']=='Singapore'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Singapore = ConfirmedCases_date_Singapore.join(fatalities_date_Singapore)
 plt.figure(figsize=(15,10))
plt.subplot(2, 2, 1)
 total_date_Indonesia.plot(ax=plt.gca() , title='Indonesia')
 plt.ylabel("Confirmed cases", size=13)
 plt.subplot(2, 2, 2)
 total_date_Malaysia.plot(ax=plt.gca() , title='Malaysia')
 plt.subplot(2, 2, 3)
 total_date_Thailand.plot(ax=plt.gca() , title='Thailand')
 plt.ylabel("Confirmed cases", size=13)
 plt.subplot(2, 2, 4)
 total_date_Singapore.plot(ax=plt.gca() , title='Singapore')
<merge>
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 lr = LogisticRegression()
param_grid = {"max_iter" : [500,1000,2000],
"penalty" : ["l1", "l2"],
"C" : np.logspace(-4, 4, 20),
"solver" : ["liblinear"]}
lr_clf = GridSearchCV(lr, param_grid=param_grid, cv=3, verbose=True, n_jobs=-1)
 best_lr_clf = lr_clf.fit(X_train, y_train)
 print("Best Logistic Regression Score: " + str(best_lr_clf.best_score_))
print("Best Parameter: " + str(best_lr_clf.best_params_))
y_test = lr_clf.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )
ConfirmedCases_date_Vietnam = train[train['Country_Region']=='Vietnam'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Vietnam = train[train['Country_Region']=='Vietnam'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Vietnam = ConfirmedCases_date_Vietnam.join(fatalities_date_Vietnam)
 ConfirmedCases_date_Philippines = train[train['Country_Region']=='Philippines'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Philippines = train[train['Country_Region']=='Philippines'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Philippines = ConfirmedCases_date_Philippines.join(fatalities_date_Philippines)
 ConfirmedCases_date_Cambodia = train[train['Country_Region']=='Cambodia'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Cambodia = train[train['Country_Region']=='Cambodia'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Cambodia = ConfirmedCases_date_Cambodia.join(fatalities_date_Cambodia)
 ConfirmedCases_date_Laos = train[train['Country_Region']=='Laos'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Laos = train[train['Country_Region']=='Laos'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Laos = ConfirmedCases_date_Laos.join(fatalities_date_Laos)
 plt.figure(figsize=(15,10))
plt.subplot(2, 2, 1)
 total_date_Vietnam.plot(ax=plt.gca() , title='Vietnam')
 plt.ylabel("Confirmed cases", size=13)
 plt.subplot(2, 2, 2)
 total_date_Philippines.plot(ax=plt.gca() , title='Philippines')
 plt.subplot(2, 2, 3)
 total_date_Cambodia.plot(ax=plt.gca() , title='Cambodia')
 plt.ylabel("Confirmed cases", size=13)
 plt.subplot(2, 2, 4)
 total_date_Laos.plot(ax=plt.gca() , title='Laos')
<merge>
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 rnd_clf = RandomForestClassifier(n_estimators=500, n_jobs=-1)
 cv_rnd_clf = cross_val_score(rnd_clf, X_train, y_train, cv=5)
 print(cv_rnd_clf)
 print("mean accuracy: " + str(cv_rnd_clf.mean()))
rnd_clf.fit(X_train, y_train)
 rnd_clf.score(X_train, y_train)
 y_test = rnd_clf.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )
ConfirmedCases_date_Brunei = train[train['Country_Region']=='Brunei'].groupby(['Date'] ).agg({'ConfirmedCases':['sum']})
 fatalities_date_Brunei = train[train['Country_Region']=='Brunei'].groupby(['Date'] ).agg({'Fatalities':['sum']})
 total_date_Brunei = ConfirmedCases_date_Brunei.join(fatalities_date_Brunei)
 plt.figure(figsize=(15,10))
plt.subplot(2, 2, 1)
 total_date_Brunei.plot(ax=plt.gca() , title='Brunei')
 plt.ylabel("Confirmed cases", size=13)
<data_type_conversions>

 X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 rf = RandomForestClassifier()
param_grid = {'n_estimators': [200,300,400,600,800,1000,1500],
'bootstrap': [True,False],
'max_depth': [3,5,10,20,50,75,100,None],
'max_features': ['auto'],
'min_samples_leaf': [1,2,4,6,10],
'min_samples_split': [2,5,10,20]}
rnd_clf = RandomizedSearchCV(rf, param_distributions=param_grid, n_iter=500, cv=3, verbose=True, n_jobs=-1)
 best_rnd_clf = rnd_clf.fit(X_train, y_train)
 print("Best Random Forest Score: " + str(best_rnd_clf.best_score_))
print("Best Parameter: " + str(best_rnd_clf.best_params_))
best_rnd_clf.score(X_train, y_train)
 y_test = best_rnd_clf.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )
<data_type_conversions><EOS> 
export_df = pd.DataFrame()
export_df["PassengerId"] = test_df_transformed["PassengerId"].astype(int)
 export_df["Survived"] = y_test.astype(int)
 now = datetime.datetime.now()
name_add = "date_"+str(now.year)+"-"+str(now.month)+"-"+str(now.day)+"_time_"+str(now.hour)+"-"+str(now.minute)
 export_df.to_csv(f"random_forest_tuned_{name_add}.csv", index=False )