0% found this document useful (0 votes)
26 views

Iris Flower Classification Using ML - by Modassir - Medium

This document describes classifying iris flowers into species using machine learning models. Several models including support vector machine, k-nearest neighbors, decision tree, and random forest classifiers are trained on iris data and evaluated on a test set, achieving accuracies from 69.67% to 100%.

Uploaded by

pavan raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Iris Flower Classification Using ML - by Modassir - Medium

This document describes classifying iris flowers into species using machine learning models. Several models including support vector machine, k-nearest neighbors, decision tree, and random forest classifiers are trained on iris data and evaluated on a test set, achieving accuracies from 69.67% to 100%.

Uploaded by

pavan raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Get unlimited access to the best of Medium for less than $1/week. Become a member

Iris Flower Classification Using ML


Modassir · Follow
5 min read · Jan 21, 2024

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 1/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Iris Flower

Overview
https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 2/21
4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Machine learning plays a crucial role in various domains, from healthcare to


finance and beyond. In this article, we’ll delve into a classic example of a
machine-learning application: the Iris Flower Classification. We will explore
the dataset, employ different machine-learning models, and discuss the
insights gained from this fascinating project. This article contains code and
resources for the Iris Flower Classification project. The objective of this
project is to classify iris flowers into distinct species based on their sepal and
petal measurements. The dataset used for training and evaluation is the well-
known Iris dataset, consisting of samples from three iris species: Setosa,
Versicolor, and Virginia.

Data Exploration
Before diving into machine learning models, let’s explore the dataset. We
load the data into a pandas data frame, perform basic statistical analysis, and
visualize the distribution of features. The Seaborn library helps us create
informative visualizations, such as count plots and pair plots, which offer
insights into the relationships between different features. The Iris dataset
includes features such as sepal length, sepal width, petal length, and petal
width. The data is split into training and testing sets for model evaluation.

Importing Necessary Libraries

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 3/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

Load the dataset

# You can also directly load the dataset.


# iris = datasets.load_iris()
# X = iris.data
# y = iris.target

df = pd.read_csv("/content/IRIS.csv")
X = df[['sepal_length','sepal_width','petal_length','petal_width']]
y = df['species']

# 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_

# Standardize the features by removing the mean and scaling to unit variance
scaler = StandardScaler()

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 4/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

df.isnull().sum()

# This will give you the count of each species


y.value_counts()
sns.countplot(x=df['species'],hue=df['species'])

Exploratory Data Analysis (EDA)


The dataset is explored using descriptive statistics and visualizations to
understand the distribution and characteristics of the features.

NULL values are checked to ensure data integrity.

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 5/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Countplot of the Species

Feature Visualization
A pair plot is generated to visualize the relationships between features for
each iris species.

# Visualize the whole dataset


plt.figure(figsize=(8, 6))
Search Write
https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 6/21
4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

sns.pairplot(df, hue='species', height=2)


plt.show()

Visualize the whole dataset

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 7/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Machine Learning Models:


We employ various machine learning models to classify the iris flowers
based on their features. The models used include:

1. Support Vector Machine (SVM): Achieved an accuracy of 69.67%.

2. k-Nearest Neighbors (k-NN): Achieved an accuracy of 100%.

3. Decision Tree Classifier: Achieved an accuracy of 100%.

4. Random Forest Classifier: After hyperparameter tuning, achieved an


accuracy of 100%.

These models are trained and evaluated on a testing set to measure their
accuracy. The scikit-learn library simplifies the implementation of these
models, making it accessible even for those new to machine learning.

Training and Testing

# Support Vector Machine (SVM)


svm_classifier = SVC(kernel='linear', C=1.0, random_state=42)
svm_classifier.fit(X_train, y_train)
y_pred_svm = svm_classifier.predict(X_test)
accuracy_svm = accuracy_score(y_test, y_pred_svm)

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 8/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

# k-Nearest Neighbors (k-NN)


knn_classifier = KNeighborsClassifier(n_neighbors=3)
knn_classifier.fit(X_train, y_train)
y_pred_knn = knn_classifier.predict(X_test)
accuracy_knn = accuracy_score(y_test, y_pred_knn)

# Decision Tree
dt_classifier = DecisionTreeClassifier(random_state=42)
dt_classifier.fit(X_train, y_train)
y_pred_dt = dt_classifier.predict(X_test)
accuracy_dt = accuracy_score(y_test, y_pred_dt)

# Print accuracies
print(f"SVM Accuracy: {accuracy_svm * 100:.2f}%")
print(f"k-NN Accuracy: {accuracy_knn * 100:.2f}%")
print(f"Decision Tree Accuracy: {accuracy_dt * 100:.2f}%")

1. SVM Accuracy: 96.67%

2. k-NN Accuracy: 100.00%

3. Decision Tree Accuracy: 100.00%

Correlation Matrix
A heatmap displays the correlation matrix of the feature set.

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 9/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Heatmap for visualizing correlation

Hyperparameter Tuning

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 10/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

