Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<merge>
%matplotlib inline
matplotlib.rcParams['figure.figsize'] =(12, 10)
 sns.set_style('whitegrid')
X_train = pd.merge(left=X_train, right=human_development_index, how='left', left_on='Country/Region', right_on='Country')
 X_test = pd.merge(left=X_test, right=human_development_index, how='left', left_on='Country/Region', right_on='Country' )<drop_column>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 sub = pd.read_csv(".. /input/gender_submission.csv" )
X_train.drop(["Country", "Gross national income(GNI)per capita 2018"], axis=1, inplace=True)
 X_test.drop(["Country", "Gross national income(GNI)per capita 2018"], axis=1, inplace=True )<load_from_csv>
Survival = train.Survived
full = pd.concat([train.drop('Survived', axis=1), test] )
night_ranger_predictors = pd.read_csv(".. /input/covid19-demographic-predictors/covid19_by_country.csv" )<drop_column>
pclass = pd.get_dummies(full1['Pclass'], prefix="Pclass_")
 pclass.head()
night_ranger_predictors = night_ranger_predictors[night_ranger_predictors.Country != "Georgia"]<merge>
full1['Sex_'] = np.where(full1.Sex == 'male', 1, 0)
 full1.head()
X_train = pd.merge(left=X_train, right=night_ranger_predictors, how='left', left_on='Country/Region', right_on='Country')
 X_test = pd.merge(left=X_test, right=night_ranger_predictors, how='left', left_on='Country/Region', right_on='Country' )<drop_column>
Embarked = pd.get_dummies(full1['Embarked'], prefix="Embarked_")
 Embarked.head()
X_train.drop(["Country", "Restrictions","Quarantine", "Schools", "Total Infected", "Total Deaths", "Total Recovered"], axis=1, inplace=True)
 X_test.drop(["Country", "Restrictions","Quarantine", "Schools", "Total Infected", "Total Deaths", "Total Recovered"], axis=1, inplace=True )<categorify>
full2 = pd.concat([full1, pclass, Embarked], axis=1)
 full2.head()
X_train = pd.concat([X_train,pd.get_dummies(X_train['Province/State'], prefix='ps')],axis=1)
 X_train.drop(['Province/State'],axis=1, inplace=True)
 X_test = pd.concat([X_test,pd.get_dummies(X_test['Province/State'], prefix='ps')],axis=1)
 X_test.drop(['Province/State'],axis=1, inplace=True )<categorify>
Title = pd.get_dummies(full.Name.map(lambda x: x.split(',')[1].split('.')[0].split() [-1]))
Title.head()
X_train = pd.concat([X_train,pd.get_dummies(X_train['Country/Region'], prefix='cr')],axis=1)
 X_train.drop(['Country/Region'],axis=1, inplace=True)
 X_test = pd.concat([X_test,pd.get_dummies(X_test['Country/Region'], prefix='cr')],axis=1)
 X_test.drop(['Country/Region'],axis=1, inplace=True )<prepare_x_and_y>
full3['FamilySize'] = full3.SibSp + full3.Parch + 1
full3['Single'] = np.where(( full3.SibSp + full3.Parch)== 0, 1, 0 )
y_train = train["Fatalities"]<choose_model_class>
full4 = pd.concat([full3, Title], axis=1)
 full4.drop('Name', axis=1, inplace=True)
 full4.head()
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
full6 = full5.drop(['SibSp','Parch'], axis=1)
 full6.head()
reg.fit(X_train, y_train, verbose=True )<groupby>
train_full = full6.iloc[:891]
test_full = full6.iloc[891:]
y_train = train.groupby(["Country/Region"] ).Fatalities.pct_change(periods=1 )<categorify>
train_age_imputer = SimpleImputer()
train_imputed = train_full.copy()
train_imputed['Age_'] = train_age_imputer.fit_transform(train_full.iloc[:,0:1])
 train_imputed['Fare_'] = train_imputed['Fare']
train_imputed.drop(['Age', 'Fare'], axis=1, inplace=True)
 train_imputed.head()
y_train = y_train.replace(np.nan, 0 )<define_variables>
test_age_imputer = SimpleImputer()
test_fare_imputer = SimpleImputer()
test_imputed = test_full.copy()
test_imputed['Age_'] = test_age_imputer.fit_transform(test_full.iloc[:,0:1])
 test_imputed['Fare_'] = test_age_imputer.fit_transform(test_full.iloc[:,1:2])
 test_imputed.drop(["Age","Fare"], axis=1, inplace=True)
 test_imputed.head()
