0% found this document useful (0 votes)
10 views24 pages

Iris Dataset Classification Using ANN Examplev2

This document describes the implementation of an Artificial Neural Network (ANN) to classify the Iris dataset, which contains samples of Iris flowers with four features and a target variable representing the species. The model consists of an input layer with 4 neurons, a hidden layer with 10 neurons using the 'relu' activation function, and an output layer with 3 neurons using 'softmax'. The document also covers the process of compiling the model with categorical cross-entropy loss, the Adam optimizer, and accuracy as the evaluation metric.

Uploaded by

Marwan Fouad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

Iris Dataset Classification Using ANN Examplev2

This document describes the implementation of an Artificial Neural Network (ANN) to classify the Iris dataset, which contains samples of Iris flowers with four features and a target variable representing the species. The model consists of an input layer with 4 neurons, a hidden layer with 10 neurons using the 'relu' activation function, and an output layer with 3 neurons using 'softmax'. The document also covers the process of compiling the model with categorical cross-entropy loss, the Adam optimizer, and accuracy as the evaluation metric.

Uploaded by

Marwan Fouad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Example: Iris Dataset Classification using ANN

In this example, we will use an ANN to classify the famous Iris dataset. The Iris dataset consists
of 150 samples of Iris flowers, each with 4 features (sepal length, sepal width, petal length, and
petal width) and a target variable (the species of the flower). Our goal is to train an ANN to
accurately classify the species of an Iris flower based on its features.

We define a neural network model with an input layer of 4 neurons, a hidden layer with 10
neurons, and an output layer with 3 neurons, that represent the three species of the flower. We
use the 'relu' activation function for the input and hidden layers, and the 'softmax' activation
function for the output layer. We compile the model using the 'categorical_crossentropy' loss
function, the 'adam' optimizer, and accuracy as the evaluation metric. Finally, we fit the model to
the iris dataset and evaluate its performance.

Version #1:
In this version we did not split dataset into training and testing sets.

Import required libraries: Here we import the necessary libraries to perform the tasks. sklearn is
used to load the iris dataset. keras is used to create the neural network model.

• Keras is a high-level neural networks API, written in Python and capable of running
on top of several lower-level deep learning frameworks such as TensorFlow,
Microsoft Cognitive Toolkit (CNTK), or Theano. It was developed to enable fast
experimentation with deep neural networks, with a focus on enabling both research
and industry use cases.

• Keras provides a user-friendly interface for building various types of deep neural
networks, including convolutional neural networks (CNNs), recurrent neural
networks (RNNs), and combinations of these architectures. It also includes pre-built
models for common use cases such as image classification, text classification, and
language translation.

from [Link] import load_iris


from [Link] import Sequential
from [Link] import Dense
from [Link] import to_categorical

Here we load the iris dataset, which is a popular machine learning dataset that contains
information about iris flowers.

iris = load_iris()

Here we split the dataset into two parts: X (features) and y (target). X contains the four features
of each iris flower: sepal length, sepal width, petal length, and petal width. y contains the target
variable, which is the class of iris flower.
X = [Link]
y = [Link]

print(y)

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2
2 2]

Here we convert the target variable y to one-hot encoded format. One-hot encoding is a
technique used to convert categorical data into a format that can be easily understood by
machine learning algorithms.

y = [Link]
y = to_categorical(y)

print(y)

[[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 1. 0.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]
[0. 0. 1.]]

Here we define the neural network model. Sequential is used to define a linear stack of layers.

This code builds a simple feedforward neural network for multi-class classification. Sequential is
used to define a linear stack of layers. The first layer is a fully connected layer (Dense) with 10
nodes and relu activation function. The second layer is also a fully connected layer with 3 nodes
and softmax activation function, one for each species of Iris flower.

Detailed explaination:

🔷 1. model = Sequential()

You are creating a Sequential model, which means:

• Layers are added one after another in order.

• Data flows straight forward from input → hidden layer → output.


This is the simplest type of neural network architecture.

🔷 2. [Link](Dense(10, input_dim=4, activation='relu'))

This creates the first hidden layer.

✳ What it means:
a) Dense(10)

• A Dense layer means a fully connected layer.

• It has 10 neurons.

• Each neuron connects to all 4 input features.


b) input_dim=4

The model expects 4 input features. For Iris dataset they are sepal length, sepal width, petal
length, petal width

