Menu

All Courses / undefined

All Courses / undefined / undefined

CSE

Uploaded on

13 Jul 2022

Skill-Lync

**What is the K nearest neighbour algorithm?**

The KNN algorithm is an instance-based algorithm. The basic assumption is that there are groups in the dataset. A new data point is assigned to a group depending upon the neighbours. This is also called the Lazy algorithm.

For example, consider the dataset given below in the picture. A point labelled as a black star is assigned to the blue group because for K=7 there are four blue and 3 red. While if we consider K=5 the same is assigned to the brown group.

The nearest neighbours are calculated by using the distances between the target point and various points around it.

The algorithm:

1, Load the dataset into your python.

2, Choose the value of k (that is the nearest neighbour)

3, Calculate the distance between the test point and other data points

1, There are four ways to compute the distance between the data points.

a) Hamming Distance

This distance compares the similarity between two strings and finds the difference between them. The distance is a measure of dissimilarity between them.

b) Euclidean Distance

4, Once the distance is found between the test point and other points, the distances are sorted in ascending order.

5, Depending upon the choice of 'k' the k number of rows is chosen.

6, Based on the majority the test point is classified.

The algorithm is simple and easy to understand and interpret. It is beneficial for non-linear data because there is no underlying assumption of any kind. It has high accuracy and one can use it for both regression and classification.

However, higher memory is required for computing the distances. For N points we will have to compute N(N-1) values. It is very sensitive to outliers.

**Here we are using the iris data from UCI machine learning repository. **

After displaying the head of the data, we do the following.

1) Split the input and output

2) Split the training and test data

3) Scale the features

Now let us import the k-NN model and fit the data to the model.

Here we have taken 10 neighbours and we predict y_pred using the X test.

To find the optimal number of neigbours we plot error vs the neighbours.

Finally, we print the model accuracy, classification report, and accuracy score.

We can use the same algorithm for regression also.

Author

Navin Baskar

Author

Skill-Lync

Continue Reading

**Related Blogs**

Christmas Time is Near, Time for Joy and Time for Cheer.

Premium Master’s Program can do so at a discount of 20%. But, Christmas is time for sharing, therefore if you and your friend were to join any Skill-Lync Master’s Program together, both of you will get a discount of 30% on the course fee of your Premium Master’s Program

CSE

24 Dec 2021

Career Prospects For Software Engineers

Increase your career opportunities by becoming a software engineer and make the world a better place. Enroll in upskilling courses and practice the skills you learn.

CSE

27 Dec 2021

Suggested Career Path For A Software Developer

Software development is rated as the best job in the industry. Individuals with the right software development skills, good communication, and an open mind to adapt, learn, and evolve can find success in the field.

CSE

28 Dec 2021

7 best Java Project Ideas To Showcase Your Programming Skills

If you aspire for a career in the software development space, upskilling yourself with the knowledge and practical application of programming languages is mandatory.

CSE

29 Dec 2021

Why choose a career in computer science?

The most fascinating thing about the chosen ways of completing tasks on computers is that we only choose them because we do not have a simpler way yet.

CSE

30 Dec 2021

Author

Skill-Lync

Continue Reading

**Related Blogs**

Christmas Time is Near, Time for Joy and Time for Cheer.

Premium Master’s Program can do so at a discount of 20%. But, Christmas is time for sharing, therefore if you and your friend were to join any Skill-Lync Master’s Program together, both of you will get a discount of 30% on the course fee of your Premium Master’s Program

CSE

24 Dec 2021

Career Prospects For Software Engineers

Increase your career opportunities by becoming a software engineer and make the world a better place. Enroll in upskilling courses and practice the skills you learn.

CSE

27 Dec 2021

Suggested Career Path For A Software Developer

Software development is rated as the best job in the industry. Individuals with the right software development skills, good communication, and an open mind to adapt, learn, and evolve can find success in the field.

CSE

28 Dec 2021

7 best Java Project Ideas To Showcase Your Programming Skills

If you aspire for a career in the software development space, upskilling yourself with the knowledge and practical application of programming languages is mandatory.

CSE

29 Dec 2021

Why choose a career in computer science?

The most fascinating thing about the chosen ways of completing tasks on computers is that we only choose them because we do not have a simpler way yet.

CSE

30 Dec 2021

Related Courses

4.7

40 Hours of content

Data science Domain

4.9

25 Hours of content

Data science Domain

4.7

137 Hours of content

Data science Domain

Showing 1 of 4 courses

Try our top engineering courses, projects & workshops today!Book a FREE Demo