TensorFlow Tutorial— Part 1

Illia Polosukhin
Nov 19, 2015 · 3 min read

Why do I care?

Simple model for Titanic dataset

pip install numpy scipy sklearn pandas
# For Ubuntu:
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
# For Mac:
pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
git clone https://github.com/ilblackdragon/tf_examples.git
>>> import pandas
>>> data = pandas.read_csv('data/titanic_train.csv')
>>> data.shape
(891, 12)
>>> data.columns
Index([u'PassengerId', u'Survived', u'Pclass', u'Name', u'Sex', u'Age',
u'SibSp', u'Parch', u'Ticket', u'Fare', u'Cabin', u'Embarked'],
dtype='object')
>>> data[:1]
PassengerId Survived Pclass Name Sex Age SibSp
0 1 0 3 Braund, Mr. Owen Harris male 22 1
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.25 NaN S
>>> y, X = train['Survived'], train[['Age', 'SibSp', 'Fare']].fillna(0)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
>>> lr = LogisticRegression()
>>> lr.fit(X_train, y_train)
>>> print accuracy_score(y_test, lr.predict(X_test))
0.664804469274
>>> from tensorflow.contrib import learn
>>> import random
>>> random.seed(42) # to sample data the same way
>>> classifier = learn.LinearClassifier(n_classes=2,
feature_columns=learn.infer_real_valued_columns_from_input(X_train), optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.05))
>>> classifier.fit(X_train, y_train, batch_size=128, steps=500)
>>> print accuracy_score(classifier.predict(X_test), y_test)
0.68156424581

TF.Learn (previously Scikit Flow)

Coming up…

Illia Polosukhin

Written by

Co-Founder @ NEAR Protocol - leading mobile blockchain revolution. I'm tweeting as @ilblackdragon.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade