Skip to main content

Simple Tensorflow example on iris dataset

This one trains the DNN on iris and also create confusion matrix. Comments will help you know what is happening.

===

#imports

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

# Load data

# Download it from here : https://archive.ics.uci.edu/ml/datasets/iris. Ensure that the categories are #labeled as Iris-setosa, Iris-virginica, Iris-versicolor and their column name as Species.
iris = pd.read_csv("irisdata.csv")

# explore what is loaded
#print(iris.shape)
#print(iris.head)

# Convert the data type of columns to float32 type

#print(iris.dtypes)

#print(iris.iloc[:,0:4])
iris.iloc[:,0:4] = iris.iloc[:,0:4].astype(np.float32)

#print(iris.dtypes)


# encode the classes to numeric values
iris["Species"] = iris["Species"].map({"Iris-setosa":0,"Iris-virginica":1,"Iris-versicolor":2})


# train test split
X_train, X_test, y_train, y_test = train_test_split(iris.iloc[:,0:4], iris["Species"], test_size=0.33, random_state=42)

# get only the column names of input features
columns = iris.columns[0:4]

#print(columns)


# these feature coulmns are made as real valued numbers
feature_columns = [tf.contrib.layers.real_valued_column(k) for k in columns]
feature_columns


# convert the input into format of the tensorflow

#takes in the dataframe and a series/vector and returns

def input_fn(df,labels):
    feature_cols = {k:tf.constant(df[k].values,shape = [df[k].size,1]) for k in columns}
    label = tf.constant(labels.values, shape = [labels.size,1])
    return feature_cols,label

# Define the classifier with hidden layers and number of classes and feature columns
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,hidden_units=[10,20,10],n_classes = 3)


# training the classifier while providing the input which is typecasted by the input function and also mention the iterations
classifier.fit(input_fn=lambda: input_fn(X_train,y_train),steps = 1000)


# evaluate the classifier
ev = classifier.evaluate(input_fn=lambda: input_fn(X_test,y_test),steps=1)


# convert values to the type of tensorflow for prediction only the input and not the label variable
def input_predict(df):
    feature_cols = {k:tf.constant(df[k].values,shape = [df[k].size,1]) for k in columns}
    return feature_cols


# prediction
pred = classifier.predict_classes(input_fn=lambda: input_predict(X_test))


# print prediction
print("The predictions")

#print(list(pred))

#Confusion Matrix
actual = list(y_test)
predicted = list(pred)
actual = pd.Series(actual)
predicted = pd.Series(predicted)
pd.crosstab(actual,predicted)
==
Major part of the code from Wei LI 

Comments

Popular posts from this blog

Option form for polytechnic admissions

Following is option form for colleges in Maharashtra for year 2015 admissions. hope it will help somebody else. colleges have been sorted with utmost care. I have looked at following factor - economic course as students were from economically weaker sections - were open category - wanted less fees - so went for tfws followed by EBC but prefered tfws as ebc is not very sure help many times. total fees is 8000 and with ebc or tfws it will be 2000 only - hostel was most important factor as government college hostels have nominal fees  around 2000 for a year  - good cities where they may have future scope - also near from the parents i.e. parbhani district - considered available modes of transport 1 6013 Government Polytechnic, Pune Pune Electronics and Telecommunication Engg 601337211T 2 6013 Government Polytechnic, Pune Pune Electronics and Telecommunication Engg 601337210 3 6013 Government Polytechnic, Pune P...

ती अशीच त्याला भेटली, अगदी अचानक आलेल्या पावसासारखी

खूप पावसाळे गेले. पण अगदी अलीकडे पर्यंत त्याला पाऊस कधीच आवडलेला नव्हता. पाऊस म्हंटल की त्याला भीती वाटायची, नसती कट कट वाटायची. भिजणे तर त्याला कधीच नको वाटायचे, उगाच सर्दी ला आमंत्रण. पण मागच्या पवसाळ्यात ढग असे आले आणि पाऊस इतका पडला की तो पावसाच्या प्रेमातच पडला. पाऊस आधीही पडायचा, कदाचित असाच, इतकाच किंवा जास्त ही. पण या वेळेस मात्र पाऊस एकटा आला नाही, त्याच्या आयुष्यात सोबत घेऊन आला तिच्या केसांचा ओला सुगंध. ती अशीच त्याला भेटली, अगदी अचानक आलेल्या मुंबईतल्या पावसासारखी, चिंब भिजलेली. त्याला ती आवडली आणि म्हणून पाऊस ही. ती त्याला पुन्हा भेटली, न ठरवून, अशीच पुन्हा एकदा अचानक पावसाराखीच. ते बोलले, थांबले, विसावले. ती समोर आली तेंव्हा पाऊस नव्हताच, ती अजून थोडी जवळ आली तेंव्हा ही पाऊस नव्हता. तो तिच्याकडे सारखा पहातच होता, पण नजर चोरून. आयुष्यात पहिल्यांदा त्याने पावसाकडे येण्याची विनवणी केली. आशेच्या नजरेने त्याने ढगांकडे पाहिले. ढग होते, पण अजून पाऊस मात्र नव्हता. तरी ही आजूबाजूंच्या झाडात, डोंगरात, फुलात आणि पाखरात ओलावा होताच. त्याच्या विनवनीने अखेर तो आला. रिम-झिम, रिम-झिम...