No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

k-Nearest Neighbors Accuracy in Python

by Khaleel O.
July 16, 2021
in Python
Reading Time: 3 mins read
A A
k-nearest neighbors accuracy python
k-nearest neighbors accuracy in Python

So for Part 2, let’s evaluate the k-nearest neighbors accuracy for our dataset. We will answer the question: will our model accurately classify normal and abnormal cases when presented with new data?

Before we use our model for real life predictions we will have to train and test our classifier and thereby get an actual numeric score to quantify how accurate our model will be in practice. This will indicate how accurate or inaccurate our knn classifier is. The higher the score, the better.

Using sklearn.model_selection.train_test_split we will split our dataset into random test and train subsets.

# Split into training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42, stratify=y)

Let’s explain what is happening here: The train_test_split() method returns 4 numpy arrays:

  1. X_train, X_test, y_train, y_test are all numpy arrays returned by train_test_split(). X_train and X_test are subsets of the X feature variable dataset. And y_train and y_test are subsets of
  2. train_test_split() accepts the following:
    • X our feature variables in the original dataset.
    • y our predictor variables in the original dataset.
    • Parameter test_size which in this case is 40% of our dataset. The split is then 60/40 with 60% of the dataset being for training of the classifier and 40% for testing.
    • Parameter random_state controls the shuffling applied to the data before it is split
    • stratify ensures that our labels are distributed between train and test sets exactly as they are in our original dataset according to the target variables or labels (y).

Once again we define our classifier knn as we did in Part 1:

#our classifier
knn = KNeighborsClassifier(n_neighbors=7)

Then we fit our classifier to the training data:

knn.fit(X_train,y_train)

Because we have to train our classifier before we actually test our classifier. We supply the training data to our classifier first and then it’s just a simple matter of getting the accuracy of our classifier by supplying it with test data and measuring how well it predicts normal vs abnormal when compared to the actual results:

print(knn.score(X_test, y_test))

#Output
#0.75

So our classifier is 75% accurate at predicting normal vs abnormal cases or, put another way, there is a 25% chance that our classifier could be wrong. Not the best, especially for medical data. We can probably do better if we use another classifier algorithm or maybe a larger dataset but we covered the basics for this tutorial so we are OK for now. 👍👍😉

Thanks for reading. We hope this tutorial was helpful. Find the full source code HERE.

Tags: k-nearest neighborsKNNmachine learning
Previous Post

Simple Python k-Nearest Neighbors Tutorial

Next Post

Python List Comprehension with DataFrames – Part 1

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy