# 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 20GB 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
"""
This is my first project on Kaggle-- Titanic Disaster.
"""
train1= pd.read_csv("../input/titanic/train.csv")
train1.head()
train1.tail()
train1.shape
train1.drop(columns=['PassengerId'], axis=1, inplace=True)
train1.describe()
print(train1)
test1= pd.read_csv("../input/titanic/test.csv")
test1.head()
test1.tail()
test1.shape
print(train1[["Sex" , "Survived"]].groupby(["Sex"], as_index=False).mean())
#Correlation
corr1= train1.corr()
print(corr1)
import seaborn as sns
sns.heatmap(corr1)
import matplotlib.pyplot as plt
plt.hist(train1.Survived)

plt.show()
plt.hist(train1.Pclass)
plt.show()
print(train1[["Pclass" , "Survived"]].groupby(["Pclass"], as_index=False).mean())
sns.barplot(train1.Cabin.value_counts().index, train1.Cabin.value_counts())
train1['Name'] = train1['Name'].map(lambda x: x.split(',')[1].split('.')[0].strip())
salutation = train1['Name'].unique()
salutation
train1['Age'].fillna(-1, inplace=True)

mean1 = dict()
for i in salutation:
    mean = train1.Age[(train1["Age"] != -1) & (train1['Name'] == i)].mean()
    mean1[i] = mean
    
for index, row in train1.iterrows():
    if row['Age'] == -1:
        train1.loc[index, 'Age'] = mean1[row['Name']]

train1.head()
train1.describe()
from sklearn.preprocessing import StandardScaler

sal_to_num = {
    'Don': 0,
    'Rev': 0,
    'Jonkheer': 0,
    'Capt': 0,
    'Mr': 1,
    'Dr': 2,
    'Col': 3,
    'Major': 3,
    'Master': 4,
    'Miss': 5,
    'Mrs': 6,
    'Mme': 7,
    'Ms': 7,
    'Mlle': 7,
    'Sir': 7,
    'Lady': 7,
    'the Countess': 7
}

train1['Name'] = train1['Name'].apply(lambda x: sal_to_num.get(x))


train1['Name'] = StandardScaler().fit_transform(train1['Name'].values.reshape(-1, 1))
train1['Age'] = StandardScaler().fit_transform(train1['Age'].values.reshape(-1, 1))
train1['Pclass'] = StandardScaler().fit_transform(train1['Pclass'].values.reshape(-1, 1))
train1.drop(columns=['Ticket'], axis=1, inplace=True)
train1.describe()
train1.shape
train1.drop(columns=['Cabin'], axis =1, inplace=True)
train1.info()
from sklearn.preprocessing import LabelEncoder
train1['Sex'] = LabelEncoder().fit_transform(train1['Sex'])
"""
train1.info()
"""
train1.drop(columns=['Embarked'], axis=1, inplace=True)
train1.info()
train1.shape
train1.drop(columns=['Survived'], axis=1, inplace=True)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train1,y, test_size=0.2, random_state=4)
print(X_train)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train,y_train)
predictions = model.predict(X_test)
from sklearn.metrics import classification_report
print(classification_report(y_test, predictions))
score = model.score(X_test, y_test)
print(score)