# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
**Reading the dataset and cleaning it.**
"""
data = pd.read_csv('/kaggle/input/titanic/train.csv')
data.head()
data.describe()
"""
**Calculating the columns having null values.**

"""
data.isnull().sum()
"""
**Dropping the rows where there are null values**
"""
data = data.dropna(subset = ['Age','Embarked'])
"""
**Since the column cabin has a lot of null values so drop it.**
"""
data=data.drop(columns = ['Cabin'])
data.isnull().sum()
data.dtypes
data.corr()
"""
**Since name and some columns do not have any correlation to survive so they are dropped.**
"""
data = data.drop(['Name','Embarked','Ticket'],axis = 1)
data.head()
"""
**Converting categorical data into numeric**
"""
list1 = ['Sex']
for column in list1:
    data[column] = data[column].astype('category')
data = pd.get_dummies(data = data, columns = list1)
data = data.astype('float64')

data.head()
"""
**Visualisation**
"""
import matplotlib.pyplot as plt
import seaborn as sns
"""
**Visualising the correlation in the form of heatmap**
"""
fig, ax=plt.subplots(figsize=(12,12))
cor=data.corr().round(2)
sns.heatmap(cor,cmap="Set1",linewidth=1.0)
"""
**Count plot for the number of people survived.   0~ Survived   1~Dead**
"""
fig , ax = plt.subplots(figsize=(6,4))
sns.countplot(x=data['Survived'], data=data)
plt.title("Survival Rate")
plt.show()
"""
**Histogram For Age**
"""
data['Age'].hist()
plt.title('Histogram of Age')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.savefig('hist_age')
"""
**Scatter plot between Age and Fare**
"""
fig, ax = plt.subplots()
ax.scatter(data['Age'], data['Fare'])
"""
**Bar Chart been the class and the passenger's age**
"""
fig, ax = plt.subplots()
ax.bar(data['Pclass'], data['Age'])
"""
**After cleaning and Visualisation, heading forward to train the model**
"""
X_train = data.iloc[:,data.columns != 'Survived']
Y_train = data['Survived']
X_train.head()
Y_train.head()
"""
![](http://)**Importing the Logistic Regression model from sklearn and Training the model**
"""
from sklearn.linear_model import LogisticRegression
from sklearn import metrics

logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
"""
**Importing the test dataset and cleaning it like the test one.**
"""
test = pd.read_csv('/kaggle/input/titanic/test.csv')
test.head()
test.isnull().sum()
test = test.dropna(subset = ['Age'])
test=test.drop(['Cabin','Name','Ticket','Embarked'], axis =1)
test.head()
list1 = ['Sex']
for column in list1:
    test[column] = test[column].astype('category')
test = pd.get_dummies(data = test, columns = list1)
test = data.astype('float64')
test.head()
X_test = test.iloc[:,test.columns != 'Survived']
Y_test = test['Survived']
"""
**Predicting the survived column for the test dataset using the Logistic Regression model.**
"""
y_pred = logreg.predict(X_test)
"""
**Accuracy of the Model**
"""
print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, Y_test)))
"""
**Confusion Matrix**
"""
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(Y_test, y_pred)
print(confusion_matrix)
"""
**Classification Report**
"""
from sklearn.metrics import classification_report
print(classification_report(Y_test, y_pred))
"""
**ROC Curve**
"""
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
logit_roc_auc = roc_auc_score(Y_test, logreg.predict(X_test))
fpr, tpr, thresholds = roc_curve(Y_test, logreg.predict_proba(X_test)[:,1])
plt.figure()
plt.plot(fpr, tpr, label='Logistic Regression (area = %0.2f)' % logit_roc_auc)
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.savefig('Log_ROC')
plt.show()