c) activation='relu'

• The activation function for the 10 neurons is ReLU: ReLU (𝑥) = max(0, 𝑥)

• ReLU helps the model learn nonlinear patterns.


✔ This layer transforms the 4 input features into 10 learned features.

🔷 3. [Link](Dense(3, activation='softmax'))

This creates the output layer.

✳ Explanation of arguments:

a) Dense(3)

• The layer has 3 output neurons.

• This means you are doing 3-class classification.

• Each neuron represents one class.


b) activation='softmax'

Softmax converts raw scores into probabilities that sum to 1. For Iris dataset:

There are 3 classes (species):

• setosa

• versicolor

• virginica
Example Softmax Output:

[0.10, 0.70, 0.20]

Meaning:

setosa = 10%

versicolor = 70%
virginica = 20%

model = Sequential()
[Link](Dense(10, input_dim=4, activation='relu'))
[Link](Dense(3, activation='softmax'))

C:\Users\pc\AppData\Roaming\Python\Python312\site-packages\keras\src\
layers\core\[Link]: UserWarning: Do not pass an
`input_shape`/`input_dim` argument to a layer. When using Sequential
models, prefer using an `Input(shape)` object as the first layer in
the model instead.
super().__init__(activity_regularizer=activity_regularizer,
**kwargs)

Here we compile the model by specifying the loss function (categorical_crossentropy), optimizer
(adam), and metrics (accuracy).

Detailed Explaination:

🔷 1. loss='categorical_crossentropy'

This sets the loss function to categorical cross-entropy, which is used when:

• You have multi-class classification

• Classes are mutually exclusive (only one correct class)

• The labels are one-hot encoded:

[1, 0, 0] → setosa

[0, 1, 0] → versicolor

[0, 0, 1] → virginica
Categorical cross-entropy measures how far the predicted probabilities are from the true one-
hot label.

🔷 2. optimizer='adam'

Adam is an optimization algorithm that updates the model weights.

Why Adam?

• Automatically adjusts learning rate

• Fast convergence

• Works well in most deep learning problems

• More efficient than basic gradient descent


Simplified idea: Adam tries to find the weights that minimize the loss with intelligent, adaptive
updates.

📌 Note: Other Optimizers You Can Use (Besides Adam)

While Adam is one of the most popular optimizers because it adapts the learning rate and
converges quickly, several other optimizers are also commonly used in training neural networks:

