Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Scikit-learn iris data set classification using TensorFlow
import sklearn
import tensorflow as tf
from sklearn import datasets
iris = datasets.load_iris()
# Convert targets to one hot
targets_onehot = [0] * len(
for i,target in enumerate(
targets_onehot[i] = [0] * 3
targets_onehot[i][target] = 1
from sklearn import cross_validation
data_train, data_test, target_train, target_test = cross_validation.train_test_split(, targets_onehot)
print "Training: {}\tTesting: {}".format(len(data_train), len(data_test))
tf_in = tf.placeholder("float", [None, 4]) # Four inputs
# Weight and bias variables
tf_weight = tf.Variable(tf.zeros([4,3]))
tf_bias = tf.Variable(tf.zeros([3]))
# Output
tf_softmax = tf.nn.softmax(tf.matmul(tf_in,tf_weight) + tf_bias)
# Training via backpropagation
tf_softmax_correct = tf.placeholder("float", [None,3])
tf_cross_entropy = -tf.reduce_sum(tf_softmax_correct*tf.log(tf_softmax))
# Train using tf.train.GradientDescentOptimizer
tf_train_step = tf.train.GradientDescentOptimizer(0.01).minimize(tf_cross_entropy)
# Add accuracy checking nodes
tf_correct_prediction = tf.equal(tf.argmax(tf_softmax,1), tf.argmax(tf_softmax_correct,1))
tf_accuracy = tf.reduce_mean(tf.cast(tf_correct_prediction, "float"))
# Initialize and run
init = tf.initialize_all_variables()
sess = tf.Session()
# Run the training
for i in range(10):, feed_dict={tf_in: data_train, tf_softmax_correct: target_train})
# Print accuracy
print "Run {}".format(i)
print, feed_dict={tf_in: data_test, tf_softmax_correct: target_test})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.