In [1]:
from tensorflow.contrib import learn
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression

In [2]:
df = pd.read_csv('data/titanic_train.csv')

In [3]:
df.shape


Out[3]:
(891, 12)

In [4]:
df.columns


Out[4]:
Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
       'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
      dtype='object')

In [5]:
df.head(1)


Out[5]:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.25 NaN S

In [6]:
y, X = df['Survived'], df[['Age', 'SibSp', 'Fare']].fillna(0)

With scikit-learn


In [7]:
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=42)

In [8]:
lr = LogisticRegression()

In [9]:
lr.fit(X_train, y_train)


Out[9]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0, warm_start=False)

In [10]:
print(accuracy_score(y_test, lr.predict(X_test)))


0.664804469274

With tf.learn


In [11]:
import random
random.seed(42) # to sample data the same way

In [12]:
classifier = learn.TensorFlowLinearClassifier(n_classes=2,
                                              batch_size=128,
                                              steps=500,
                                              learning_rate=0.05)

In [13]:
classifier.fit(X_train, y_train)


Step #100, epoch #16, avg. train loss: 0.68626
Step #200, epoch #33, avg. train loss: 0.63602
Step #300, epoch #50, avg. train loss: 0.63053
Step #400, epoch #66, avg. train loss: 0.62640
Step #500, epoch #83, avg. train loss: 0.62822
Out[13]:
TensorFlowLinearClassifier(batch_size=128, class_weight=None,
              clip_gradients=5.0, config=None, continue_training=False,
              learning_rate=0.05, n_classes=2, optimizer='Adagrad',
              steps=500, verbose=1)

In [14]:
print(accuracy_score(classifier.predict(X_test), y_test))


0.670391061453