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 the sepals’ and petals’ length and width measurements.

Here, we design a model that makes proper classifications for new flowers. In other words, one that exhibits good generalization.

Contents

  1. Application type.
  2. Data set.
  3. Neural network.
  4. Training strategy.
  5. Testing analysis.
  6. Model deployment.
  7. Tutorial video.

The iris data set contains fifty instances of each of the three species.

This example is solved with the data science and machine learning platform Neural Designer. Use the free trial to follow it step by step.

1. Application type

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.

2. Data set

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:

  • Data source.
  • Variables.
  • Instances.

Data source

The data source is the file iris_flowers.csv. It contains the data for this example in comma-separated values (CSV) format. The number of columns is 5, and the number of rows is 150.

Variables

The variables are:

  • sepal_length: Sepal length, in centimeters, used as input.
  • sepal_width: Sepal width, in centimeters, used as input.
  • petal_length: Petal length, in centimeters, used as input.
  • petal_width: Petal width, in centimeters, used as input.
  • class: Iris Setosa, Versicolor, or Virginica, used as the target.

Note that neural networks work with numbers. In this regard, the categorical variable “class” is transformed into three numerical variables as follows:

  • iris_setosa: 1 0 0.
  • iris_versicolor: 0 1 0.
  • iris_virginica: 0 0 1.

Instances

The instances are randomly split into training, selection, and testing subsets.

They represent 60% (90), 20% (30), and 20% (30) of the original instances, respectively.

Distributions

We can calculate the distributions of all variables. The following 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.

3. Neural network

The second step is to choose a neural network. For classification problems, it is usually composed of:

  • A scaling layer.
  • Two perceptron layers.
  • A probabilistic layer.

The neural network must have four inputs since the data set has four input variables (sepal length, sepal width, petal length, and petal width).

The scaling layer normalizes the input values. All input variables have normal distributions, so we use the mean and standard deviation scaling method.

Here, we use two perceptron layers:

The probabilistic layer allows us to interpret the outputs as probabilities. In this regard, all outputs are between 0 and 1, and their sum is 1. The softmax probabilistic method is used here.

The neural network has three outputs since the target variable contains three classes (Setosa, Versicolor, and Virginica).

The following figure is a graphical representation of this classification neural network:

4. Training strategy

The fourth step is to set the training strategy, which is composed of:

  • Loss index.
  • Optimization algorithm.

The loss index chosen for this application is the normalized squared error with L2 regularization.

The error term fits the neural network to the training instances of the data set. The regularization term makes the model more stable and improves generalization.

The optimization algorithm searches for the neural network parameters that minimize the loss index. The quasi-Newton method is chosen here.

The following chart shows how the training (blue) and selection (orange) errors decrease with the epochs during training.

The final errors are 0.005 NSE for training (blue) and 0.195 NSE for validation (orange).

6. Testing analysis

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.

Confusion matrix

In the confusion matrix, rows are the actual values and columns are the predicted values.

Diagonal cells show correct classifications, while off-diagonal cells show misclassifications.

Predicted setosaPredicted versicolorPredicted virginica
Real setosa10 (33.3%)00
Real versicolor011 (36.7%)0
Real virginica01 (3.33%)8 (26.7%)

Here, we can see that all testing instances are well classified, except for one.

In particular, the neural network has classified one flower as Virginica, which is actually Versicolor.

Note that the confusion matrix depends on the particular testing instances we have.

Classification metrics

The confusion matrix allows us to calculate the model’s accuracy and error:

  • Classification accuracy: 96.67%.
  • Error rate: 3.33%.

7. Model deployment

The neural network is now ready to predict outputs for inputs 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 widths of its sepals and petals. For instance:

Sepal length (cm):
Sepal width (cm):
Petal length (cm):
Petal width (cm):
Probability of Iris Setosa: 21.85%
Probability of Iris Versicolor: 56.72%
Probability of Iris Virginica: 21.42%

8. Video tutorial

Watch the step-by-step video tutorial of this example solved with Neural Designer.

Related posts