# ---
# 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 numpy as np
import pandas as pd
import seaborn as sns
import datasist as ds

sns.set(rc={'figure.figsize': [13, 13]}, font_scale=1.2)
# -

df = pd.read_csv('titanic.csv')

df

# df.groupby('Ticket').mode()
df.groupby(['Cabin']).sum()

df.info()

from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
df['Sex']= label_encoder.fit_transform(df['Sex'])

df['title']=df['Name'].str.split().str[1].str.split('.').str[0]

df['title'].value_counts()

top_title = list(df['title'].value_counts().sort_values(ascending=False).head(6).index)

top_title

for label in top_title:
    df[label] = np.where(df['title']==label,1,0)

df.drop(['title'],axis=1,inplace=True)

df

df['Cabin'] = df['Cabin'].str[0]

df

sns.countplot(x='Cabin',hue='Survived', data=df)

df.groupby('Cabin').describe()[['Fare','Pclass','Sex']].transpose()

# from sklearn.preprocessing import LabelEncoder
# label_encoder = LabelEncoder()
df['NCabin']= label_encoder.fit_transform(df['Cabin'])

df

df['NCabin'] = np.where(df['NCabin']==8,np.nan,df['NCabin'])

df

df.groupby(['Sex','Pclass']).median('NCabin')['NCabin']

from sklearn.impute import KNNImputer
imputer = KNNImputer()
# cols = ['SibSp','Sex', 'Fare','Pclass','Survived','NCabin']
cols = ['SibSp','Sex', 'Fare','Pclass','Survived','NCabin']
XX = df[cols]
cc=imputer.fit_transform(XX)

df['NCabin']= cc[:,-1:]

df['NCabin'] = df['NCabin'].round()

df['NCabin'].value_counts()

df['Age'].value_counts()

df

# +
# from sklearn.impute import KNNImputer
# imputer = KNNImputer()
# cols = ['SibSp', 'Fare','NCabin','Pclass','Survived','Age']
cols = ['SibSp', 'Fare','NCabin','Pclass','Survived','Age']

XX = df[cols]
cc=imputer.fit_transform(XX)
# -

df['Age']= cc[:,-1:]

df['Age'].value_counts()

df.isna().sum()

df

df = pd.get_dummies(df, columns=['NCabin'], drop_first=True)
df

df.info()

df.drop(['Cabin'], axis=1,inplace=True)
# df.drop(['NCabin'], axis=1,inplace=True)

df.info()

import pandas_profiling

# +
# pandas_profiling.ProfileReport(df)
# -

sns.heatmap(df.corr(), annot=True, fmt='.1f')

df

df.isna().sum().sort_values(ascending=False)

missing_columns = list(df.isna().sum()[df.isna().sum()>1].index)

df = pd.get_dummies(df, columns=['Embarked'], drop_first=True)
df

x = df.drop(['Survived','Name','Ticket','PassengerId'], axis=1)
y = df['Survived']

x.isna().sum()

y

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

y_train.value_counts()

# +
# from imblearn.under_sampling import RandomUnderSampler
# sampler = RandomUnderSampler()
# x_train, y_train = sampler.fit_resample(x_train, y_train)
# y_train.value_counts()
# -

from imblearn.over_sampling import SMOTE
sampler = SMOTE()
x_train, y_train = sampler.fit_resample(x_train, y_train)
y_train.value_counts()

# +
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(x_train)

x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

# +
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier

from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, fbeta_score, classification_report
# -

models = {
    "LR": LogisticRegression(),
    "KNN": KNeighborsClassifier(),
    "SVC": SVC(),
    "DT": DecisionTreeClassifier(),
    "RF": RandomForestClassifier(),
    "XGB": XGBClassifier(),
    "Naive Bayes": GaussianNB()
}

for name, model in models.items():
    print(f'Training Model {name} \n--------------')
    model.fit(x_train, y_train)
    y_pred = model.predict(x_test)
    print(f'Training Accuracy: {accuracy_score(y_train, model.predict(x_train))}')
    print(f'Testing Accuracy: {accuracy_score(y_test, y_pred)}')
    print(f'Testing Confusion Matrix: \n{confusion_matrix(y_test, y_pred)}')
    print(f'Testing Recall: {recall_score(y_test, y_pred)}')    
    print(f'Testing Precesion: {precision_score(y_test, y_pred)}')
    print(f'Testing F-1: {f1_score(y_test, y_pred)}')
    print(f'Testing F-Beta: {fbeta_score(y_test, y_pred, beta=0.5)}')
    print('-'*30)

# +
model = LogisticRegression()

model.fit(x_train, y_train)
# -

y_pred = model.predict(x_test)
y_pred

y_test

import joblib
joblib.dump(model, 'model.h5')
joblib.dump(scaler, 'scaler.h5')



StarcoderdataJupyterScriptsDedupFiltered