Classification of iris flowers is perhaps the best-known example of machine learning.
The aim is to classify iris flowers among three species (Setosa, Versicolor, or Virginica) from sepals' and petals' length and width measurements.
The iris data set contains fifty instances of each of the three species.
The central goal is to design a model that makes proper classifications for new flowers. In other words, one which exhibits good generalization.
This is a classification project. Indeed, the variable to be predicted is categorical (setosa, versicolor, or virginica).
The goal is to model class membership probabilities conditioned on the flower features.
The first step is to prepare the data set. This is the source of information for the classification problem. For that, we need to configure the following concepts:
The variables are:
Note that neural networks work with numbers. In this regard, the categorical variable "class" is transformed into three numerical variables as follows:
The instances are divided into training, selection, and testing subsets. They represent 60% (90), 20% (30), and 20% (30) of the original instances, respectively, and are randomly split.
We can calculate the distributions of all variables. The next figure is the pie chart for the iris flower class.
As we can see, the target is well-distributed. Indeed, there is the same number of Virginica, Setosa, and Versicolor samples.
The second step is to choose a neural network. For classification problems it is usually composed by:
The neural network must have four inputs since the data set has four input variables (sepal length, sepal width, petal length, and petal width).
Here we use 2 perceptron layers:
The neural network has three outputs since the target variable contains three classes (Setosa, Versicolor, and Virginica).
The next figure is a graphical representation of this classification neural network:
The fourth step is to set the training strategy, which is composed of:
The following chart shows how the training and selection errors decrease with the epochs during training. The final values are training error = 0.005 NSE (blue), and selection error = 0.195 NSE (orange).
We want to find a neural network with a selection error of less than 0.195 NSE, which is the value we have achieved so far.
Order selection algorithms train several network architectures with a different number of neurons and select that with the smallest selection error.
The incremental order method starts with a small number of neurons and increases the complexity at each iteration. The following chart shows the training error (blue) and the selection error (orange) as a function of the number of neurons.
As we can see, the order that yields the minimum selection error is two. Therefore, we select the neural network with two neurons in the first perceptron layer.
The purpose of the testing analysis is to validate the generalization performance of the model. Here we compare the neural network outputs to the corresponding targets in the testing instances of the data set.
In the confusion matrix, the rows represent the targets (or real values) and the columns the corresponding outputs (or predictive values). The diagonal cells show the correctly classified samples. The off-diagonal cells show the misclassified samples.
|Predicted setosa||Predicted versicolor||Predicted virginica|
|Real setosa||10 (33.3%)||0||0|
|Real versicolor||0||11 (36.7%)||0|
|Real virginica||0||1 (3.33%)||8 (26.7%)|
Here we can see that all testing instances are well classified, but one. In particular, the neural network has classified one flower as Virginica being Versicolor. Note that the confusion matrix depends on the particular testing instances we have.
The confusion matrix allows us to calculate the model's accuracy and error:
The neural network is now ready to predict outputs for inputs that it has never seen. This process is called model deployment.
To classify a given iris flower, we calculate the neural network outputs from the lengths and withs of its sepals and petals. For instance:
For this particular case, the neural network would classify that flower as being of the versicolor species since it has the highest probability.
The mathematical expression of the trained neural network is listed below.
We can implement this expression in any programming language to obtain the output for our input.
Watch the step by step video tutorial of this example solved with Neural Designer.