text_plain_output_1.png
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score

from sklearn.neighbors import KNeighborsClassifier

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

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

df_train = pd.read_csv('../input/train.csv')

df_train['sex_female'] = df_train['Sex'].apply(lambda x: 1 if x == 'female' else 0)
df_train['age_snr'] = df_train['Age'].apply(lambda x: 1 if x >= 50 else 0)
df_train['age_mid'] = df_train['Age'].apply(lambda x: 1 if x > 10 and x < 50 else 0)
df_train['age_jnr'] = df_train['Age'].apply(lambda x: 1 if x <= 10 else 0)
df_train['known_age'] = df_train['Age'].apply(lambda x: 0 if pd.isnull(x) else 1)
df_train.loc[df_train['known_age'] == 0, 'age_mid'] = 1

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_valid_std = sc.transform(X_valid)

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
lr = LogisticRegression(C=1000.0, random_state=0)
lr.fit(X_train_std, y_train)
y_pred = lr.predict(X_valid_std)

df_test = pd.read_csv('../input/test.csv')

df_test['sex_female'] = df_test['Sex'].apply(lambda x: 1 if x == 'female' else 0)
df_test['age_snr'] = df_test['Age'].apply(lambda x: 1 if x >= 50 else 0)
df_test['age_mid'] = df_test['Age'].apply(lambda x: 1 if x > 10 and x < 50 else 0)
df_test['age_jnr'] = df_test['Age'].apply(lambda x: 1 if x <= 10 else 0)
df_test['known_age'] = df_test['Age'].apply(lambda x: 0 if pd.isnull(x) else 1)
df_test.loc[df_test['known_age'] == 0, 'age_mid'] = 1

test = df_test.loc[:, ['PassengerId', 'Pclass', 'sex_female', 'age_jnr', 'known_age']]
test_std = sc.transform(test)
yy_test = lr.predict(test_std)

df_submission = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': yy_test})
df_submission.to_csv('accountant_titanic_01.csv', index=False)

def classif_func(data_train, label_train, data_valid, label_valid, classif):
    classif.fit(data_train, label_train)
    y_pred = classif.predict(data_valid)
    return ((label_valid != y_pred).sum(), accuracy_score(label_valid, y_pred))

def classif_Kne(data_train, label_train, data_valid, label_valid, k):
    classif = KNeighborsClassifier(k)
    classif.fit(data_train, label_train)
    y_pred = classif.predict(data_valid)
    return ((label_valid != y_pred).sum(), accuracy_score(label_valid, y_pred))

def classif_SVC(data_train, label_train, data_valid, label_valid, k):
    classif = SVC(gamma=2, C=k)
    classif.fit(data_train, label_train)
    y_pred = classif.predict(data_valid)
    return ((label_valid != y_pred).sum(), accuracy_score(label_valid, y_pred))

classif = KNeighborsClassifier(20)
classif.fit(X_train_std, y_train)
yy_test = classif.predict(test_std)
df_submission = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': yy_test})
df_submission.to_csv('accountant_titanic_02.csv', index=False)

classif = SVC(gamma=2, C=3)
classif.fit(X_train_std, y_train)
yy_test = classif.predict(test_std)
df_submission = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': yy_test})
print(df_submission.head())
df_submission.to_csv('accountant_titanic_03.csv', index=False)