<jupyter_start><jupyter_text>Titanic Survival : Predicitve Model Notebook**Author**:🧕🏿 <NAME> \**Date** : 31th August, 2019  Building Predictive Model - Baseline Model<jupyter_code>import os
import numpy as np
import pandas as pd
%matplotlib inline
processed_data_path = os.path.join (os.path.pardir, 'data','processed')
test_file_path = os.path.join (processed_data_path, 'test.csv')
train_file_path = os.path.join (processed_data_path, 'train.csv')
train_df = pd.read_csv(train_file_path, index_col = 'PassengerId')
test_df = pd.read_csv(test_file_path, index_col = 'PassengerId')
train_df.info()
test_df.info()<jupyter_output><empty_output><jupyter_text>Data Preparation<jupyter_code>X = train_df.loc[:, 'Age':].as_matrix().astype('float')
y = train_df['Survived'].ravel()
print (X.shape, y.shape)
#train test split
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
print(np.mean(y_train), np.mean(y_test))<jupyter_output><empty_output><jupyter_text>Baseline Model<jupyter_code>from sklearn.dummy import DummyClassifier
#creates the model
model_dummy = DummyClassifier(strategy='most_frequent', random_state = 0)
#training the model
model_dummy.fit(X_train,y_train);
#Basline Accuracy
print('Accuracy Score for the bseline Model : {0: .3f}'.format(model_dummy.score(X_test,y_test)))

#performance metricz

from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score
#ACCURACY
print('accuracy for baseline model: {0: .3f}' .format(accuracy_score(y_test, model_dummy.predict(X_test))))
#CONFUSION MATRIX
print('confusion_matrix for baseline model: \n {0}' .format(confusion_matrix(y_test, model_dummy.predict(X_test))))
print('precision for baseline model: {0: .3f}' .format(precision_score(y_test, model_dummy.predict(X_test))))
print('recall for baseline model: {0: .3f}' .format(recall_score(y_test, model_dummy.predict(X_test))))<jupyter_output><empty_output><jupyter_text>Submitting the Baseline Model<jupyter_code>def get_submission_file(model, filename):
    #converting to the matrix
    test_X = test_df.as_matrix().astype('float')
    #make predicitions
    predictions = model.predict(test_X)
    #dataframe to submit
    df_submission = pd.DataFrame({'PassengerId':test_df.index, 'Survived':predictions})
    #submission file 
    submission_data_path = os.path.join(os.path.pardir, 'data','external')
    submission_file_path = os.path.join(submission_data_path, filename)
    df_submission.to_csv(submission_file_path, index = False)

get_submission_file(model_dummy, 'dummy_01.csv')
from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression(random_state = 0)
lr_model.fit(X_train, y_train);
print('Score For logistic Regression : Version 0.0.1 - {0: .2f}'. format(lr_model.score(X_test,y_test)))
#ACCURACY
print('accuracy for baseline model: {0: .3f}' .format(accuracy_score(y_test, lr_model.predict(X_test))))
#CONFUSION MATRIX
print('confusion_matrix for baseline model: \n {0}' .format(confusion_matrix(y_test, lr_model.predict(X_test))))
print('precision for baseline model: {0: .3f}' .format(precision_score(y_test, lr_model.predict(X_test))))
print('recall for baseline model: {0: .3f}' .format(recall_score(y_test, lr_model.predict(X_test))))

get_submission_file(lr_model, 'lr_model.csv');

from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression(random_state = 0)
lr_model.fit(X_train, y_train);
print('Score For logistic Regression : Version 0.0.1 - {0: .2f}'. format(lr_model.score(X_test,y_test)))<jupyter_output><empty_output><jupyter_text>Performance Metrics For Logisitic Regression Model<jupyter_code>#ACCURACY
print('accuracy for baseline model: {0: .3f}' .format(accuracy_score(y_test, lr_model.predict(X_test))))
#CONFUSION MATRIX
print('confusion_matrix for baseline model: \n {0}' .format(confusion_matrix(y_test, lr_model.predict(X_test))))
print('precision for baseline model: {0: .3f}' .format(precision_score(y_test, lr_model.predict(X_test))))
print('recall for baseline model: {0: .3f}' .format(recall_score(y_test, lr_model.predict(X_test))))
lr_model.coef_
get_submission_file(lr_model, 'lr_model.csv');<jupyter_output><empty_output><jupyter_text>Hyper Parameter Optimization<jupyter_code>lr_model = LogisticRegression(random_state= 0)
from sklearn.model_selection import GridSearchCV
params = {'C':[1.0,10.0, 50.0,100.0, 1000.0], 'penalty':['l1','l2']}
clf = GridSearchCV(lr_model, param_grid = params, cv = 3)
clf.fit(X_train, y_train)
clf.best_params_
get_submission_file(clf,'03_lr.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13379983036471585, 'fraction_numerical': 0.014843087362171332, 'mean_word_length': 8.088631984585742, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '573750', 'license': 'MIT', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1717, 'n_words': 320, 'path': 'notebooks/prediction-model.ipynb', 'repo_name': 'thayeylolu/Titanic-Disaster', 'size': '4725'}