Iris Flower Classification Using Machine Learning

1. Introduction
Iris Flower Classification is one of the most popular and beginner-friendly Machine Learning projects. The main objective of this project is to classify an Iris flower into one of three different species based on its physical measurements.
The three Iris flower species included in the dataset are:
Iris Setosa
Iris Versicolor
Iris Virginica
The classification is performed using four physical measurements of the flower:
Sepal Length
Sepal Width
Petal Length
Petal Width
These measurements are provided in centimeters and are used as input features for the Machine Learning model.
This is a Supervised Machine Learning problem because the model is trained using labeled data. Each flower in the training dataset has known measurements and a corresponding species label. The model learns the relationship between the physical measurements and the flower species.
Since the target variable represents a category or class, this is a Classification problem. In this project, Logistic Regression is used as the classification algorithm because it is simple, efficient, and suitable for multi-class classification problems.
The Iris dataset is widely used for learning and demonstrating Machine Learning concepts because it is relatively small, well-structured, and contains three clearly defined classes.
2. Objective
The main objective of this project is to build a Machine Learning model that learns from the Iris dataset and predicts the species of a new flower based on its physical measurements.
The project demonstrates important Machine Learning concepts such as:
Loading a built-in dataset
DataFrame creation
Feature selection
Target variable selection
Classification
Logistic Regression
Model training
Prediction
Converting numerical predictions into class names
3. Dataset
The Iris dataset contains 150 flower samples divided into three species. Each species contains 50 samples.
The four input features are:
Sepal Length
The length of the sepal measured in centimeters.
Sepal Width
The width of the sepal measured in centimeters.
Petal Length
The length of the petal measured in centimeters.
Petal Width
The width of the petal measured in centimeters.
The target variable represents the species of the flower.
The three species are represented numerically in the dataset:
Numeric ValueSpecies0Setosa1Versicolor2Virginica
Sample Dataset
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) Species
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
4. Technologies Used
The following technologies and libraries are used:
Python – Main programming language.
Pandas – Used to create and display the dataset in DataFrame format.
Scikit-learn – Used for the dataset and Machine Learning algorithm.
Iris Dataset – Built-in dataset containing flower measurements.
Logistic Regression – Used to classify the flowers into different species.
5. Working Principle
The system follows a simple classification workflow.
First, the built-in Iris dataset is loaded using Scikit-learn's load_iris() function. The dataset contains the flower measurements and their corresponding target labels.
The data is then converted into a Pandas DataFrame so that it can be easily viewed and analyzed.
The four flower measurements are selected as the input features. These features are stored in the variable X.
The species labels are selected as the target variable and stored in y.
Next, a Logistic Regression model is created. The model is trained using all the available Iris samples. During training, the algorithm learns patterns in the flower measurements that distinguish Setosa, Versicolor, and Virginica.
After training, measurements for a new flower are provided to the model. The model analyzes these measurements and predicts the most likely species.
6. Python Implementation
The following Python program implements the Iris Flower Classification system.
# Import required libraries
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
# --------------------------------------------
# Step 1: Load the Iris dataset
# --------------------------------------------
iris = load_iris()
# Create DataFrame
df = pd.DataFrame(
iris.data,
columns=iris.feature_names
)
# Add target column
df["Species"] = iris.target
# Display dataset
print("Dataset:")
print(df.head())
# --------------------------------------------
# Step 2: Select input features (X)
# --------------------------------------------
X = iris.data
# --------------------------------------------
# Step 3: Select target variable (y)
# --------------------------------------------
y = iris.target
# --------------------------------------------
# Step 4: Create the Logistic Regression model
# --------------------------------------------
model = LogisticRegression(max_iter=200)
# --------------------------------------------
# Step 5: Train the model
# --------------------------------------------
model.fit(X, y)
# --------------------------------------------
# Step 6: Predict flower species
# --------------------------------------------
# Flower measurements:
# Sepal Length = 5.1 cm
# Sepal Width = 3.5 cm
# Petal Length = 1.4 cm
# Petal Width = 0.2 cm
new_flower = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_flower)
# Convert prediction number into species name
species = iris.target_names[prediction[0]]
# --------------------------------------------
# Step 7: Display prediction
# --------------------------------------------
print("\nPredicted Species:")
print(species)
7. Explanation of the Python Program
The program begins by importing the required libraries. Pandas is used for creating and displaying the dataset, while Scikit-learn provides the Iris dataset and Logistic Regression algorithm.
In Step 1, the Iris dataset is loaded using:
iris = load_iris()
The iris.data contains the four flower measurements, while iris.target contains the numerical species labels.
A Pandas DataFrame is created using the feature names provided by the dataset. The target values are then added as a new column called Species.
In Step 2, the four flower measurements are selected as the input features:
X = iris.data
These include sepal length, sepal width, petal length, and petal width.
In Step 3, the target variable is selected:
y = iris.target
The target contains numerical values representing the three species.
In Step 4, a Logistic Regression model is created:
model = LogisticRegression(max_iter=200)
The max_iter parameter allows the algorithm to perform enough iterations to successfully train the model.
In Step 5, the model is trained:
model.fit(X, y)
During training, the model learns the patterns that distinguish the three Iris species.
8. Prediction
A new flower is provided with the following measurements:
Sepal Length = 5.1 cm
Sepal Width = 3.5 cm
Petal Length = 1.4 cm
Petal Width = 0.2 cm
These values are provided to the trained model:
new_flower = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_flower)
The model returns a numerical prediction. This number is converted into the actual species name using:
species = iris.target_names[prediction[0]]
9. Output
Dataset
Dataset:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) Species
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
Prediction
Predicted Species:
setosa
The model predicts that the new flower belongs to the Iris Setosa species.
10. Why the Model Predicts Setosa
The provided flower measurements are:
Sepal Length = 5.1 cm
Sepal Width = 3.5 cm
Petal Length = 1.4 cm
Petal Width = 0.2 cm
These measurements are strongly associated with the Setosa class in the Iris dataset. In particular, the relatively small petal length and petal width are typical characteristics of Iris Setosa.
The trained Logistic Regression model recognizes this pattern and assigns the flower to the Setosa class.
11. Advantages
The Iris Flower Classification project has several advantages:
Simple and beginner-friendly.
Uses a well-known Machine Learning dataset.
Demonstrates supervised classification.
Easy to implement using Python.
Uses multiple input features.
Can classify flowers into multiple categories.
Provides a practical introduction to Logistic Regression.
12. Limitations
The Iris dataset is small and contains only three flower species. Therefore, this model cannot classify flower species outside the three classes present in the dataset.
The dataset is also relatively clean and well structured, unlike many real-world datasets. Real-world classification problems may contain missing values, noisy data, outliers, and imbalanced classes.
Another limitation is that the project trains the model using the complete dataset without separating it into training and testing datasets. Therefore, the example focuses mainly on demonstrating the classification process rather than providing a complete evaluation of model performance.
13. Future Improvements
The project can be improved by dividing the dataset into training and testing sets. This would allow the model to be evaluated using previously unseen data.
Performance metrics such as Accuracy, Precision, Recall, F1-Score, and Confusion Matrix can be used to evaluate the classifier.
Other classification algorithms can also be compared with Logistic Regression, including:
Decision Tree
Random Forest
K-Nearest Neighbors (KNN)
Support Vector Machine (SVM)
Naive Bayes
A larger flower dataset containing more species and additional physical characteristics could also be used to build a more advanced flower classification system.
14. Conclusion
The Iris Flower Classification Using Machine Learning project demonstrates how a supervised classification algorithm can be used to identify flower species based on physical measurements.
The project uses the popular Iris dataset and Logistic Regression to classify flowers into three species: Setosa, Versicolor, and Virginica. The model uses sepal length, sepal width, petal length, and petal width as input features.
For the example flower with measurements of 5.1 cm, 3.5 cm, 1.4 cm, and 0.2 cm, the trained model predicts the species as Setosa.
This project provides a simple and practical introduction to Machine Learning classification, dataset handling, feature selection, model training, and prediction. It can serve as a foundation for understanding more complex classification problems and algorithms.
