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

Experiment 4: Aim/Overview of The Practical: Task To Be Done

The document describes an experiment to implement the K-nearest neighbors (KNN) machine learning algorithm. The experiment uses the Iris dataset to classify samples into categories based on their similarity to existing samples. The KNN algorithm is applied using various values of k to determine the optimal number of neighbors for classification. Accuracy scores are calculated and plotted against k to identify the best performing k value, which is 12 for this dataset.

Uploaded by

Mrsingh Official
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)
70 views

Experiment 4: Aim/Overview of The Practical: Task To Be Done

The document describes an experiment to implement the K-nearest neighbors (KNN) machine learning algorithm. The experiment uses the Iris dataset to classify samples into categories based on their similarity to existing samples. The KNN algorithm is applied using various values of k to determine the optimal number of neighbors for classification. Accuracy scores are calculated and plotted against k to identify the best performing k value, which is 12 for this dataset.

Uploaded by

Mrsingh Official
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/ 7

Experiment 4

1. Aim/Overview of the practical: Write a program to Implement K-Nearest Neighbor on any data
set.
2. Task to be done: To implement KNN by using any dataset. K-Nearest Neighbour is one of the
simplest Machine Learning algorithms based on Supervised Learning technique.K-NN algorithm
assumes the similarity between the new case/data and available cases and put the new case into the
category that is most similar to the available categories.K-NN algorithm stores all the available data and
classifies a new data point based on the similarity. This means when new data appears then it can be
easily classified into a good suite category by using K- NN algorithm.K-NN algorithm can be used for
Regression as well as for Classification but mostly it is used for the Classification problems.K-NN is
a non-parametric algorithm, which means it does not make any assumption on underlying data.It is also
called a lazy learner algorithm because it does not learn from the training set immediately instead it
stores the dataset and at the time of classification, it performs an action on the dataset.KNN algorithm at
the training phase just stores the dataset and when it gets new data, then it classifies that data into a
category that is much similar to the new data.
Example: Suppose, we have an image of a creature that looks similar to cat and dog, but we want to
know either it is a cat or dog. So for this identification, we can use the KNN algorithm, as it works on a
similarity measure. Our KNN model will find the similar features of the new data set to the cats and
dogs images and based on the most similar features it will put it in either cat or dog category.
3. Algorithm: The K-NN working can be explained on the basis of the below algorithm:

Step-1: Select the number K of the neighbors


Step-2: Calculate the Euclidean distance of K number of neighbors\
Step-3: Take the K nearest neighbors as per the calculated Euclidean distance.
Step-4: Among these k neighbors, count the number of the data points in each category.
Step-5: Assign the new data points to that category for which the number of the neighbor is maximum.
Step-6: Our model is ready.

Flowchart:
4. Dataset:Iris Dataset

5. Code for experiment/practical:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import sklearn.metrics as metrics

from sklearn.model_selection import train_test_split


from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import accuracy_score
#import sklearn.metrics as metrics
from sklearn.neighbors import KNeighborsClassifier

from google.colab import files


uploaded = files.upload()
data = pd.read_csv('iris.csv')
data.head()
data.info()

tmp = data.drop('Unnamed: 5', axis=1)


tmp.head()
X = data.drop(['Unnamed: 5', 'variety'], axis=1)
y = data['variety']
#print(X.head())
print(X.shape)
#print(y.head())
print(y.shape)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=5)


print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
k_range = list(range(1,26))
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X, y)
y_pred = knn.predict(X)
scores.append(metrics.accuracy_score(y, y_pred))
plt.plot(k_range, scores)
plt.xlabel('Value of k for KNN')
plt.ylabel('Accuracy Score')
plt.title('Accuracy Scores for Values of k of k-Nearest-Neighbors')
plt.show()
k_range = list(range(1,26))
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
scores.append(metrics.accuracy_score(y_test, y_pred))
plt.plot(k_range, scores)

plt.xlabel('Value of k for KNN')


plt.ylabel('Accuracy Score')
plt.title('Accuracy Scores for Values of k of k-Nearest-Neighbors')
plt.show()
knn = KNeighborsClassifier(n_neighbors=12)
knn.fit(X, y)
# make a prediction for an example of an out-of-sample observation
knn.predict([[6, 3, 4, 2]])

6. Result/Output:
Learning outcomes (What I have learnt):

1. I have Learnt What is KNN Algorithm.

2. I have learnt how to implement the K-Nearest Neighbor on any data set.

3. I have learnt How KNN algorithm works.


Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Demonstration and Performance 5
(Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

You might also like