Created
November 20, 2017 13:46
-
-
Save balzer82/d11af3038a32590ad75b76e5d714d664 to your computer and use it in GitHub Desktop.
Using Tensorflow to predict labels on the Iris Dataset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": true | |
}, | |
"source": [ | |
"## Using Tensorflow to predict Iris Dataset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow.contrib.learn as skflow\n", | |
"import tensorflow as tf\n", | |
"tf.logging.set_verbosity(tf.logging.ERROR)\n", | |
"\n", | |
"from sklearn import datasets, metrics" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"iris = datasets.load_iris()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"feature_columns = [tf.contrib.layers.real_valued_column(\"\", dimension = 4)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"MODEL_PATH='/tmp/tf_examples/iris/'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"classifier = skflow.DNNClassifier(feature_columns=feature_columns,\n", | |
" hidden_units=[10, 20, 10],\n", | |
" n_classes=3,\n", | |
" model_dir=MODEL_PATH)\n", | |
"classifier.fit(x=iris.data, y=iris.target, steps=5000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"y_pred = classifier.predict(iris.data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"score = metrics.accuracy_score(iris.target, list(y_pred))\n", | |
"print(\"Accuracy: %f\" % score)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"`tensorboard --logdir='/tmp/tf_examples/iris/'`" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
}, | |
"latex_metadata": { | |
"affiliation": "ZIGPOS GmbH, Dresden, Germany", | |
"author": "Paul Balzer", | |
"title": "Positioning" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment