Skip to content

alphaPlan · Programming in a Data World · Cheat-sheet 03

From code to ideas

See how machines learn from examples, build and break a Caesar cipher, and write programs that wait for a key or a click.

Ideas to remember

  1. 01Machine learning is programming a computer to get better at a task by using example data, useful exactly when a plain rule is hard to write.
  2. 02Supervised learning learns from labelled examples, unsupervised learning finds structure in unlabelled data, and reinforcement learning learns from rewards and penalties.
  3. 03The pieces of information you feed a model are its features; adding a good feature, such as weight next to height, can raise accuracy a lot.
  4. 04The Caesar cipher moves every letter forward by a shift and wraps around at the end of the alphabet, so decrypting is shifting by minus the same number.
  5. 05An event is an action from outside the program, like a key press or a click, and event-driven code waits for it and reacts.
  6. 06A state machine is always in one of a few states, and events move it from one state to the next.

Words

ord, chr
ord returns the number the computer uses for a character, 65 for capital A; chr turns that number back into the character.
% 26
The remainder after dividing by 26, which makes the alphabet wrap around like a circle and handles negative shifts too.
screen.listen()
Tells the screen to start paying attention to events; without this line nothing happens.
screen.onclick(my_function)
Runs my_function on a click; the function must take x and y, the coordinates of the click.
state = (state + 1) % 3
Moves to the next state and wraps back to 0 after 2, like a traffic light.
GAN
A generative adversarial network: a Generator produces fakes, a Discriminator says real or fake, and every rejection improves the Generator.

Do this

  • Solve the smallest problem first: make one character shift correctly, then let a loop handle the whole message.
  • Pass the function itself to onkey, my_function and not my_function(), so the screen can call it later each time the key is pressed.
  • Pick features, measure accuracy, then add or change features until the predictions are good enough for the job.
  • Choose a long, unusual passphrase that follows no common template, because a pattern-hunting model expects the obvious patterns.
  • For a key handler write def my_function(): with no arguments; for a click handler write def my_function(x, y):.

Watch out

  • The Caesar cipher has only 25 useful shifts, so a computer can try them all in a blink; it is a toy, not real security.
  • Writing my_function() with brackets runs the function right now and hands the screen its result instead of the function.
  • A rule with one feature, such as height alone, gets many cases wrong; count the mistakes before you trust a model.

Found something unclear, outdated or improvable? Suggest an improvement