in Review, Uncategorized

Introduction to Tensor Flow

Something a little more interesting

Tensorflow provides some high level libraries, which abstract away the task of building package has many such useful abstractions out of the box.

Just for fun lets try out the Kaggle titanic problem, submitting a linear model

import pandas as pd
from tensorflow.contrib import learn, layers
from tensorflow.contrib.metrics import accuracy

titanic_df = pd.read_csv("train.csv" )
titanic_test_df = pd.read_csv("test.csv" )

titanic_df = titanic_df.fillna(titanic_df.mean()['Age'])
titanic_df['Sex'] = (titanic_df['Sex'].apply(lambda x: x=='female')).astype(int)
titanic_test_df['Sex'] = (titanic_test_df['Sex'].apply(lambda x: x=='female')).astype(int)
titanic_df['Embarked'] = (titanic_df['Embarked'].apply(lambda x: x=='Q')).astype(int)
titanic_test_df['Embarked'] = (titanic_test_df['Embarked'].apply(lambda x: x=='Q')).astype(int)

print "=========Train Metrics========="
print titanic_df.head()
print "=========Test Metrics========="
print titanic_test_df.head()

features = ['Pclass',  'Age', 'SibSp', 'Parch', 'Fare', 'Sex', 'Embarked']
y_train, X_train = titanic_df[['Survived']], titanic_df[features]
X_test =  titanic_test_df[features]

classifier = learn.LinearClassifier(n_classes=2, feature_columns=learn.infer_real_valued_columns_from_input(X_train)), y_train, batch_size=128, steps=500)

titanic_test_df.loc[:,'Survived'] =  classifier.predict(X_test)
result =  titanic_test_df[['PassengerId', 'Survived']]

print result.head()
result.to_csv('result.csv', index = False)

The above code gives a score of 0.78 on Kaggle. You might have noticed we didn’t have to define a graph, contrib.learn does the tedious work of defining the graph for us.

Tensorflow integrates seamlessly with tools like jupyter, where you can play with tensorflow in an interactive session.

Write a Comment