Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<import_modules>
warnings.filterwarnings('ignore')
 sns.set_style('whitegrid')
 %matplotlib inline
from sklearn.metrics import precision_recall_curve,plot_precision_recall_curve,plot_roc_curve<import_modules>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
for col in train.columns:
print('number of null values in ' + col + ': ' + str(train[pd.isnull(train[col])].shape[0]))
from sklearn.neighbors import KNeighborsClassifier<import_modules>
for col in test.columns:
print('number of null values in ' + col + ': ' + str(test[pd.isnull(test[col])].shape[0]))
from sklearn.neighbors import KNeighborsClassifier<choose_model_class>
train = train.join(pd.get_dummies(train.Sex))
test = test.join(pd.get_dummies(test.Sex))
knn_model = KNeighborsClassifier(n_neighbors=1 )<train_model>
class_dummies = pd.get_dummies(train.Pclass)
 class_dummies.columns = ['Higher', 'Middle', 'Lower']
train = train.join(class_dummies )
knn_model.fit(scaled_X_train,y_train )<predict_on_test>
X_train = train[['male', 'female','Higher', 'Middle', 'Lower']]
y = train['Survived']
class_dummies = pd.get_dummies(test.Pclass)
 class_dummies.columns = ['Higher', 'Middle', 'Lower']
test = test.join(class_dummies)
 X_test = test[['male', 'female', 'Higher', 'Middle', 'Lower']]
y_knn_pred = knn_model.predict(scaled_X_test )<compute_test_metric>
log_reg = linear_model.LogisticRegression()
baseline_log_reg = log_reg.fit(X_train, y)
 predicted_survivors = baseline_log_reg.predict(X_test)
 print('Accuracy: ', metrics.accuracy_score(train.Survived, baseline_log_reg.predict(X_train)))
 sns.heatmap(metrics.confusion_matrix(train.Survived, baseline_log_reg.predict(X_train)) ,\
cmap="Blues", annot=True)
 plt.xlabel('Pred Label')
 plt.ylabel('True Label')
 test_baseline_model = test
test_baseline_model['Survived'] = predicted_survivors
test_baseline_model = test_baseline_model[['PassengerId', 'Survived']]
print(classification_report(y_test,y_knn_pred))<choose_model_class>
first_tree = tree.DecisionTreeClassifier()
first_tree_fit = first_tree.fit(X_train, y)
 predicted_survivors = first_tree_fit.predict(X_test)
 print('Accuracy: ', metrics.accuracy_score(train.Survived, first_tree_fit.predict(X_train)))
 sns.heatmap(metrics.confusion_matrix(train.Survived, first_tree_fit.predict(X_train)) , \
cmap="Blues", annot=True)
 plt.xlabel('Pred Label')
 plt.ylabel('True Label')
 test_first_model = test
test_first_model['Survived'] = predicted_survivors
test_first_model = test_first_model[['PassengerId', 'Survived']]
knn = KNeighborsClassifier()<define_search_space>
X_train = train[['male', 'female', 'Pclass', 'new_Parch', 'new_SibSp']]
y = train['Survived']
test['new_SibSp'] = test.SibSp
test['new_SibSp'] = test.SibSp.astype(int)
 test.loc[train.new_SibSp > 1, 'new_SibSp'] = 2
test['new_Parch'] = test.Parch
test['new_Parch'] = test.new_Parch.astype(int)
 test.loc[train.new_Parch > 1, 'new_Parch'] = 2
X_test = test[['male', 'female', 'Pclass', 'new_Parch', 'new_SibSp']]
k_values = list(range(1,20))
params_grid = {'n_neighbors':k_values}<choose_model_class>
second_tree = tree.DecisionTreeClassifier()
second_tree_fit = second_tree.fit(X_train, y)
 predicted_survivors = second_tree_fit.predict(X_test)
 print('Accuracy: ', metrics.accuracy_score(train.Survived, second_tree_fit.predict(X_train)))
 sns.heatmap(metrics.confusion_matrix(train.Survived, second_tree_fit.predict(X_train)) , \
cmap="Blues", annot=True)
 plt.xlabel('Pred Label')
 plt.ylabel('True Label')
 test_second_model = test
test_second_model['Survived'] = predicted_survivors
test_second_model = test_second_model[['PassengerId', 'Survived']]
knn_grid_model = GridSearchCV(knn,param_grid=params_grid,cv=5,scoring='accuracy' )<train_model>
model_names = [
'KNeighborsClassifier',
'SVC(kernel="linear")',
'RandomForestClassifier',
'AdaBoostClassifier() ',
'GaussianNB() ',
]
models = [
KNeighborsClassifier() ,
SVC(kernel="linear"),
RandomForestClassifier() ,
AdaBoostClassifier() ,
GaussianNB()
]
fit_models = []
for i in range(len(models)) :
temp_score = cross_val_score(models[i], X_train, y, cv=5)
 fit_models.append(models[i].fit(X_train,y))
print(model_names[i],' ', temp_score.mean())
 print('+/- ',temp_score.var() )
knn_grid_model.fit(scaled_X_train,y_train )<find_best_params>
test_rf_model = test
test_rf_model['Survived'] = fit_models[2].predict(X_test)
 test_rf_model = test_rf_model[['PassengerId', 'Survived']]
knn_grid_model.best_params_<predict_on_test>
print('S embarked survival %:',\
round(( train[train.Embarked == 'S']\ 
.Survived.sum() /train[train.Embarked == 'S'].Survived.count())*100., 3))
print('C embarked survival %:',\
round(( train[train.Embarked == 'C']\ 
.Survived.sum() /train[train.Embarked == 'C'].Survived.count())*100., 3))
print('Q embarked survival %:',\
round(( train[train.Embarked == 'Q']\ 
.Survived.sum() /train[train.Embarked == 'Q'].Survived.count())*100., 3))
y_knn_grid_pred = knn_grid_model.predict(scaled_X_test )<compute_test_metric>
train[train.Embarked.isnull() ]
print(classification_report(y_test,y_knn_grid_pred))<import_modules>
train.loc[train.Embarked.isnull() , 'Embarked'] = 'S'
train = train.join(pd.get_dummies(train.Embarked))
from sklearn.svm import SVC<train_on_grid>
test = test.join(pd.get_dummies(test.Embarked))
param_grid = {'C':[0.001,0.01,0.1,0.5,1],'gamma':['scale','auto']}
grid_svc = GridSearchCV(svc,param_grid )<train_model>
test[test.Fare.isnull() ]
grid_svc.fit(scaled_X_train,y_train )<find_best_params>
test.loc[test.Fare.isnull() , 'Fare'] = test[(test.Pclass == 3)].Fare.median()
print(test[test.PassengerId == 1044] )
grid_svc.best_params_<predict_on_test>
train.loc[train.Age.isnull() , 'Age'] = -0.5
train = train.join(pd.get_dummies(pd.cut(train.Age, [-1,0,5, 12,18,35,60, 100],\
labels=['missing', 'infant', 'child', 'teen', 'youngAdult', 'adult', 'senior'])))
 train = train.join(pd.cut(train.Age, [-1,0,5, 12,18,35,60, 100],\
labels=['missing', 'infant', 'child', 'teen', 'youngAdult', 'adult', 'senior']),\
rsuffix='_class')
 test.loc[~test.Age.isnull() , 'Age'] = -0.5
test = test.join(pd.get_dummies(pd.cut(test.Age, [-1,0,5, 12,18,35,60, 100],\
labels=['missing', 'infant', 'child', 'teen', 'youngAdult', 'adult', 'senior'])) )
y_svc_grid_pred = grid_svc.predict(scaled_X_test )<compute_test_metric>
train = train.join(pd.get_dummies(train.new_SibSp, prefix='SibSp_'))
train = train.join(pd.get_dummies(train.new_Parch, prefix='Parch_'))
print(classification_report(y_test,y_svc_grid_pred))<import_modules>
test = test.join(pd.get_dummies(test.new_SibSp, prefix='SibSp_'))
test = test.join(pd.get_dummies(test.new_Parch, prefix='Parch_'))
from sklearn.tree import DecisionTreeClassifier<train_model>
features = ['female', 'male',
'Higher', 'Middle', 'Lower', 'C', 'Q', 'S',
'missing', 'infant', 'child', 'teen', 'youngAdult', 'adult', 'senior'
, 'SibSp__0', 'SibSp__1', 'SibSp__2', 'Parch__0', 'Parch__1', 'Parch__2']
target = ['Survived']
dt_model.fit(X_train,y_train )<predict_on_test>
X_train = train[features]
y = train[target]
X_test = test[features]
y_dt_pred = dt_model.predict(X_test )<create_dataframe>
model_names = [
'KNeighborsClassifier() ',
'SVC(kernel="linear")',
'RandomForestClassifier',
'AdaBoostClassifier',
'GradientBoostingClassifier'
]
models = [
KNeighborsClassifier() ,
SVC(kernel="linear"),
RandomForestClassifier() ,
AdaBoostClassifier() ,
GradientBoostingClassifier()
]
fit_models = []
for i in range(len(models)) :
temp_score = cross_val_score(models[i], X_train, y, cv=5)
 fit_models.append(models[i].fit(X_train,y))
print(model_names[i],' ', round(temp_score.mean() ,2))
print('+/- ',temp_score.var())
 print(metrics.classification_report(y, models[i].predict(X_train)) )
<import_modules><EOS> 
test_predictions_GBT = test
test_predictions_GBT['Survived'] = fit_models[4].predict(X_test)
 test_predictions_GBT[['PassengerId', 'Survived']].to_csv('test_predictions_GBT.csv', index=False )