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

import pandas as pd
import os
import numpy as np

#set the path of the processed data
processed_data_path = os.path.join(os.path.pardir,'data','processed')
train_file_path = os.path.join(processed_data_path,'train.csv')
test_file_path = os.path.join(processed_data_path,'test.csv')

# read the data with all default parameters
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)

#average survival in train and test
print('Mean survival in train: {0:.3f}'.format(np.mean(y_train)))
print('Mean survival in test: {0:.3f}'.format(np.mean(y_test)))

#check Scikit learn version
import sklearn

sklearn.__version__

#baseline model
from sklearn.dummy import DummyClassifier

#create model
model_dummy = DummyClassifier(strategy = 'most_frequent',random_state = 0)

#train model
model_dummy.fit(x_train,y_train)

#Accuracy ot base model
print('Scoreline for base model: {0:.2f}'.format(model_dummy.score(x_test,y_test)))

#performane metrics
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

#accuracy score
print('Accuracy of baseline model: {0:.2f}'.format(accuracy_score(y_test,model_dummy.predict(x_test))))

#confusion matrix
print('Confusion matrix of baseline model: \n{0}'.format(confusion_matrix(y_test,model_dummy.predict(x_test))))

#precision and recall score
print('Precision of baseline model: {0:.2f}'.format(precision_score(y_test,model_dummy.predict(x_test))))
print('Recall of baseline model: {0:.2f}'.format(recall_score(y_test,model_dummy.predict(x_test))))

#first kaggle submission
#converting to the matrix
test_x = test_df.as_matrix().astype('float')

#get predictions
predictions = model_dummy.predict(test_x)

df_submission = pd.DataFrame({'PassengerId':test_df.index, 'Survived':predictions})

df_submission.head()

submission_data_path = os.path.join(os.path.pardir,'data','external')
submission_file_path = os.path.join(submission_data_path,'01_dummy.csv')

df_submission.to_csv(submission_file_path,index = False)


def get_submission_file(model,filename):
    #converting to the matrix
    test_X = test_df.as_matrix().astype('float')
    #make predictions
    predictions = model.predict(test_X)
    #submission dataframe
    df_submission = pd.DataFrame({'PassengerId':test_df.index, 'Survived':predictions})
    submission_data_path = os.path.join(os.path.pardir,'data','external')
    submission_file_path = os.path.join(submission_data_path,filename)
    #write to the file
    df_submission.to_csv(submission_file_path,index = False)


#get submission file
get_submission_file(model_dummy,'01_dummy.csv')

#logistic regression model
from sklearn.linear_model import LogisticRegression

#create model
model_lr_1 = LogisticRegression(random_state = 0)

#train model
model_lr_1.fit(x_train,y_train)

#evaluate model
print ("Score of logitic regression model version 1 is:{0:.2f}".format(model_lr_1.score(x_test,y_test)))

#performance metrics
print('Accuracy of baseline model: {0:.2f}'.format(accuracy_score(y_test,model_lr_1.predict(x_test))))
print('Confusion matrix of baseline model: \n{0}'.format(confusion_matrix(y_test,model_lr_1.predict(x_test))))
print('Precision of baseline model: {0:.2f}'.format(precision_score(y_test,model_lr_1.predict(x_test))))
print('Recall of baseline model: {0:.2f}'.format(recall_score(y_test,model_lr_1.predict(x_test))))

#model coeficients
model_lr_1.coef_

#get second submission file
get_submission_file(model_lr_1,'02_dummy.csv')

# hyperparameter optimization using k fold crossvalidation
#base model
model_lr = LogisticRegression(random_state = 0)

from sklearn.model_selection import GridSearchCV

parameters = {'C':[1.0, 10.0, 50.0, 100.0, 1000.0],'penalty':['l1','l2']}
clf = GridSearchCV(model_lr, param_grid=parameters, cv =3)

clf.fit(x_train,y_train)

clf.best_params_

print('best score: {0:.2f}'.format(clf.best_score_))

#make third submission
get_submission_file(clf,'03_lr.csv')

#feature normalization and standardization
from sklearn.preprocessing import MinMaxScaler, StandardScaler
#MinMaxScaler is for normalization and StandardScaler is for standardization

#feature normalization
scaler = MinMaxScaler()
x_train_scaled = scaler.fit_transform(x_train)

x_train_scaled[:,0].min(),x_train_scaled[:,0].max()

#normalize test data
x_test_scaled = scaler.transform(x_test)

#feature standardization
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.transform(x_test)

#create model after standardization
model_lr = LogisticRegression(random_state =0)
parameters = {'C':[1.0, 10.0, 50.0, 100.0, 1000.0],'penalty':['l1','l2']}
clf = GridSearchCV(model_lr, param_grid=parameters, cv =3)
clf.fit(x_train_scaled,y_train)

clf.best_score_

#evaluate model
print ("Score of logitic regression model version 3 is:{0:.2f}".format(clf.score(x_test_scaled,y_test)))

#make fourth submission
get_submission_file(clf,'04_lr.csv')

#model persistence using pickle
import pickle

#create the file paths
model_file_path = os.path.join(os.path.pardir,'models','lr_model_pkl')
scaler_file_path = os.path.join(os.path.pardir,'models','lr_scaler_pkl')

#open the files to write
model_file_pickle = open(model_file_path,'wb')
scaler_file_pickle = open(scaler_file_path,'wb')

#persist the model and scaler
pickle.dump(clf,model_file_pickle)
pickle.dump(scaler,scaler_file_pickle)

#close the file
model_file_pickle.close()
scaler_file_pickle.close()

#load the persisted file
#open files in read mode
model_file_pickle = open(model_file_path,'rb')
scaler_file_pickle = open(scaler_file_path,'rb')
#load files
clf_loaded = pickle.load(model_file_pickle)
scaler_loaded = pickle.load(scaler_file_pickle)
#close files
model_file_pickle.close()
scaler_file_pickle.close()

clf_loaded

scaler_loaded

x_test_scaled = scaler_loaded.transform(x_test)
print ("Score of logitic regression model version 3 is:{0:.2f}".format(clf_loaded.score(x_test_scaled,y_test)))



StarcoderdataJupyterScriptsDedupFiltered