liveBook · Manning

Chapter 12: k-nearest neighbors

What you will learn in chapter 12 of Grokking Algorithms, Second Edition

This chapter introduction previews classification with k-nearest neighbors, feature extraction, regression, and the algorithm’s uses and limitations.

After this chapter you will be able to

Section by section

Introduction

This chapter introduction previews classification with k-nearest neighbors, feature extraction, regression, and the algorithm’s uses and limitations.

12.1 Classifying oranges vs. grapefruit

This section introduces k-nearest neighbors classification by deciding whether an ambiguous fruit is an orange or a grapefruit.

Key terms: k-nearest neighbors (KNN), classification, neighbors

12.2 Building a recommendations system

Shows how a movie recommendation system can use nearby users with similar tastes to suggest movies.

Key terms: recommendations system, similarity, similar taste

12.3 Feature extraction

The section shows how to represent fruit or users as numbers, measure their similarity by distance, and use that similarity to recommend movies.

Key terms: features, feature extraction, vectors, distance, Pythagorean formula, dimensions, recommendations system, ratings, similar users

12.4 Regression

The section explains how KNN predicts numerical values by averaging nearby examples, applies this to bread sales, and notes a limitation of distance-based similarity.

Key terms: k-nearest neighbors, regression, classification, features, nearest neighbors, loaves sold, distance formula, cosine similarity, vectors

12.5 Picking good features

The section explains why KNN recommendations depend on choosing features that are relevant to the prediction and broad enough to represent users’ tastes.

Key terms: KNN, features, bias, recommendations engine

12.6 Introduction to machine learning

The section introduces machine learning through KNN and identifies the recommendations system as an example.

Key terms: KNN, machine learning, recommendations system

12.7 OCR

The section uses OCR to show how feature extraction and k-nearest neighbors can recognize characters in images, then names the training step.

Key terms: OCR, optical character recognition, KNN, nearest neighbors, feature extraction, training, model, lines, points, curves

12.8 Building a spam filter

This section introduces the Naive Bayes classifier as a way to estimate whether an email is spam from the words it contains.

Key terms: Naive Bayes classifier, spam, probability

12.9 Predicting the stock market

The section uses stock-market prediction to show why choosing useful features for machine learning can be difficult.

Key terms: features, past numbers, future performance, variables

12.10 A high-level overview of training an ML model

This section outlines how to prepare data, train and evaluate an ML model, and tune it before use.

Key terms: data, clean, bad data, features, train, validate, evaluating, predictions, parameter tuning, K

12.11 Recap

The recap summarizes how KNN uses nearby examples for classification and regression, and why comparable numerical features matter.

Key terms: KNN, k-nearest neighbors, classification, regression, feature extraction, features

Concepts explained in this chapter

← Chapter 11: Dynamic programming Chapter 13: Where to go next →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.