Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using k-Nearest Neighbors (kNN) in Python (Overview)

In this video course, you’ll get a thorough introduction to the k-Nearest Neighbors (kNN) algorithm in Python. The kNN algorithm is one of the most famous machine learning algorithms and an absolute must-have in your machine learning toolbox. Python is the go-to programming language for machine learning, so what better way to discover kNN than with Python’s famous packages NumPy and scikit-learn!

You’ll explore the kNN algorithm both in theory and in practice. It’s important to learn about the mechanics of machine learning algorithms to understand their potential and limitations. At the same time, it’s essential to understand how to use an algorithm in practice. With that in mind, you’ll also focus on the use of kNN in the Python library scikit-learn.

In this video course, you’ll learn how to:

  • Explain the kNN algorithm both intuitively and mathematically
  • Implement kNN in Python from scratch using NumPy
  • Use kNN in Python with scikit-learn
Download

Sample Code (IPYNB)

55.8 KB
Download

Course Slides (PDF)

2.5 MB

00:00 Welcome to Using k-Nearest Neighbors in Python. I’m Kimberly with Real Python, and I’ll be your instructor for this video course. I’m delighted to discuss k-nearest neighbors with you because it’s one of the most flexible and intuitive machine learning algorithms.

00:17 In many data science problems, you’ll present an algorithm with a dataset of known features, like width and height. When a new observation arises, you’ll want to use the information you currently have to make a prediction for it. k-nearest neighbors can help you make such a prediction, and it does so by examining the new data points’ closest neighbors.

00:39 It uses known information about those neighbors to help predict an outcome.

00:46 But you don’t need to visualize all your data to make kNN work. Instead, you can code up your own k-nearest neighbors algorithm using Python.

00:57 Here’s what you can expect from this course. After this brief overview, you’ll learn what kNN is and what its main features are. We’ll use one primary example throughout this course, so you’ll get a sense for that dataset about sea snails before diving into a clear explanation of how the k-nearest neighbors algorithm works. Next, you’ll get a chance to code up your own KNN model, first from scratch and then with the machine learning package scikit-learn. After that, we’ll conclude by summarizing the course and reviewing the benefits and drawbacks of kNN.

01:34 So, let’s get started with the distinguishing features of k-nearest neighbors.

Become a Member to join the conversation.