One of the most well-known case studies in the data science world is Iris Flower Classification. Almost every data science rookie has attempted to tackle this case study at least once.You are provided the measurements related with each species of iris flower, and you must train a machine learning model for the goal of identifying iris blossoms based on this data. This post is for you if you are new to machine learning and have never attempted to tackle this case study. In this article, I will show you how to classify iris flowers using machine learning and Python.
Iris Flower Classification:
There are three species of iris flower: setosa, versicolor, and virginica, which varies in size. Assume you have the measurements of iris blooms classified by species, and your goal is to construct a machine learning model that can learn from the iris species data and categorise them.
I hope you now have a better understanding of the iris flower classification case study. Although the Scikit-learn package has a dataset for iris flower classification, you can get the same dataset from
here for Machine Learning iris flower classification. Now, in the section below, I'll show you how to use machine learning and the Python programming language to categorise iris flower species.
Iris Flower Classification using Python:
I'll begin the work of Iris flower classification by loading the essential Python modules as well as the dataset that we'll need:
1.import matplotlib.pyplot as plt 2.import numpy as np 3.import pandas as pd 4.iris=pd.read_csv("E:\Skin-cancer-project\iris.csv")
let’s have a look at the first and last five rows of this dataset:
5. iris.head(), iris.tail()
Now let’s have a look at the descriptive statistics of this dataset:
6.iris.describe()
The target labels of this dataset are present in the species column, let’s have a quick look at the target labels:
7.print("Target labels",iris["variety"])
8.print("Target labels",iris["variety"].unique()) #unique()
Now let’s plot the data using a scatter plot which will plot the iris species according to the sepal length and sepal width:
9.import plotly.express as px
10.fig = px.scatter(iris, x="sepal.width", y="sepal.length", color="variety")
11.fig.show()
Now let’s train a machine learning model for the task of classifying iris species. Here, I will first split the data into training and test sets, and then I will use the KNN classification algorithm to train the iris classification model:
12. x = iris.drop("variety", axis=1)
13.#print(x)
14.y=iris["variety"]
15.#print(y)
16.from sklearn.model_selection import train_test_split
17.x_train, x_test, y_train, y_test = train_test_split(x, y,test_size=0.2, random_state=0)
18.from sklearn.neighbors import KNeighborsClassifier #KNN algorithm.
19.knn = KNeighborsClassifier(n_neighbors=1)
20.knn.fit(x_train, y_train)
check the model:
21.x_new = np.array([[6.7, 3.0, 5.2, 2.3]])
22.
prediction = knn.predict(x_new)
23.print("Prediction: {}".format(prediction))
0 Comments