Training My First Neural Network To Classify Different Types of Irises
Training the Network:
Based on 4 data points, sepal length, sepal width, petal length, and petal width I trained a neural network to classify different types of irises. To do that I preprocessed the data to normalize the data points so it was not the size of the data that mattered, but the ratio of sizes between the 4 data points to be able to more accurately identify the irises. The code can be seen below:
# Preprocess the data data_new =  for d in data: data_new.append( #np.array(d) / np.sum(d) np.array(d) / np.sqrt(np.sum(np.square(d))) ) data = np.array(data_new)
After the data was pre-processed I used scikitlearn’s neural network module to train the network to classify irises within the data set I used LBFGS as the optimization method for my network, ReLU for the activation, and found the optimal hidden layer size to be 4.
# Train the neural network classifier. clf = MLPClassifier( solver='lbfgs', # lbfgs is good for small problems. activation='relu', # options: logistic, tanh, relu, identity hidden_layer_sizes=(4), # play with this! tol=1e-8 # default: 1e-4 )
To test if the network was able to generalize to irises that it wasn’t trained on I split the data set into two subsets, training and testing sets:
#training data trData = np.array(data[:100]) trLabels = labels[:100] #testing data tData = data[100:] tLabels = labels[100:]
The training set was composed of the first 100 data points in the set and the test set was composed of the last 50 data points. To ensure that the training set would not train on the first two types of irises and leave out the third I had to shuffle the data before assigning them to their subsets to ensure each iris was represented by the network.
#shuffle data so that irises of all types are in training set credit: https://stackoverflow.com/questions/43229034/randomly-shuffle-data-and-labels-from-different-files-in-the-same-order np.random.seed(3) idx = np.random.permutation(len(data)) data,labels = data[idx], labels[idx] np.random.seed()
the neural network was then trained on the training set and used again to see if it would generalize to the test set that I created. The results were pleasing on the test set I would sometimes receive a score higher than the training set for my last test I received 98% accuracy on the training set and 100% accuracy on the test set.
A graphical display of the neural network can be seen below. the weights and biases are labeled for the connection hidden layers and output:
For the full code visit my git repository at: https://github.com/geopopos/Iris-Classification-Neural-Network