ThunderingWest4's Website

What is the Iris Flower Dataset?

The Iris Flower Dataset, also known as Fisher’s Iris set, was introduced by a British scientist named Ronald Fisher in 1936. The Dataset has 50 different samples from each of three different types of flowers - Iris Setosa, Iris Virginica, and Iris Versicolor. Each sample has the sepal length, sepal width, petal length, and petal width of the flower.

This dataset can have many uses within data analysis and I decided, as one of my first experiments with Neural Networks and Machine Learning, to build a Neural Network capable of classifying the three different species all the way from the ground up. This meant I wasn’t allowed to use any libraries like Numpy or Tensorflow or Keras, even for Linear Algebra resulting in a lot of custom functions designed to calculate the sigmoid, sigmoid derivative, and dot product of lists and numbers. The Network has one input layer with 4 nodes, one hidden layer with 5 nodes, and one output layer with 3 nodes and used the Sigmoid function for the activation of nodes. It took a while to get working, and even now the backpropogation isn’t fully working (only the hidden to output layer weights are being modified) but the network ended up with a relatively high accuracy rate on the test dataset.

The sigmoid function is one of the functions seen most commonly in classifier programs which gives a value between 0 and 1. The equation for the sigmoid function is 1 / (1 + \(e^{-x}\)) where e represents the natural growth rate, also known as Euler’s number, 2.71828.

A Plot of the Sigmoid Graph

One of the main reasons that the sigmoid is used is because it’s only between 0 and 1, making it extremely useful for probability and classification problems. For example, in this network, the program is just calculating the probability of certain inputs corresponding to a specific flower and the output node with the highest value is the network saying that the flower has the highest probability of being that type/breed.

Summary of a Neural Network

A Neural Network is a program designed to mimic how the human brain works. Just think about the process of when you see and identify something - the input layer, or your eyes, takes the information and passes it to the hidden layer, or your brain, which then processes the information and passes the result to the output layer, which is what you identify it as. All the different layers of a Neural Network have “nodes”, which are similar to small cells designed to carry information and each node is connected to all the nodes in the next layer. Neural Networks generally use two different subsections of the overall dataset: + A training set, which is used (as in its name describes) for training the network + A testing set, which is used to test the accuracy of the network after it was trained The Iris Flower dataset has 150 total examples so initially, I used 125 examples for training and 25 to test but later, to get a better idea of the Network’s accuracy over a larger testing set, I used 100 examples for training and 50 for testing which is also what the network is trained/tested on in the embedded code below.

The Network Itself

Now that we’ve briefly gone over what a neural network is and what its dataset is, we can experiment with it! You’ll be able to adjust the learning rate of the network as well as the number of iterations for which it trains and in turn see how that affects its accuracy. The console will show the past simulations that you’ve run to allow you to maximize accuracy based on iterations and learning rates. However, even if you use the exact same iterations and learning rate on two different simulations, it’s incredibly improbable that they will have the same accuracy because the weights (connections between nodes) are randomly generated each time the program is run and then changed from there based on the backpropogation and error calculations.

From Scratch

Note: If you hit “no” to run another simulation but decided that you do, in fact, want to run another simulation, just hit the “run” button at the top of the embedded code box (the little sideways triangle). The “run” button simply runs the program again allowing you to run more simulations.

Sandbox for the Network:

Tensorflow Network

After building my own network from scratch, I decided to build one using Tensorflow to see the difference. Not only was the code several hundred lines shorter, but it was also more accurate than mine.

Sandbox for the tensorflow network: I can’t find a good site that embeds and runs the code. However, I’ll keep looking and update this site as soon as I get a chance. For now, enjoy the code I used! A total of 36 lines, not including blank lines and comments.

from sklearn import datasets
import random
import tensorflow as tf
from tensorflow import keras
import numpy as np

#Thanks to https://machinelearningmastery.com/how-to-choose-loss-functions-when-training-deep-learning-neural-networks/

iris = datasets.load_iris()
irisdat = iris.data
#print(irisdat)
numTypes = 3
#total of 150 different things in the iris dataset
#4 attributes
#first 50 are setosa, second 50 are versicolour, last 50 are virginica
val = []

for i in range(len(irisdat)):

    u = irisdat[i]
    if(i<=50): 
        val.append([u, [1, 0, 0]])
    elif(50 < i and i <= 100):
        val.append([u, [0, 1, 0]])
    elif(100 < i and i <= 150):
        val.append([u, [0, 0, 1]])

random.shuffle(val)

training = val[0:99]
trainX, trainy = np.array([np.array(training[i][0]) for i in range(len(training))]), np.array([np.array(training[i][1]) for i in range(len(training))])
testing = val[100:]
testX, testy = np.array([np.array(testing[i][0]) for i in range(len(testing))]), np.array([np.array(testing[i][1]) for i in range(len(testing))])

iters = 3000
alpha = 0.1

model = keras.Sequential([

    keras.layers.Dense(units=5, activation='sigmoid'), 
    keras.layers.Dense(units=6, activation='sigmoid'),
    keras.layers.Dense(units=3, activation='sigmoid')
])

model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.SGD(lr=alpha, momentum=0.9), metrics=['accuracy'])
print("----------- about to start fitting model ------------")
history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=iters, verbose=0)
print("--------- about to test predictions ------------")
test_eval = model.evaluate(testX, testy, verbose=0)
print("Accuracy on testing set: " + str(test_eval))
print("Accuracy on training set: " + str(model.evaluate(trainX, trainy, verbose=0)))

Additional Notes:

To see the code that I’ve used and embedded in the network sandboxes, check out my github repository for the website here which has all the code I’ve written and compiled currently running on this site. For the code specifically used and embedded, click here. There are two separate folders - one for my custom network, the one I built from scratch, and another for the one built using tensorflow.

 

A Website made and maintained by ThunderingWest4