# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Titanic Survival : Predicitve Model Notebook
# **Author**:🧕🏿 <NAME> \
# **Date** : 31th August, 2019

# ### Building Predictive Model - Baseline Model
#

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()

# ### Data Preparation

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))

# #### Baseline Model

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))))


#  ### Submitting the Baseline Model

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)))

# ### Performance Metrics For Logisitic Regression Model

#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');

# ## Hyper Parameter Optimization

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')




StarcoderdataJupyterScriptsDedupFiltered