The Random Forest model undergoes hyperparameter tuning using grid


search with cross-validation. The best parameters are identified, and the
model is trained with those parameters.

The best-performing model is the Random Forest Classifier with an accuracy


of 100%.

Accuracy of different Models


We can visualize the accuracy of models used using this code.

# Create a DataFrame with model names and accuracy values


results_df = pd.DataFrame({
'Model': ['SVM', 'k-NN', 'Decision Tree', 'Random Forest'],
'Accuracy': [accuracy_svm, accuracy_knn, accuracy_dt, accuracy_rf]
})

# Plot the bar graph


plt.figure(figsize=(6, 6))
sns.barplot(x='Model', y='Accuracy', data=results_df, palette='viridis')
plt.title('Accuracy of Different Models')
plt.ylim(0, 1) # Set y-axis limit to the range of 0 to 1 (accuracy range)
plt.show()

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 11/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Accuracy of different models

Conclusion
In conclusion, the Iris Flower Classification project provides a hands-on
exploration of machine learning techniques. Through data exploration,
https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 12/21
4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

model training, and visualization, we gain a deeper understanding of the


dataset and the capabilities of various classification models.

This project serves as an excellent starting point for those new to machine
learning, offering practical insights into data preprocessing, model training,
and performance evaluation. Feel free to explore the code, experiment with
different models, and contribute to the project’s growth.

How to Use

How to Use the Code:


The code is available in the associated GitHub repository
https://github.com/Modassirnazar/OIBSIP_1. You can clone the repository,
install the necessary dependencies, and run the provided script to
experience the project firsthand.

By engaging with this project, you’ll not only enhance your understanding of
machine learning but also contribute to a valuable resource for others
exploring this fascinating field.

Happy coding!

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 13/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Modassir

If you find the article helpful, please like and comment on this.

Link to other articles:


6 Months Data Science Roadmap You Should Never Miss

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 14/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

6 Months Data Science Roadmap You Should Never Miss.


Data Science is an interdisciplinary field that combines scientific
methods, algorithms, processes, and systems to…

6 Most In-Demand Skills for Data Analysts in 2024

6 Most In-Demand Skills for Data Analysts in 2024


Data analysts are professionals who gather, clean, interpret, and
study data sets to help resolve organizational…

7 Steps To Get A Data Science Job

7 Steps To Get A Data Science Job


Are you fascinated by the world of data, algorithms, and insights?
Do you dream of diving into the exciting realm of…
medium.com

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 15/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Machine Learning Iris Flower Classification Python Programming

Data Science

Written by Modassir Follow

8 Followers

More from Modassir

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 16/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

Modassir Modassir in AI Mind

Sales Prediction: Leveraging 6 Most In-Demand Skills for Data


Machine Learning to Enhance… Analysts in 2024
Overview Data analysts are professionals who gather,
clean, interpret and study data sets to help…

12 min read · Jan 22, 2024 4 min read · Jul 26, 2023

1 53

Modassir Modassir in AI Mind

Unveiling the Mystery: Predicting 6 Months Data Science Roadmap


Car Prices with Machine Learning you should never Miss.
https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 17/21
4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

“In the realm of data, where car enthusiasts Data Science is an interdisciplinary field that
and algorithms collide, a fascinating journey… combines scientific methods, algorithms,…

5 min read · Jan 22, 2024 6 min read · Jul 30, 2023

100

See all from Modassir

Recommended from Medium

Ramazan Olmez Sandun Dayananda

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 18/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

End-to-End Machine Learning Classification


Project: Churn Prediction Mathematical and machine learning
The main objective of this article is to develop approach
an end-to-end machine learning project. For…

18 min read · Feb 23, 2024 4 min read · Apr 10, 2024

123

Lists

Predictive Modeling w/ Practical Guides to Machine


Python Learning
20 stories · 1134 saves 10 stories · 1359 saves

Natural Language Processing data science and AI


1409 stories · 906 saves 40 stories · 139 saves

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 19/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

RG Data Notes

Fraud Detection using XGBoost Evaluating Multi-Class


and Deep Learning Classification Model using…
Binary classification involves predicting one
of two classes, like ‘Yes’ or ‘No’. Multi-class…

8 min read · Apr 20, 2024 6 min read · Mar 1, 2024

2 3

Srijanie Dey, PhD in Towards Data Science Hugman Sangkeun Jung

Deep Dive into Self-Attention by Classification Overview


Hand✍︎ Explore essential AI classification techniques
Explore the intricacies of the attention and how they solve complex problems,…
mechanism responsible for fueling the…

11 min read · 6 days ago · 12 min read · Apr 18, 2024

433 3

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 20/21


4/29/24, 1:39 PM Iris Flower Classification Using ML | by Modassir | Medium

See more recommendations

Help Status About Careers Blog Privacy Terms Text to speech Teams

https://medium.com/@mn.zamindar/iris-flower-classification-using-ml-29277b18b233#:~:text=Machine Learning Models%3A,Achieved an accuracy of 100%25. 21/21

You might also like