Data Science

K-Nearest Neighbors Algorithm Using Python

Last updated on May 22,2019 12.2K Views

AtulSr. Research Analyst with a demonstrated history of working in the e-learning...Sr. Research Analyst with a demonstrated history of working in the e-learning industry. Experienced in machine learning with python and visualizing data and creating...

With the business world entirely revolving around Data Science, it has become one of the most sort after fields. Hence, the heavy demand for a Data Science Certification. In this blog on KNN algorithm, you will understand how the KNN algorithm works and how it can be implemented by using Python.

What is KNN Algorithm?

K nearest neighbors or KNN Algorithm is a simple algorithm which uses the entire dataset in its training phase. Whenever a prediction is required for an unseen data instance, it searches through the entire training dataset for k-most similar instances and the data with the most similar instance is finally returned as the prediction.

kNN is often used in search applications where you are looking for similar items, like find items similar to this one.

Algorithm suggests that if you’re similar to your neighbours, then you are one of them. For example, if apple looks more similar to peach, pear, and cherry (fruits) than monkey, cat or a rat (animals), then most likely apple is a fruit.

How does a KNN Algorithm work?

The k-nearest neighbors algorithm uses a very simple approach to perform classification. When tested with a new example, it looks through the training data and finds the k training examples that are closest to the new example. It then assigns the most common class label (among those k-training examples) to the test example.

Now you need to split the data into a training dataset (for making the prediction) and a testing dataset (for evaluating the accuracy of the model).

Before you continue, convert the flower measures loaded as strings to numbers. Next, randomly split the dataset into train and test dataset. Generally, a standard ratio of 67/33 is used for test/train split

Adding it all, let’s define a function handleDataset which will load the CSV when provided with the exact filename and splits it randomly into train and test datasets using the provided split ratio.

Testing handleDatasetfunction

Step 2: Calculate the distance

In order to make any predictions, you have to calculate the distance between the new point and the existing points, as you will be needing k closest points.

In this case for calculating the distance, we will use the Euclidean distance. This is defined as the square root of the sum of the squared differences between the two arrays of numbers

Specifically, we need only first 4 attributes(features) for distance calculation as the last attribute is a class label. So for one of the approach is to limit the Euclidean distance to a fixed length, thereby ignoring the final dimension.

This was all about the kNN Algorithm using python. In case you are still left with a query, don’t hesitate in adding your doubt to the blog’s comment section.

Python Basics: What makes Python so Powerful?

Python, you’ve heard of it and wonder what’s so special with this language. With the rise of Machine Learning and Artificial Intelligence, it is impossible to get away from it. You may question yourself, is Python easy to learn? Let me tell you, it actually is! and I am here to help you get started […]

What is an Interpreter in Java?

Interpreter in Java is a computer program that converts high-level program statement into Assembly Level Language. It is designed to read the input source program and then translate the source program instruction by instruction. Let us dig a bit deeper and understand the concept in detail! What is an Interpreter in Java? How does an […]

Top Python developer Skills you need to know

With the rising popularity and boon of Python, it has become one of the most sought-after and widely used programming languages in the industry. Creating a euphoria among developers, one is bound to wonder what are the Python Developer skills in order to become a Python developer. In this article, I will walk you through […]

How to Read CSV File in Python?

Do you know what mechanism works behind storing tabular data into a plain text file? The answer is CSV(Comma Separated Values) file which allows putting data into a plain-text format. In this article on “How to Read CSV File in Python”, we will be learning how to read, write and parse a CSV file in […]

Object Oriented Programming Python: All you need to know

Objected oriented programming as a discipline has gained a universal following among developers. Python, an in-demand programming language also follows an object-oriented programming paradigm. It deals with declaring Python classes and objects which lays the foundation of OOPs concepts. This article on “object oriented programming python” will walk you through declaring python classes, instantiating objects from […]

Python Database Connection: Know how to connect with database

Databases are critical for storing and processing data even if you consider a powerful programming language like Python. Ever wondered where does this whole large set of data is stored in or fetched from? In this article on “Python database connection”, I’ll talk about the same and take you through the following aspects in detail. […]

What is Python JSON and How to implement it?

Do you know how to transport your data from online API’s or store different kinds of data to your local machines? One way or the other you have immersed yourself in JSON which stands for Java Script Object Notation. It is a renowned and popular data format used for representing semi-structured data. Let’s get to […]

How to Learn Python 3 from Scratch – A Beginners Guide

Many of us stumble upon the same question, “as a beginner, should I learn Python 3?” If you are seeking the answer too, read this article, as I unravel what Python 3 has to offer. Programming using Python 3 has become the next-gen skill to acquire. No doubt, most entry level programmers are inclined towards […]

What are Comments in Python and how to use them?

Have you ever asked any programmer to explain the code he wrote a year ago? Or have you ever tried reading your own previously written code? It would become very time consuming and tiring if you had to reanalyze every block of code from scratch! The best thing to do is, add a comment. Comments […]