Machine learning with KNN classifier

KNN is important classifier used in machine learning t for classifying the data.

  • Language used: Python,
  • Library used: Scikit, numpy, pandas, matplot
  • Interface: Anaconda Jupyter
  1. Save the data file in either excel or csv format. Re-direct python directory to file location.


2.  Import file and import required libraries, Identify the key features and classes in your data-set, and scatter plot to check their co-variance.



3. The output of print (df)


4. Initiate X array for the feature data-set and Y array for class data-set.




5. CASE I: In case I we shall use the entire data in data-set for training and testing again with the same data-set. Import KNN classifier and metrics. Calculate accuracy of prediction.


6.The net accuracy of training and testing on the same data.



7. CASE II: In case II we shall use the divide data-set into training and testing data-sets. Import KNN classifier,confusion matrix and metrics. Give test-size and random state as per required. Calculate accuracy of prediction.


8. Confusion matrix and net accuracy calculated where the training and testing is done on split data.





9. CASE III: In case III we shall use the cross-validation  training and testing. Import KNN classifier and metrics. Give test-size and random state as per required. Apart from accuracy score, cross-validation accuracy score, precision score, recall score, F1 score is also important parameters to assess the machine learned classifier.



10. Many times it a question as to use which K value. Use the below code to figure out optimum value of k-value that gives maximum accuracy in predicting.


optimum k value.png













Mtech in Clinical Eng Jointly offered by Indian institute of technology Madras& Christian medical college Vellore& Sree chitra tirunal institute for medical sciences and technology Trivandrum.
This entry was posted in Machine Learning, Uncategorized and tagged , , . Bookmark the permalink.

2 Responses to Machine learning with KNN classifier

  1. mohammed says:

    wow…what a great article…great writer..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s