"""
Understanding the data Survival : 0 = No, 1 = Yes

Pclass : A proxy for socio-economic status (SES) 1st = Upper 2nd = Middle 3rd = Lower

sibsp : The # of siblings / spouses aboard the Titanic Sibling = brother, sister, stepbrother, stepsister Spouse =

husband, wife (mistresses and fiancés were ignored)

parch : The # of parents / children aboard the Titanic Parent = mother, father Child = daughter, son, stepdaughter, stepson Some children travelled only with a nanny, therefore parch=0 for them.

Ticket : Ticket number

Fare : Passenger fare

Cabin : Cabin number embarked

Port of Embarkation: C = Cherbourg, Q = Queenstown, S = Southampton

Name, Sex , Age are self-explanatory
"""
import pandas as pd
import numpy as np
import seaborn as sns 
import matplotlib.pyplot as plt

 
train_data = pd.read_csv('../input/titanic/train.csv')
test_data = pd.read_csv('../input/titanic/test.csv')

train_data.head()
test_data.head()
train_data.info()
test_data.info()

print("training data ", train_data.shape)
print("test data ", test_data.shape)
sns.countplot('Survived', data = train_data)
plt.show()
sns.countplot('Survived', hue = 'Sex', data = train_data)
plt.plot()
sns.countplot('Survived', hue = 'Embarked', data = train_data)
plt.plot()
sns.countplot(x='Survived', hue = 'Embarked', data = train_data)
plt.plot()
((train_data.groupby(['Sex','Survived']).Survived.count() * 100) / train_data.groupby('Sex').Survived.count())
(train_data.groupby(['Pclass','Survived']).Survived.count() * 100) / train_data.groupby('Pclass').Survived.count()
(train_data.groupby(['Embarked','Survived']).Survived.count() * 100) / train_data.groupby('Embarked').Survived.count()
train_data.isnull().sum()
test_data.isnull().sum()
train_data.drop('Cabin', axis = 1, inplace = True)
test_data.drop('Cabin', axis = 1, inplace = True)
columns_to_drop = ['PassengerId','Ticket']
train_data.drop(columns_to_drop, axis = 1, inplace = True)
test_data.drop(columns_to_drop[1], axis = 1, inplace = True)
train_data['Age'].fillna(train_data['Age'].mean(), inplace = True)
test_data['Age'].fillna(test_data['Age'].mean(), inplace = True)
train_data['Embarked'].fillna('S', inplace = True)
test_data['Fare'].fillna(test_data['Fare'].mean(), inplace = True)
train_data.isnull().sum()
test_data.isnull().sum()
gender = {'male': 0,'female': 1}
Embark={'S': 0,'C':1,'Q':2}
train_data.Embarked =[Embark[item] for item in train_data.Embarked ]
train_data.Sex = [gender[item] for item in train_data.Sex]
test_data.Embarked =[Embark[item] for item in test_data.Embarked ]
test_data.Sex = [gender[item] for item in test_data.Sex]
train_data.drop('Name', axis = 1, inplace = True)
test_data.drop('Name', axis = 1, inplace = True)
from sklearn.preprocessing import StandardScaler
ss= StandardScaler()
features= ['Age', 'Fare']

train_data[features]= ss.fit_transform(train_data[features])
test_data[features]= ss.fit_transform(test_data[features])
train_data.head()
X_train = train_data.drop("Survived", axis=1)
Y_train = train_data["Survived"]
X_test = test_data.drop("PassengerId", axis = 1)
print("shape of X_train",X_train.shape)
print("Shape of Y_train",Y_train.shape)
print("Shape of x_test",X_test.shape)
from sklearn.linear_model import LogisticRegression

clf= LogisticRegression()

clf.fit(X_train, Y_train.ravel())

predictions= clf.predict(X_test)
print(clf.score(X_train, Y_train))
submission= pd.DataFrame({'PassengerId' : test_data['PassengerId'], 'Survived': predictions })
submission.head()