UNDERSTANDING MACHINE Dr.
Jitendra Kumar Rout
Dept. of CSE
LEARNING CODES NIT Raipur
IRIS FLOWER CLASSIFICATION
IRIS DATASET
DATA DESCRIPTION
Species(Class
Sl. No. Sepal.Length Sepal.Width Petal Length Petal.Width
Label)
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3 1.4 0.2 setosa
3 7 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
5 6.3 3.3 6 2.5 virginica
6 7.1 3 5.9 2.1 virginica
EXAMPLES OF CLASSIFICATION ALGORITHMS
Decision Tree
Naive Bayes Classifier
K-Nearest Neighbors
Support Vector Machines
Artificial Neural Networks ……….
IRIS CLASSIFICATION
Problem Statement: Create the model that can classify the different species
of the Iris flower.
Steps:
1. Create the dataset.
2. Build the model
3. Train the model
4. Make predictions.
DATASET DESCRIPTION
sklearn comes with the inbuilt datasets for the iris classification problem.
The data set consists of:
150 samples
3 labels: species of Iris (Iris setosa, Iris virginica and Iris versicolor)
4 features: Sepal length, Sepal width, Petal length, Petal Width in cm
PYTHON CODE TO LOAD THE IRIS DATASET
Assign the data and target to separate variables.
x=iris.data
y=iris.target
x contains the features and y contains the labels
SPLITTING THE DATASET
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.5)
x_train contains the training features
x_test contains the testing features
y_train contains the training label
y_test contains the testing labels
BUILD THE MODEL
from sklearn import tree
classifier=tree.DecisionTreeClassifier()
The above code will create the empty model.
If our model has to predict the flower, we have to train the model with the Features and the
Labels.
We can also use KNeighborsClassifier
from sklearn import neighbors
classifier=neighbors.KNeighborsClassifier()
TRAIN THE MODEL
We can train the model with fit function.
classifier.fit(x_train,y_train)
Now the model is ready to make predictions
MAKE PREDICTIONS
Predictions can be done with predict function
y_pred=classifier.predict(x_test)
These predictions can be matched with the expected output to measure the
accuracy value.
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred))
CONFUSION MATRIX
Print the confusion matrix:
from sklearn.metrics import accuracy_score,confusion_matrix
confusion_matrix(y_test,y_pred)
Print the accuracy value
accuracy=accuracy_score(y_test,y_pred)*100
print("Accuracy of the model is {:.2f}".format(accuracy))
COMPLETE CODE
from sklearn import datasets
iris=datasets.load_iris()
x=iris.data
y=iris.target
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.5)
from sklearn import tree
classifier=tree.DecisionTreeClassifier()
classifier.fit(x_train,y_train)
predictions=classifier.predict(x_test)
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,predictions))
REFERENCES
1. Machine learning-Iris classification: https://medium.com/@jebaseelanravi96/machine-
learning-iris-classification-33aa18a4a983
2. https://data-flair.training/blogs/iris-flower-classification/
3. https://www.analyticsvidhya.com/blog/2022/06/iris-flowers-classification-using-machine-
learning/
4. https://www.kaggle.com/code/junyingzhang2018/classification-on-iris-data