y_train = y_train.replace(np.inf, 0 )<choose_model_class>
kfold = KFold(n_splits=5, random_state=1, shuffle=True)
 kfold
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
accuracy = {}
reg.fit(X_train, y_train, verbose=True )<prepare_x_and_y>
m1_nb = GaussianNB()
y_train = train["ConfirmedCases"]<choose_model_class>
accuracy['Gaussian Naive Bayes'] = np.mean(cross_val_score(m1_nb, train_imputed, Survival, scoring="accuracy", cv=kfold))
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
m2_log = LogisticRegression(solver='newton-cg' )
reg.fit(X_train, y_train, verbose=True )<groupby>
accuracy['Logistic Regression'] = np.mean(cross_val_score(m2_log, train_imputed, Survival, scoring="accuracy", cv=kfold))
y_train = train.groupby(["Country/Region"] ).ConfirmedCases.pct_change(periods=1 )<categorify>
m3_knn = KNeighborsClassifier(n_neighbors = 5 )
y_train = y_train.replace(np.nan, 0 )<define_variables>
accuracy['K Nearest Neighbors'] = np.mean(cross_val_score(m3_knn, train_imputed, Survival, scoring="accuracy", cv=kfold))
y_train = y_train.replace(np.inf, 0 )<choose_model_class>
m4_rf = RandomForestClassifier(n_estimators=10 )
reg = xgb.XGBRegressor(n_estimators=1000 )<train_model>
accuracy['Random Forest'] = np.mean(cross_val_score(m4_rf, train_imputed, Survival, scoring="accuracy", cv=kfold))
reg.fit(X_train, y_train, verbose=True )<train_model>
m5_svc = SVC(gamma='scale' )
y_train = train["ConfirmedCases"]
confirmed_reg = xgb.XGBRegressor(n_estimators=1000)
 confirmed_reg.fit(X_train, y_train, verbose=True)
 preds = confirmed_reg.predict(X_test)
 preds = np.array(preds)
 preds[preds < 0] = 0
preds = np.round(preds, 0 )<prepare_output>
accuracy['SVM'] = np.mean(cross_val_score(m5_svc, train_imputed, Survival, scoring="accuracy", cv=kfold))
preds = np.array(preds )<load_from_csv>
m6_gb = XGBClassifier(max_depth=3, n_estimators=300, learning_rate=0.05 )
submissionOrig = pd.read_csv(".. /input/covid19-global-forecasting-week-1/submission.csv" )<prepare_output>
accuracy['Gradient Boosting'] = np.mean(cross_val_score(m6_gb, train_imputed, Survival, scoring="accuracy", cv=kfold))
submissionOrig["ConfirmedCases"]=pd.Series(preds )<feature_engineering>
max_accuracy = max(accuracy, key=accuracy.get)
 print(max_accuracy, '\taccuracy:', accuracy[max_accuracy] )
for index, row in submissionOrig.iterrows() :
if index >= 1:
if submissionOrig.iloc[index, 'ConfirmedCases'] < submissionOrig.iloc[index - 1, 'ConfirmedCases']:
submissionOrig.at[index, 'ConfirmedCases'] = submissionOrig.iloc[index - 1,'ConfirmedCases']<predict_on_test>
m6_gb.fit(train_imputed, Survival)
 m6_gb.feature_importances_
y_train = train["Fatalities"]
confirmed_reg = xgb.XGBRegressor(n_estimators=1000)
 confirmed_reg.fit(X_train, y_train, verbose=True)
 preds = confirmed_reg.predict(X_test)
 preds = np.array(preds)
 preds[preds < 0] = 0
preds = np.round(preds, 0)
 submissionOrig["Fatalities"]=pd.Series(preds )<feature_engineering>
param_grid = {'max_depth': [1,3,5,10,15], 'n_estimators': [50,100,200,500,1000], 'learning_rate': [1,0.1,0.01,0.001,0.0001]}
grid = GridSearchCV(XGBClassifier() , param_grid, cv=kfold)
 grid.fit(train_imputed[["Fare_", "Age_", "FamilySize", "Sex_"]], Survival)
 grid.best_params_
for index, row in submissionOrig.iterrows() :
if index >= 1:
if submissionOrig.iloc[index, 'Fatalities'] < submissionOrig.iloc[index - 1, 'Fatalities']:
submissionOrig.at[index, 'Fatalities'] = submissionOrig.iloc[index - 1,'Fatalities']<save_to_csv>
gb = XGBClassifier(max_depth=3, n_estimators=1000, learning_rate=0.01)
 np.mean(cross_val_score(gb, train_imputed[["Fare_", "Age_", "FamilySize", "Sex_"]], Survival, scoring="accuracy", cv=kfold))
submissionOrig.to_csv('submission.csv',index=False )<set_options>
gb.fit(train_imputed[["Fare_", "Age_", "FamilySize", "Sex_"]], Survival)
 predictions = gb.predict(test_imputed[["Fare_", "Age_", "FamilySize", "Sex_"]] )
<load_from_csv><EOS> 
submission = pd.DataFrame({ 'PassengerId': test.PassengerId,
'Survived': predictions })
 submission.to_csv("TitanicSubmission.csv", index=False )