- Sheikh Aman

# K-NN classification in Python using Scikit-learn.

Updated: Aug 10

**Topics for you:**

Introduction.

What is K-NN algorithm?

Python implementation K-NN algorithm.

K-NN classification using Scikit-Learn.

Application of Knn.

Conclusion.

**Introduction.**

According to experience, this is one of interesting and easy to use an algorithm which makes classification very easy. Before going to code of classification and regression using Scikit-Learn in K-Nearest Neighbor(K-NN) algorithm you should have basic knowledge on the following topics:

What is the K-Nearest Neighbour (KNN) algorithm?

What is the need of KNN algorithm?

When we use KNN algorithm?

KNN algorithm steps. | Working of KNN. | Pseudocode for KNN algorithm.

How to select K for the KNN algorithm.

Advantage and Disadvantage of KNN algorithm.

How to improve performance of KNN?

Don't worry if you are missing any of this just click on this ** Introduction for K-Nearest Neighbor Algorithm. | For Beginners**. It will just 4 minutes and you will be ready and If you have concepts about those topics mentioned above, then let's jump to the main topic.

**What is K-NN algorithm?**

K-nearest neighbors (KNN) algorithm is a sort of supervised ML algorithm which may be used for both classifications also as regression predictive problems. However, it's mainly used for classification predictive problems within the industry. the subsequent two properties would define KNN well −

Lazy learning algorithm − KNN is a lazy learning algorithm because it doesn't have a specialized training phase and uses all the data for training while classification.

Non-parametric learning algorithm − KNN is a non-parametric learning algorithm because it doesn’t assume anything about the underlying data.

**Python implementation K-NN algorithm.**

As we all know that K-NN algorithm is used for both classification and regression, So here is the application of K-NN algorithm in both the fields. Link for the dataset is __here__

**K-NN classification using Scikit-Learn.**

Code snippets with output.

Exact Python code for K-NN classification.

Run the code yourself facility.

**Code snippets with output.**

__Important ____packages__.

This is a basic thing for training any model. At first, we to have import basic *packages.*

__Loading and reading the dataset.__

Now loading the data and check with a *head( ) function*.

__Data preprocessing.__

At first, we will select *dependent and independent* values. Then, we will import *train_test_split* class of *model_selection* library from *scikit-learn*. And then, we will import *StandardScaler *class of preprocessing from scikit-learn

__Training the model and making predictions__

1.for k=8

Next, we will import *KNeighborsClassifier* class of neighbors from *scikit-learn* and then we will predict our model with *predict( ) function*. Then, we will get our *accuracy score* and *confusion_matrix from sklearn.metrics*

2.for k=5

We will do the same as above.

**Exact code for K-NN classification in python.**

**Run the code by your self.**

**Application of K-NN **

**Banking System**

KNN is often utilized in the banking industry to predict whether a private is fit loan approval? Does that individual have characteristics almost like the defaulters one?

**Calculating Credit Ratings**

KNN algorithms are often wont to find an individual’s credit rating by comparing with the persons having similar traits.

**Politics**

With the assistance of KNN algorithms, we will classify a possible voter into various classes like “Will Vote”, “Will not Vote”, “Will Vote to Party ‘Congress’, “Will Vote to Party ‘BJP’.

Other areas during which the KNN algorithm are often used are Speech Recognition, Handwriting Detection, Image Recognition and Video Recognition.

**Conclusion.**

Great, Here comes an end of this discussion. I will not say this as a tutorial because a healthy discussion effects more than a tutorial. If you agree with my point then let me know in the comment section.

Here we have learned Following thing:

Introduction.

What is K-NN algorithm?

Python implementation K-NN algorithm.

K-NN classification using Scikit-Learn.

Application of Knn.

Conclusion.

I will always look forward to any feedback or question from your side. Ask a question in the comment section, I will try my best to answer. Till then stay tuned By filling Subscribe form present at end of this page(header section).

Thank you!