# coding=utf-8

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression

train_df = pd.read_csv('./data/train.csv', index_col=False).head(600)
test_df = pd.read_csv('./data/train.csv', index_col=False).tail(291)

def getTargetFeatures(data):
  source = data.loc[:, ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare']]
  dummies_embarked = pd.get_dummies(data['Embarked'], prefix= 'Embarked')
  source['Age'] = source['Age'].fillna(0)
  source['Fare'] = source['Fare'].fillna(0)
  return pd.concat([source, dummies_embarked], axis=1)

X = getTargetFeatures(train_df)
y = train_df['Survived']
testX = getTargetFeatures(test_df)
testY = test_df['Survived']


reg = LogisticRegression()
reg.fit(X, y)

print(np.count_nonzero(reg.predict(X) == y) / float(len(y)))
print(np.count_nonzero(reg.predict(testX) == testY) / float(len(testY)))
# coding=utf-8

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression

train_df = pd.read_csv('./data/train.csv', index_col=False).head(600)
test_df = pd.read_csv('./data/train.csv', index_col=False).tail(291)

def getTargetFeatures(data):
  source = data.loc[:, ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare']]
  dummies_embarked = pd.get_dummies(data['Embarked'], prefix= 'Embarked')
  source['Sex'] = data['Sex'].map(lambda x : 1 if x == 'male' else 0 )
  source['Age'] = source['Age'].fillna(0)
  source['Fare'] = source['Fare'].fillna(0)
  return pd.concat([source, dummies_embarked], axis=1)

X = getTargetFeatures(train_df)
y = train_df['Survived']
testX = getTargetFeatures(test_df)
testY = test_df['Survived']


reg = LogisticRegression()
reg.fit(X, y)

print(np.count_nonzero(reg.predict(X) == y) / float(len(y)))
print(np.count_nonzero(reg.predict(testX) == testY) / float(len(testY)))
add Sex features with 1 and 0