Skip to content
Exit presentation

How AI Learns I: From Rules to Data

Write the rule yourself

The nearest-example rule is a program you can type.

Loading lab…

Six examples. A new crate at 3 marked, 2 kilos.
Step 1 of 5
  1. Six examples. A new crate at 3 marked, 2 kilos.
  2. First example: distance 9. Best so far.
  3. Second example: distance 4. Smaller. New best.
  4. Examples 3 to 6: none smaller. Best stays.
  5. Hand back the winner's word: full price.

The rule

A walk visits the examples one by one, in list order.

Walk the examples once and carry the nearest one so far. Hand back its word at the end. When two are exactly as near, keep the one met first. A vote of several examples reports a tie instead. This walk keeps the first.

  • Distance — the gap across, squared, plus the gap up, squared. Across is the marked count. Up is the kilos.
  • Best so far — one box for the smallest distance. One box for its word.

Each example is a list: marked count, kilos, word. examples[i] is one example. The second [ ] picks one item from it. examples[i][0] is the marked count. examples[i][2] is that example's word.

Three crates are checked. First: 3 marked, 2 kilos. Second: 10 marked, 6 kilos. Third: 6 marked, 3 kilos. For the third, the nearest example is 5 marked, 2 kilos. Its distance is 1 + 1 = 2. Its word is full price. The real word is cut price, so the walk is wrong there. A fair test reports this miss.

Jump to lesson

This track

  1. 0 Start here
  2. 1 A rule a person wrote, and a pile of examples
  3. 2 Keep some crates back
  4. 3 Right out of how many
  5. 4 Split it differently
  6. 5 Learning the examples instead of the pattern
  7. 6 Ask more than one
  8. 7 The order you do it in
  9. 8 Write the rule yourself
  10. 9 The big picture
  11. 10 Track quiz

Other tracks