K-Nearest Neighbor – Python

0
469

KNN Classifier: In this section we would cover K-NN in Python. As we have already covered basics of K-NN algorithm in our previous topic, so in this section we would look into Python libraries we need to have in our system, Python Commands required to implement the K-NN logic. So Let us start with our step by step process of implementation of K-NN.

We will try to predict probability of default/Non-Default using Naïve Bayes algorithm. Please download the data provided in the data repository section.

Step 1: Import the Libraries. We would use three libraries for this analysis:

  • numpy
  • pyplot
  • pandas

Use below command to import the libraries.

KNN_Py_111

Step 2: Import the data set.

 

KNN_Py_112

Step 3: Splitting the data set into the Training set and Test set. Provided data set has 300 observations, we would divide it into 75%-25% for training and testing purpose.

 

KNN_Py_113

Step 4: Feature Scaling: Since range of two of our predictor variables is very different, while Age varies from 18-60 whereas salary varies from 1127-142293. We need to do feature scaling so that both of the variables have same proportional impact on the target variable. In the following command we are excluding the target variable from feature scaling which is at position 3.

 

KNN_Py_114

Step 5: Fitting K-NN to the Training set

 

KNN_Py_115

Step 6: Predicting the Test set results

KNN_Py_116

Step 7: Checking the accuracy of data model using confusion matrix

KNN_Py_117

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here