Machine learning · learning from examples

Teach the Machine: is the mango ripe?

Nobody writes a rule for the machine. You show it examples of mangoes you already know how to classify, and it colors the map with what it "thinks" each region is. Then we test it with mangoes it has never seen.

↑ firmness (harder)
green skinskin color →yellow skin
Click on the map to:
0 unripe0 ripe

1 · Choose the training data

Each point on the map takes the color of the majority of its k nearest examples. This is the "k nearest neighbors" algorithm, one of the simplest in machine learning.

2 · Test with 200 new mangoes

—
correct
How does the machine decide? (optional)

On the map, choose ask and click anywhere: a new mango (◆) appears, linked to the k examples the machine consults, and its color comes from the vote. With the teach buttons, you decide the color: that is how you give the machine an example.

1Each mango becomes two numbers

x = skin color (0 = green, 1 = yellow) and y = firmness (0 = soft, 1 = hard). To the machine, a mango is just a point on the map.

2It measures the distance to each example

To find the nearest neighbors, the machine has to measure. We see it at a glance; it calculates the distance to each example, like a ruler:

d=Δx2+Δy2

Δx is the difference in skin color and Δy the difference in firmness between the new mango and the example. It is the Pythagorean theorem: the straight-line distance between two points.

3The k nearest ones vote

The machine picks the k examples with the smallest distance, and the majority decides. That is why k is always odd: there are no ties.

4It repeats this for the whole map

Running the vote on every small piece of the map produces the background colors. Notice that "training" here is just storing the examples: there is no hidden formula, the machine looks at its memory every time.

Choose ask and click on the map to see the vote.

To discuss with the class

  1. The machine learned a rule that nobody wrote. Where did it come from?
    A possible answer

    From the examples. The boundary between "unripe" and "ripe" on the map is just a consequence of where the examples are. Nobody wrote "if the skin is yellow and the fruit is soft, it is ripe." Change or add an example and the rule changes with it.

  2. Why does the biased sample get so much wrong, if every one of its examples is correct?
    A possible answer

    Because the sample is unbalanced: many unripe, few ripe, and no mango with yellow skin. In that region, the nearest neighbors are mangoes with green or half-yellow skin, almost all unripe, and the machine extends what it saw there. Correct data is not enough: it has to represent all the cases the machine will meet.

  3. Replace "mango" with "face," "résumé," or "loan application." What happens to the people who were left out of the data?
    A possible answer

    The same thing that happened to the yellow mangoes: the machine makes more mistakes with them, without warning. There have been face recognition systems that failed far more often with Black women, and a résumé screening tool that learned, from past hiring, to penalize women. Nobody programmed the prejudice; it came with the data.

  4. Add a wrong example on purpose (a ripe one among the unripe ones). What changes with k = 1 and with k = 9?
    A possible answer

    With k = 1, the wrong example creates a small "ripe island" around it: the machine memorizes the mistake. With k = 9, it loses the vote to its neighbors and the island disappears. A larger k ignores isolated mistakes, but if it is too large it also erases real details of the boundary. It is the trade-off between memorizing and generalizing.