• (SGD (Stochastic Gradient Descent) The simplest optimizer; updates weights


using a fixed learning rate. Often used with momentum to speed up training.

• RMSprop Adjusts the learning rate for each parameter individually. Works well for
recurrent neural networks and noisy problems.

• Adagrad Adapts the learning rate based on how frequently a parameter is updated.
Good for sparse data.

• Adadelta A refinement of Adagrad that keeps updates more stable by restricting the
growth of learning rate adjustments.

• Adamax A variant of Adam that uses the infinity norm; more stable in some cases.

• Nadam Combines Adam with Nesterov momentum, often improving convergence


slightly.
Each optimizer has its own strengths, and the best choice depends on the dataset and the
problem you are trying to solve.

🔷 3. metrics=['accuracy']

This tells Keras what performance metric to track during training and testing.

• Accuracy = % of predictions that are correct

• Keras will print accuracy for each epoch

• Helps you see if the model is improving


📌 Note: Other Performance Metrics (Besides Accuracy)

Although accuracy is the most commonly used evaluation metric, it is not always the best—
especially when classes are imbalanced. Several other metrics provide deeper insight into how
well a model is performing:

Precision, Recall (Sensitivity), F1-Score, AUC-ROC (Area Under the ROC Curve), Loss Value, ...

These metrics help you understand where the model is making mistakes, not just how often it's
correct.

[Link](loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
Here we train the model on the iris dataset by fitting the model on features X and target y. The
epochs parameter specifies the number of times the model will iterate over the entire training
dataset during the training process. The batch_size parameter determines the number of
samples that will be used in each iteration. We run the model for 100 epochs and a batch size of
10.

During training, the model will make predictions based on the input features and compare them
to the target labels. The difference between the predicted and actual values is the loss, and the
goal of the training process is to minimize this loss. The optimizer algorithm specified in the
[Link]() method will adjust the weights and biases of the model to minimize the loss.

After the training process is complete, the model will have learned a mapping from the input
features to the target labels, and can be used to make predictions on new, unseen data.

[Link](X, y, epochs=100, batch_size=10)

Epoch 1/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.3903 - loss:
2.1343
Epoch 2/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.3575 - loss:
1.9547
Epoch 3/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 992us/step - accuracy: 0.3308 - loss:
1.5466
Epoch 4/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.5108 - loss:
1.4007
Epoch 5/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.6731 - loss:
1.1970
Epoch 6/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.6115 - loss:
1.22310
Epoch 7/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 894us/step - accuracy: 0.6786 - loss:
1.0897
Epoch 8/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.5054 - loss:
1.0599
Epoch 9/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.3590 - loss:
1.0434
Epoch 10/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.3814 - loss:
0.9916
Epoch 11/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 939us/step - accuracy: 0.4946 - loss:
0.9373
Epoch 12/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 895us/step - accuracy: 0.5145 - loss:
0.9338
Epoch 13/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 796us/step - accuracy: 0.6240 - loss:
0.8935
Epoch 14/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.6073 - loss:
0.8734
Epoch 15/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 949us/step - accuracy: 0.7030 - loss:
0.8283
Epoch 16/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 966us/step - accuracy: 0.6055 - loss:
0.8446
Epoch 17/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7359 - loss:
0.7830
Epoch 18/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 860us/step - accuracy: 0.6937 - loss:
0.7812
Epoch 19/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 823us/step - accuracy: 0.6943 - loss:
0.7405
Epoch 20/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7368 - loss:
0.7235
Epoch 21/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 974us/step - accuracy: 0.6887 - loss:
0.7186
Epoch 22/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 938us/step - accuracy: 0.7573 - loss:
0.7047
Epoch 23/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7132 - loss:
0.6761
Epoch 24/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7073 - loss:
0.6661
Epoch 25/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 861us/step - accuracy: 0.7535 - loss:
0.6314
Epoch 26/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7136 - loss:
0.6651
Epoch 27/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7300 - loss:
0.6559
Epoch 28/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 917us/step - accuracy: 0.7310 - loss:
0.6481
Epoch 29/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7027 - loss:
0.6071
Epoch 30/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7743 - loss:
0.5702
Epoch 31/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7739 - loss:
0.5939
Epoch 32/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 930us/step - accuracy: 0.7977 - loss:
0.5747
Epoch 33/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 867us/step - accuracy: 0.7882 - loss:
0.5684
Epoch 34/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 898us/step - accuracy: 0.7834 - loss:
0.5579
Epoch 35/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 970us/step - accuracy: 0.7965 - loss:
0.5824
Epoch 36/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 933us/step - accuracy: 0.8333 - loss:
0.5403
Epoch 37/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7768 - loss:
0.5310
Epoch 38/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 938us/step - accuracy: 0.8023 - loss:
0.5369
Epoch 39/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.8206 - loss:
0.4958
Epoch 40/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 732us/step - accuracy: 0.8401 - loss:
0.5056
Epoch 41/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.8498 - loss:
0.4879
Epoch 42/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 718us/step - accuracy: 0.8186 - loss:
0.4973
Epoch 43/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 860us/step - accuracy: 0.7852 - loss:
0.5320
Epoch 44/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 644us/step - accuracy: 0.8579 - loss:
0.4899
Epoch 45/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.7760 - loss:
0.4954
Epoch 46/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9019 - loss:
0.5110
Epoch 47/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 931us/step - accuracy: 0.8208 - loss:
0.4842
Epoch 48/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 792us/step - accuracy: 0.8593 - loss:
0.4321
Epoch 49/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 900us/step - accuracy: 0.8258 - loss:
0.4941
Epoch 50/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 809us/step - accuracy: 0.8458 - loss:
0.4671
Epoch 51/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.8492 - loss:
0.4491
Epoch 52/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 797us/step - accuracy: 0.8877 - loss:
0.4582
Epoch 53/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 776us/step - accuracy: 0.8916 - loss:
0.4595
Epoch 54/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 863us/step - accuracy: 0.8511 - loss:
0.4854
Epoch 55/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 754us/step - accuracy: 0.9031 - loss:
0.4096
Epoch 56/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 923us/step - accuracy: 0.8899 - loss:
0.4207
Epoch 57/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 715us/step - accuracy: 0.8628 - loss:
0.4408
Epoch 58/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9117 - loss:
0.4447
Epoch 59/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.8706 - loss:
0.4353
Epoch 60/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 788us/step - accuracy: 0.9150 - loss:
0.4600
Epoch 61/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 649us/step - accuracy: 0.8618 - loss:
0.3987
Epoch 62/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 907us/step - accuracy: 0.9087 - loss:
0.4384
Epoch 63/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 718us/step - accuracy: 0.9008 - loss:
0.4298
Epoch 64/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 979us/step - accuracy: 0.8925 - loss:
0.4241
Epoch 65/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9260 - loss:
0.4215
Epoch 66/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 717us/step - accuracy: 0.9016 - loss:
0.4231
Epoch 67/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 799us/step - accuracy: 0.9087 - loss:
0.4142
Epoch 68/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9143 - loss:
0.3967
Epoch 69/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 809us/step - accuracy: 0.9206 - loss:
0.4170
Epoch 70/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 950us/step - accuracy: 0.9234 - loss:
0.4064
Epoch 71/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9049 - loss:
0.4437
Epoch 72/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 838us/step - accuracy: 0.9221 - loss:
0.4279
Epoch 73/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9211 - loss:
0.3940
Epoch 74/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 681us/step - accuracy: 0.9108 - loss:
0.4497
Epoch 75/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9268 - loss:
0.3952
Epoch 76/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9144 - loss:
0.3811
Epoch 77/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 930us/step - accuracy: 0.8979 - loss:
0.3991
Epoch 78/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 792us/step - accuracy: 0.8871 - loss:
0.4067
Epoch 79/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 747us/step - accuracy: 0.9498 - loss:
0.3657
Epoch 80/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 655us/step - accuracy: 0.9369 - loss:
0.3972
Epoch 81/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 789us/step - accuracy: 0.9391 - loss:
0.3894
Epoch 82/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 865us/step - accuracy: 0.9080 - loss:
0.3676
Epoch 83/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 868us/step - accuracy: 0.9270 - loss:
0.4064
Epoch 84/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 680us/step - accuracy: 0.9565 - loss:
0.3828
Epoch 85/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9479 - loss:
0.3771
Epoch 86/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 792us/step - accuracy: 0.9438 - loss:
0.3757
Epoch 87/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 823us/step - accuracy: 0.9164 - loss:
0.3701
Epoch 88/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 716us/step - accuracy: 0.9296 - loss:
0.3510
Epoch 89/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9265 - loss:
0.3655
Epoch 90/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9102 - loss:
0.3934
Epoch 91/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9031 - loss:
0.3908
Epoch 92/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 953us/step - accuracy: 0.9366 - loss:
0.3747
Epoch 93/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 864us/step - accuracy: 0.8947 - loss:
0.3951
Epoch 94/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 966us/step - accuracy: 0.9320 - loss:
0.3443
Epoch 95/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 858us/step - accuracy: 0.9280 - loss:
0.3930
Epoch 96/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 894us/step - accuracy: 0.9407 - loss:
0.3612
Epoch 97/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 643us/step - accuracy: 0.9597 - loss:
0.3595
Epoch 98/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 822us/step - accuracy: 0.9103 - loss:
0.3935
Epoch 99/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9283 - loss:
0.37757
Epoch 100/100
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 859us/step - accuracy: 0.9477 - loss:
0.3353

<[Link] at 0x25c98e1f4a0>

Note: You can set the verbose parameter to 0 to avoid printing the result of each epoch. The
default value for verbose is 1, which prints the progress bar and the metric values for each
epoch. Setting verbose to 0 will turn off this output, while setting it to 2 will print a summary line
for each epoch.

[Link](X, y, epochs=100, batch_size=10, verbose=2)

Epoch 1/100
15/15 - 0s - 2ms/step - accuracy: 0.9400 - loss: 0.3556
Epoch 2/100
15/15 - 0s - 2ms/step - accuracy: 0.9400 - loss: 0.3569
Epoch 3/100
15/15 - 0s - 2ms/step - accuracy: 0.9400 - loss: 0.3521
Epoch 4/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3502
Epoch 5/100
15/15 - 0s - 2ms/step - accuracy: 0.9333 - loss: 0.3491
Epoch 6/100
15/15 - 0s - 2ms/step - accuracy: 0.9333 - loss: 0.3489
Epoch 7/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3449
Epoch 8/100
15/15 - 0s - 2ms/step - accuracy: 0.9400 - loss: 0.3443
Epoch 9/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3433
Epoch 10/100
15/15 - 0s - 3ms/step - accuracy: 0.9467 - loss: 0.3400
Epoch 11/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3383
Epoch 12/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3364
Epoch 13/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3347
Epoch 14/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3352
Epoch 15/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3319
Epoch 16/100
15/15 - 0s - 2ms/step - accuracy: 0.9400 - loss: 0.3299
Epoch 17/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3283
Epoch 18/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3276
Epoch 19/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3249
Epoch 20/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3234
Epoch 21/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3232
Epoch 22/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3210
Epoch 23/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3193
Epoch 24/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3184
Epoch 25/100
15/15 - 0s - 2ms/step - accuracy: 0.9467 - loss: 0.3154
Epoch 26/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3138
Epoch 27/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3117
Epoch 28/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.3116
Epoch 29/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3085
Epoch 30/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3073
Epoch 31/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3051
Epoch 32/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3045
Epoch 33/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.3022
Epoch 34/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2998
Epoch 35/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.2989
Epoch 36/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2988
Epoch 37/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2954
Epoch 38/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2938
Epoch 39/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2930
Epoch 40/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2905
Epoch 41/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2891
Epoch 42/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2877
Epoch 43/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.2867
Epoch 44/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2853
Epoch 45/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2827
Epoch 46/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2815
Epoch 47/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2799
Epoch 48/100
15/15 - 0s - 2ms/step - accuracy: 0.9533 - loss: 0.2787
Epoch 49/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2765
Epoch 50/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2746
Epoch 51/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2734
Epoch 52/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2717
Epoch 53/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2704
Epoch 54/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2694
Epoch 55/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2674
Epoch 56/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2660
Epoch 57/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2647
Epoch 58/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2661
Epoch 59/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2615
Epoch 60/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2602
Epoch 61/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2588
Epoch 62/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2597
Epoch 63/100
15/15 - 0s - 3ms/step - accuracy: 0.9600 - loss: 0.2554
Epoch 64/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2544
Epoch 65/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2547
Epoch 66/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2506
Epoch 67/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2507
Epoch 68/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2498
Epoch 69/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2464
Epoch 70/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2461
Epoch 71/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2435
Epoch 72/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2432
Epoch 73/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2409
Epoch 74/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2401
Epoch 75/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2380
Epoch 76/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2372
Epoch 77/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2354
Epoch 78/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2368
Epoch 79/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2324
Epoch 80/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2311
Epoch 81/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2297
Epoch 82/100
15/15 - 0s - 2ms/step - accuracy: 0.9600 - loss: 0.2286
Epoch 83/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2272
Epoch 84/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2259
Epoch 85/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2244
Epoch 86/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2231
Epoch 87/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2222
Epoch 88/100
15/15 - 0s - 2ms/step - accuracy: 0.9800 - loss: 0.2207
Epoch 89/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2190
Epoch 90/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2185
Epoch 91/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2184
Epoch 92/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2154
Epoch 93/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2138
Epoch 94/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2142
Epoch 95/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2109
Epoch 96/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2132
Epoch 97/100
15/15 - 0s - 2ms/step - accuracy: 0.9733 - loss: 0.2089
Epoch 98/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2083
Epoch 99/100
15/15 - 0s - 2ms/step - accuracy: 0.9800 - loss: 0.2070
Epoch 100/100
15/15 - 0s - 2ms/step - accuracy: 0.9667 - loss: 0.2060

<[Link] at 0x25c98c98770>

Here we evaluate the model on the same dataset we trained it on. We print out the accuracy of
the model.

Explanation

1. [Link](X, y) This function tests the model on the dataset (X, y) and returns a list
of metric values.
• The first value is usually loss.

• The following values are the metrics you defined when compiling the model (e.g.,
accuracy).
Example returned list: scores = [0.45, 0.88]

Here:

• scores[0] = loss

• scores[1] = accuracy

1. model.metrics_names[1] This gives the name of the second metric, usually


"accuracy".

2. scores[1] * 100 Converts accuracy from decimal (e.g., 0.88) to percentage (88%).

scores = [Link](X, y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

5/5 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9820 - loss: 0.1384

compile_metrics: 97.33%

Prediction example: In this example, we assume you have a new sample with the features [5.1,
3.5, 1.4, 0.2]. We create a NumPy array to represent this sample, and then use the [Link]
method to make a prediction for the sample. The output will be a one-hot encoded array
representing the predicted class probabilities for the three iris species.

We use the [Link] function to find the index of the highest predicted probability, which
corresponds to the predicted class label. Finally, we use the iris.target_names array to get the
corresponding class name (setosa, versicolor, or virginica).

import numpy as np

# Assume you have a new sample with the following features


new_sample = [Link]([[5.1, 3.5, 1.4, 0.2]])

# Make a prediction for the new sample


prediction = [Link](new_sample)
print(prediction)
# The output will be a one-hot encoded array
# representing the predicted class probabilities
# in the order [setosa, versicolor, virginica]
print("\nPredicated class probabilities:",prediction)

# To get the predicted class label, you can use [Link]


predicted_class = [Link](prediction)
print("\nPredicted class label: ",predicted_class)

# You can also get the class name from the original iris dataset
class_names = iris.target_names
print("\nPredicated class name: ", class_names[predicted_class])
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step
[[9.9783784e-01 2.1554106e-03 6.7935703e-06]]

Predicated class probabilities: [[9.9783784e-01 2.1554106e-03


6.7935703e-06]]

Predicted class label: 0

Predicated class name: setosa

Version #2:
In this version we split the dataset into training and testing sets.

from [Link] import load_iris


from sklearn.model_selection import train_test_split
from [Link] import Sequential
from [Link] import Dense
from [Link] import to_categorical
import [Link] as plt
import numpy as np

# Load the iris dataset


iris = load_iris()

# Split the dataset into features and target


X = [Link]
y = [Link]

# One-hot encode the target variable


y = to_categorical(y)

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Define the model


model = Sequential()
[Link](Dense(10, input_dim=4, activation='relu'))
[Link](Dense(3, activation='softmax'))

# Compile the model


[Link](loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Fit the model


history = [Link](X_train, y_train, epochs=100, batch_size=10,
validation_data=(X_test, y_test), verbose=0)

# Evaluate the model


scores = [Link](X_test, y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

# Use the trained model for prediction


# Assume you have a new sample with the following features
new_sample = [Link]([[5.1, 3.5, 1.4, 0.2]])

# Make a prediction for the new sample


prediction = [Link](new_sample)

# The output will be a one-hot encoded array


# representing the predicted class probabilities
# in the order [setosa, versicolor, virginica]
print("\nPredicated class probabilities:",prediction)

# To get the predicted class label, you can use [Link]


predicted_class = [Link](prediction)
print("\nPredicted class label: ",predicted_class)

# You can also get the class name from the original iris dataset
class_names = iris.target_names
print("\nPredicated class name: ", class_names[predicted_class])

# Plot the accuracy over epochs


[Link]([Link]['accuracy'])
[Link]([Link]['val_accuracy'])
[Link]('Model Accuracy')
[Link]('Accuracy')
[Link]('Epoch')
[Link](['Train', 'Test'], loc='upper left')
[Link]()

# Plot the loss over epochs


[Link]([Link]['loss'])
[Link]([Link]['val_loss'])
[Link]('Model Loss')
[Link]('Loss')
[Link]('Epoch')
[Link](['Train', 'Test'], loc='upper left')
[Link]()

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 82ms/step - accuracy: 1.0000 - loss:


0.2048

compile_metrics: 100.00%
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 28ms/step

Predicated class probabilities: [[9.8259705e-01 1.7333739e-02


6.9191512e-05]]
Predicted class label: 0

Predicated class name: setosa


Notes:

• val_accuracy and val_loss are calculated, not given. They are computed automatically by
Keras during training because you provided:

validation_data=(X_test, y_test)

When you run:

history = [Link](..., validation_data=(X_test, y_test), ...)

Keras does the following each epoch:

1 Trains on the training data


1️⃣

• Calculates loss → stored as [Link]['loss']

• Calculates accuracy → stored as [Link]['accuracy']


2️⃣Evaluates on the validation data (X_test, y_test)

• Calculates validation loss → stored as [Link]['val_loss']

• Calculates validation accuracy → stored as [Link]['val_accuracy']

You might also like