Difference makes the DIFFERENCE
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report
df = pd.read_csv('/content/titanic_train.csv')
df.head(2)
df.isnull()
sns.heatmap(df.isnull(), yticklabels= False, cbar = False, cmap = "Blues")
sns.countplot(x='Survived', data = df)
sns.countplot(x = 'Survived', hue = "Sex", data = df)
sns.countplot(x='Survived', hue = "Pclass", data = df)
df.drop('PassengerId', inplace = True, axis = 1)
sns.pairplot(df)
df.head(2)
sns.boxplot(x = 'Pclass', y = 'Age', data = df)
def impute_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age) == True:
if Pclass == 1:
return 37
elif Pclass == 2:
return 32
else:
return 24
else:
return Age
df['Age'] = df[['Age', 'Pclass']].apply(impute_age, axis = 1)
sns.heatmap(df.isnull(), cmap = 'Blues')
df.head(2)
df.drop(['Name', 'Ticket'], inplace = True, axis = 1)
df.head(2)
sex_df = pd.get_dummies(df['Sex'], drop_first=True)
Embark_df = pd.get_dummies(df['Embarked'], drop_first = True)
sex_df
Embark_df
df = pd.concat([df, sex_df, Embark_df], axis = 1)
df.drop('Cabin', inplace = True, axis = 1)
logreg = LogisticRegression()
df
df.drop('Sex', inplace = True, axis = 1)
df.drop('Embarked', inplace = True, axis = 1)
df
from sklearn.model_selection import train_test_split
X = df.drop('Survived', axis = 1)
y = df['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
logreg.fit(X_train, y_train)
predictions = logreg.predict(X_test)
predictions
df
print(classification_report(y_test, predictions))
confusion_matrix(y_test, predictions)
sns.heatmap(df, cmap = 'Blues')
df
df.head(2)