DeepLLM Quiz
Machine Learning

Nearest neighbours: density estimation and classification

10 questions on K-nearest-neighbour density estimation and classification, the role of K, the nearest-neighbour rule and its costs.

  • 10 questions
  • Stopwatch
  • Instant feedback

Shown publicly with your score. Don’t use your full real name.

Sample questions

Play now ↑

Answers stay hidden. Start the quiz to play them.

  1. Sample 1

    A new point has five nearest training neighbours: three of class A and two of class B. What does the classifier with K equal to 5 choose?

    • Class B
    • Class A
    • Neither: it refuses to decide
  2. Sample 2

    Two classes tie among the K nearest neighbours. How is the tie settled?

    • The classifier crashes
    • Ties are broken at random
    • The point is always given the first class

How it works

  • One question per screen. An answer is final once sent.
  • You see right away whether you were right, with a short explanation.
  • The stopwatch runs: on equal scores, the faster player ranks first.
  • Only your best attempt counts, so replaying is never risky.

About this quiz

One of the oldest and most intuitive ideas in machine learning fits in a single sentence: to decide about a new case, look at the most similar cases you already know. A bank judges a loan applicant by comparing them with similar past applicants. A doctor recalls patients with similar symptoms. A music service recommends tracks that similar listeners enjoyed. In statistics this idea is called the method of nearest neighbours, and it can be used both to estimate how densely data is packed in a region and to classify new points.

This quiz begins with the density version. It starts from a limitation of kernel methods, whose smoothing width is fixed for the whole data space, and shows how nearest neighbours lets the width adapt to the local situation. You will see what is held fixed and what is read from the data, what the single setting K controls, and what happens to the quality of the estimate when K is too small or too large. You will also learn a curious fact: the result is not a true probability density, because its total area is not finite.

The second part moves to classification. Applying the density idea to each class in turn and combining the results with Bayes' theorem leads to a simple rule: a new point goes to the class that is most common among its K nearest training points. Special cases and details, such as what happens when K is one and how ties are settled, are covered, together with a remarkable guarantee about its error rate when the data set becomes very large. The quiz closes with the cost of the method, which is large, and what can be done about it.

Frequent misunderstandings are worth testing. People assume that nearest neighbours has no settings to tune, that larger K is always better, or that the method learns a compact model. In fact it simply remembers the data. Others think that a simple rule cannot have a theoretical guarantee, and this quiz shows that it can. Despite its age, the method is still used in practice, for example in recommendation and retrieval systems, and in modern forms it searches among learned representations instead of raw measurements. Understanding its strengths and costs is useful background for those more modern uses.

The quiz suits students and practitioners who want to understand a classic method and the trade-offs behind it. No mathematics is needed. There are 10 questions, each with an explanation, and the quiz takes about four minutes.

Questions and answers

›How long does the quiz take?

About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation appears after every answer.

›Do I need to know what a density estimate is?

It helps, and the earlier quiz on histograms and kernel density estimates is a good introduction. But every question describes its setting, and a density can be thought of simply as how crowded the data is around a point.

›How does nearest-neighbour density estimation work?

Around the point of interest, a region grows until it contains a chosen number of data points, and the density is estimated from that number and the size of the region. Where data are crowded, the region is small, and where they are sparse, it has to grow large, so the estimate adapts to the local situation.

›Why is the result not a true probability density?

Far from the data, the region needed to capture the chosen number of points becomes enormous, so the estimated density falls off too slowly. Added up over all space, it comes to infinity instead of one, which a proper probability density can never do.

›What does K do?

K sets the amount of smoothing. A very small value makes the estimate respond to every individual point and be noisy. A large value averages over many points, which gives a smoother picture but can wash out real structure. As in other methods, the best choice lies in between.

›How does nearest-neighbour classification work?

To classify a new point, find the K training points closest to it and count how many belong to each class. The new point is assigned to the class with the most representatives. It follows from applying Bayes' theorem to the density estimates of the separate classes.

›What is special about using just one neighbour?

The rule becomes extremely simple: give the new point the class of the single closest training point. It remains surprisingly good, since with an unlimited amount of data its error rate cannot exceed twice that of the best classifier that could possibly be built.

›What are the practical drawbacks?

The whole training set has to be stored, and classifying a new point means searching it, which becomes expensive for large data sets. Special search structures, such as trees, can speed up the search, at the cost of some one-off preparation, but the approach remains limited compared with models that compress what they learn. Because the method relies on distances, the way features are scaled matters a great deal: a feature measured in thousands will swamp one measured in fractions unless the inputs are rescaled first. Choosing K by checking performance on held-back data is the standard way to avoid both too noisy and too smooth a result. In many modern systems the same idea is used with distances measured between learned feature vectors, which keeps the method relevant.

More quizzes