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

KNN Algo

The document discusses the K-nearest neighbors algorithm, which uses training datasets to classify new examples based on their similarity to existing labeled examples. It works by finding the k closest training examples in feature space and assigning the label with the most common class among those k examples. The document provides examples of using KNN for classification and regression problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

KNN Algo

The document discusses the K-nearest neighbors algorithm, which uses training datasets to classify new examples based on their similarity to existing labeled examples. It works by finding the k closest training examples in feature space and assigning the label with the most common class among those k examples. The document provides examples of using KNN for classification and regression problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

K-nearest Neighbors

K-nearest neighbors (k-NN) is a pattern recognition algorithm that uses training datasets to find the k closest relatives in
future examples.

When k-NN is used in classification, you calculate to place data within the category of its nearest neighbor. If k = 1, then it
would be placed in the class nearest 1. K is classified by a plurality poll of its neighbors.

Ex : to find the movie genere

K-Nearest Neighbor(KNN) Algorithm for Machine Learning

o K-Nearest Neighbour is one of the simplest Machine Learning algorithms based on Supervised Learning technique.
o 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.
o 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 well suite category by using K- NN algorithm.
o K-NN algorithm can be used for Regression as well as for Classification but mostly it is used for the Classification
problems.
o K-NN is a non-parametric algorithm, which means it does not make any assumption on underlying data.
o 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.
o 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.

o 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.

Why do we need a K-NN Algorithm?


Suppose there are two categories, i.e., Category A and Category B, and we have a new data point x1, so this data point will
lie in which of these categories. To solve this type of problem, we need a K-NN algorithm. With the help of K-NN, we can
easily identify the category or class of a particular dataset. Consider the below diagram:

How does K-NN work?

The K-NN working can be explained on the basis of the below algorithm:

o Step-1: Select the number K of the neighbors


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

Question :Predict the genere of “Barbie” movie in which IMdb rating is 7.4 and
duration is 114 then find the Genere of it?
Sol : Find the distances using Euclidean Distance with formula
D = √[ (x2 2 – x1 1 )2 + (y2 2 – y1 1 )2].
Means we could say Barbie movie is the Comedy movie.

You might also like