By Gautam Dhall
Iris is flower with different species. In this project we will learn how to predict the flower species using different petal size,sepal size length etc.
In this project we will learn to build our model which will help us in predicting the differnet species of a flower using different petal size,length etc. using K nearest neighbors algorithim(KNN).
So lets get started
First of all we need to install the iris dataset. For it download from the given link below
https://www.kaggle.com/uciml/iris/data#
Save the above file in CSV UTF-8 (Common Delimited)
Now , we need to import our essential libraries
Go to Command Prompt and type
pip install numpy pandas sklearn
Numpy is used to do the matematical operations
Pandas is used to read the data set in an organized manner
sklearn in a Machine Learning library which helps in prediction.
After installing everything we are now ready to start our project. So lets start.
Step1:
Import all the esential libraries.
import numpy as np import pandas as pd from sklearn.neighbors import KNeighborsClassifier
Step 2:
Read the data set using the pandas library.
data = pd.read_csv("datasets_19_420_Iris.csv") data.head()
and you can print the dataset using data.head() in a tabular form.
We need only the first four coloumns for out training set and the last coloumn for our test set.
Step 3:
We now print the data which would require for our training data.
X = data.iloc[:,0:4] X.head()
Similary we can read our y data set which is our test set
y = data.iloc[:,-1] y.head()
Step 4:
Next we find shape of the dataset. The first element shows the number of data examples(number of rows) and the second element show th number of columns
Using the numpy,we find the square root of the first element of the shape. The approx value of the square root will act as 'n' in our KNN.
data.shape np.sqrt(data.shape[0]) knn = KNeighborsClassifier(n_neighbors = 12)
Step 5:
We then use the fit function. Fit measures how well a machine learning model generalizes to similar data to that on which it was trained.
We then use the predict function.We predict our training set.
y_pred = knn.predict(X) y.values#testing set
Step 6:
We create a Data frame where we can store our predicted data in it and print it.
y_prediction = pd.DataFrame(data = [y_pred,y.values]) y_prediction.values
Step 7:
Our model is trained and now we can predict the iris species
a = knn.predict([[1,1,1,1]]) a
The elements in the list are:
SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm |
---|
Now we can give any values and predict the species.
The full code:
import numpy as np import pandas as pd from sklearn.neighbors import KNeighborsClassifier data = pd.read_csv("iris.csv") data.head() X = data.iloc[:,0:4] X.head() y = data.iloc[:-1] y.head() data.shape np.sqrt(data.shape[0]) knn = KNeighborsClassifier(n_neighbors = 12) knn.fit(X,y) y_pred = knn.predict(X) y.values y_prediction = pd.DataFrame(data = [y_pred,y.values]) a = knn.predict([[1,1,1,1]]) a
Cheers!!
Thank you
Submitted by Gautam Dhall (gautamdhall5)
Download packets of source code on Coders Packet
Comments