Kaggle competition: Titanic - Machine Learning from Disaster
X_prep_train = full_pipeline.fit_transform(X_train )<import_modules>
train = pd.read_csv('.. /input/titanic/train.csv' )
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier<import_modules>
target_class = 'Survived'
x = train.drop(target_class,axis=1)
 y = train[target_class]
from sklearn.metrics import accuracy_score, classification_report<choose_model_class>
es = ft.EntitySet(id = 'Titanic')
 es.entity_from_dataframe(entity_id = 'data', dataframe = x,
make_index = True, index = 'index')
 feature_matrix, feature_defs = ft.dfs(entityset = es, target_entity = 'data',
trans_primitives = ['add_numeric', 'multiply_numeric'])
 feature_matrix.head()
LR_clf = LogisticRegression()
LR_clf.fit(X_prep_train, y_train)
 y_pred = LR_clf.predict(X_prep_train)
 print(classification_report(y_train, y_pred))<import_modules>
pip install evalml
from sklearn.model_selection import cross_val_score, KFold<choose_model_class>
import evalml
from evalml import AutoMLSearch
kfold = KFold(n_splits=5 )<compute_train_metric>
X_train, X_holdout, y_train, y_holdout = evalml.preprocessing.split_data(x, y, test_size=0.2, random_state=0)
scores = cross_val_score(LR_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 print('Scores:',scores)
 print('Mean:',np.mean(scores))
print('Std:',np.std(scores))<import_modules>
automl = AutoMLSearch(problem_type='binary',
objective='auto',
additional_objectives=["accuracy binary"],
max_batches=1,
ensembling=True,
optimize_thresholds=True)
from sklearn.model_selection import GridSearchCV<import_modules>
automl.search(X_train, y_train )
from sklearn.model_selection import GridSearchCV<train_on_grid>
automl.rankings
param_grid = {
'C':[0.01, 0.03, 0.1, 0.3, 1, 3, 10],
'max_iter':[50, 100, 150],
}
LR_clf = LogisticRegression()
grid_search = GridSearchCV(LR_clf, param_grid, cv=kfold, scoring='accuracy', verbose=1)
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 LR_clf = grid_search.best_estimator_
scores = cross_val_score(LR_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 LR_clf.fit(X_prep_train, y_train)
 y_pred = LR_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 df_comp = pd.DataFrame([['Logistic Regression', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp<train_on_grid>
best_pipeline = automl.best_pipeline
param_grid = {
'n_neighbors': range(1, 25, 5),
'weights':['uniform', 'distance'],
'algorithm':['auto', 'ball_tree', 'kd_tree', 'brute'],
}
KNN_clf = KNeighborsClassifier()
grid_search = GridSearchCV(KNN_clf, param_grid, cv=kfold, scoring='accuracy', verbose=1)
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 KNN_clf = grid_search.best_estimator_
scores = cross_val_score(KNN_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 KNN_clf.fit(X_prep_train, y_train)
 y_pred = KNN_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 temp = pd.DataFrame([['KNN', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp = df_comp.append(temp,ignore_index=True)
 df_comp<train_on_grid>
best_pipeline.fit(X_train, y_train )
param_grid = {
}
NB_clf = GaussianNB()
grid_search = GridSearchCV(NB_clf, param_grid, cv=kfold, scoring='accuracy')
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 NB_clf = grid_search.best_estimator_
scores = cross_val_score(NB_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 NB_clf.fit(X_prep_train, y_train)
 y_pred = NB_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 temp = pd.DataFrame([['Naive Bayes', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp = df_comp.append(temp,ignore_index=True)
 df_comp<train_on_grid>
best_pipeline.score(X_holdout, y_holdout, objectives=[ "accuracy binary"] )
param_grid = {
'criterion':['gini', 'entropy'],
'splitter':['best','random'],
'max_depth':[None] + list(range(1,20, 2)) ,
'max_features':[None, 'auto', 'sqrt', 'log2'],
'ccp_alpha':[0.0, 0.01, 0.03, 0.1]
}
DT_clf = DecisionTreeClassifier()
grid_search = GridSearchCV(DT_clf, param_grid, cv=kfold, scoring='accuracy', verbose=1)
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 DT_clf = grid_search.best_estimator_
scores = cross_val_score(DT_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 DT_clf.fit(X_prep_train, y_train)
 y_pred = DT_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 temp = pd.DataFrame([['Decision Tree', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp = df_comp.append(temp,ignore_index=True)
 df_comp<train_on_grid>
permutation_importance = evalml.model_understanding.calculate_permutation_importance(best_pipeline,X_holdout, y_holdout, objective= "accuracy binary" )
param_grid = {
'C':[0.01, 0.03, 0.1, 0.3, 1, 3, 10],
'kernel':['linear', 'poly', 'rbf', 'sigmoid'],
'degree':list(range(1,5)) ,
}
SVM_clf = SVC()
grid_search = GridSearchCV(SVM_clf, param_grid, cv=kfold, scoring='accuracy', verbose=1)
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 SVM_clf = grid_search.best_estimator_
scores = cross_val_score(SVM_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 SVM_clf.fit(X_prep_train, y_train)
 y_pred = SVM_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 temp = pd.DataFrame([['SVM', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp = df_comp.append(temp,ignore_index=True)
 df_comp<train_on_grid>
test = pd.read_csv('.. /input/titanic/test.csv' )
param_grid = {
'n_estimators':range(50,200,50),
'criterion':['gini', 'entropy'],
'bootstrap':[True, False],
'max_features':['auto', 'sqrt', 'log2'],
'ccp_alpha':[0.01, 0.03, 0.1]
}
RF_clf = RandomForestClassifier()
grid_search = GridSearchCV(RF_clf, param_grid, cv=kfold, scoring='accuracy', verbose=1, n_jobs=-1)
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 RF_clf = grid_search.best_estimator_
scores = cross_val_score(RF_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 RF_clf.fit(X_prep_train, y_train)
 y_pred = RF_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 temp = pd.DataFrame([['Random Forest', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp = df_comp.append(temp,ignore_index=True)
 df_comp<train_on_grid>
test_es = ft.EntitySet(id = 'Titanic_test')
 test_es.entity_from_dataframe(entity_id = 'data', dataframe = test,
make_index = True, index = 'index' )
param_grid = {
'loss':['deviance', 'exponential'],
'learning_rate':[0.01, 0.03, 0.1, 0.3, 1],
'n_estimators':list(range(50,200,50)) ,
'criterion':['friedman_mse', 'mse', 'mae'],
'ccp_alpha':[0.01, 0.03, 0.1]
}
GB_clf = GradientBoostingClassifier()
grid_search = GridSearchCV(GB_clf, param_grid, cv=kfold, scoring='accuracy', verbose=1, n_jobs=-1)
 grid_search.fit(X_prep_train, y_train)
 print(grid_search.best_params_)
 GB_clf = grid_search.best_estimator_
scores = cross_val_score(GB_clf, X_prep_train, y_train, scoring='accuracy', cv=kfold)
 GB_clf.fit(X_prep_train, y_train)
 y_pred = GB_clf.predict(X_prep_train)
 acc = accuracy_score(y_train, y_pred)
 temp = pd.DataFrame([['Grad.Boost.', np.round(acc,2), np.round(np.mean(scores),2)]],
columns=['Algo', 'Train_acc', 'Val_acc'])
 df_comp = df_comp.append(temp,ignore_index=True)
 df_comp<normalization>
test_feature = ft.calculate_feature_matrix(feature_defs, test_es)
X_test = df_test[cat_feats + num_feats]
X_prep_test = full_pipeline.fit_transform(X_test )<predict_on_test>
ypred = best_pipeline.predict(test ).astype(int )
y_pred_test = SVM_clf.predict(X_prep_test )<save_to_csv>
predictions = pd.DataFrame(ypred, columns = ['Survived'])
 submission = pd.concat([test.PassengerId,predictions],axis=1)
 submission.to_csv('submission.csv',index=False )