0% found this document useful (0 votes)
10 views5 pages

Week 11 KNN

The document outlines a program to implement the k-Nearest Neighbour algorithm for classifying the Iris dataset using Python's sklearn library. It includes details on the training and classification algorithms, as well as code to train the model, make predictions, and evaluate performance using a confusion matrix and accuracy metrics. The output demonstrates the model's predictions and performance metrics, including precision, recall, and F1-score.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Week 11 KNN

The document outlines a program to implement the k-Nearest Neighbour algorithm for classifying the Iris dataset using Python's sklearn library. It includes details on the training and classification algorithms, as well as code to train the model, make predictions, and evaluate performance using a confusion matrix and accuracy metrics. The output demonstrates the model's predictions and performance metrics, including precision, recall, and F1-score.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Machine Learning Laboratory

Week 11.Write a program to implement k-Nearest Neighbour


algorithm to classify the iris data set. Print both correct and
wrong predictions. Java/Python ML library classes can be used
for this problem.

K-Nearest Neighbor Algorithm

Training algorithm:
 For each training example (x, f (x)), add the example to the list training
examples Classification algorithm:
 Given a query instance xq to be classified,
 Let x1 . . .xk denote the k instances from training examples that are nearest to xq
 Return

 Where, f(xi) function to calculate the mean value of the k nearest training
examples.

Data Set:

Iris Plants Dataset: Dataset contains 150 instances (50 in each of three
classes) Number of Attributes: 4 numeric, predictive attributes and the Class
Machine Learning Laboratory

Program:

from sklearn.model_selection import train_test_split


from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn import datasets
iris=datasets.load_iris()

x = iris.data
y = iris.target

print ('sepal-length', 'sepal-width', 'petal-length', 'petal-width')


print(x)

print('class: 0-Iris-Setosa, 1- Iris-Versicolour, 2- Iris-Virginica')


print(y)

x_train, x_test, y_train, y_test =


train_test_split(x,y,test_size=0.3)

classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(x_train, y_train)

y_pred=classifier.predict(x_test)

print('Confusion Matrix')
print(confusion_matrix(y_test,y_pred))
print('Accuracy Metrics')
print(classification_report(y_test,y_pred))
Machine Learning Laboratory

Output:

sepal-length sepal-width petal-length petal-width


[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
. . . . .
. . . . .

[6.2 3.4 5.4 2.3]


[5.9 3. 5.1 1.8]]

class: 0-Iris-Setosa, 1- Iris-Versicolour, 2- Iris-Virginica


[0 0 0 ………0 0 1 1 1 …………1 1 2 2 2 ………… 2 2] Confusion

Matrix
[[20 0 0]
[ 0 10 0]
[ 0 1 14]]

Accuracy Metrics

Precision recall f1-score support

0 1.00 1.00 1.00 20


1 0.91 1.00 0.95 10
2 1.00 0.93 0.97 15

avg / total 0.98 0.98 0.98 45


Machine Learning Laboratory

Basic knowledge

Confusion Matrix

True positives: data points labelled as positive that are actually positive
False positives: data points labelled as positive that are actually negative
True negatives: data points labelled as negative that are actually negative
False negatives: data points labelled as negative that are actually positive

Accuracy: how often is the classifier correct?

F1-Score:

Support: Total Predicted of Class.


Support = TP + FN
Machine Learning Laboratory

Example:

 Support _ A = TP_A + FN_A


= 30 + (20 + 10)
= 60

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy