TensorFlow Learn Tutorial from https://medium.com/@ilblackdragon/tensorflow-tutorial-part-1-c559c63c0cb1#.9p5m28k6d
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]:
In [4]:
df.columns
Out[4]:
In [5]:
df.head(1)
Out[5]:
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]:
In [10]:
print(accuracy_score(y_test, lr.predict(X_test)))
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)
Out[13]:
In [14]:
print(accuracy_score(classifier.predict(X_test), y_test))