Everyday Guidebooks
Cover of Build a Tiny Prediction Machine

More Books

Build a Tiny Prediction Machine

A Beginner's Guide to Learning from Text, Testing Predictions, and Understanding Mistakes with Python

by Todd Michael

Available now

Wondering how to make an ai for beginners without getting lost in advanced math or expensive tools? Start with one small, understandable project: a program that predicts a label for short pieces of text.

Using beginner-friendly Python examples, Todd Michael guides you from your first lines of code to a working prediction machine. You’ll create and clean examples, turn words into numbers, split data into training and test sets, and compare your model with a simple baseline. Along the way, you’ll read its mistakes, spot misleading results, and try careful improvements without treating the test set as a practice worksheet.

No prior programming or machine-learning experience is required. You’ll need a computer, an internet connection for setup, and a willingness to experiment. Most importantly, you’ll discover what a tiny model can teach you—and why it should never make consequential decisions for people.

Contents
  1. 1. Meet Your Tiny Prediction Machine
  2. 2. Set Up Your Python Workspace
  3. 3. Python Essentials for the Project
  4. 4. Make a Useful Set of Examples
  5. 5. Inspect and Prepare the Data
  6. 6. Hold Some Examples Back
  7. 7. Turn Words into Numbers
  8. 8. Train the First Model
  9. 9. Check the Model Against a Baseline
  10. 10. Read the Mistakes
  11. 11. Improve Without Cheating
  12. 12. Try Your Own Messages
  13. Load and prepare the examples.
  14. Create the pipeline.
  15. Fit the pipeline on the chosen training data.
  16. Start the prompt only after fitting.
  17. 13. Understand the Limits
  18. 14. Package, Document, and Keep Learning
  19. Other Books by Todd Michael

Read a sample: 1. Meet Your Tiny Prediction Machine

Imagine finding a short note on a kitchen counter:

“Please put the apples in the bowl.”

You probably recognize it as a message about food. Now imagine another:

“Please wipe the kitchen counter.”

That one concerns cleaning. The two notes are simple enough that the distinction may seem hardly worth discussing. Yet asking a computer to make that distinction opens a useful door into machine learning.

A computer does not look at a sentence and understand a kitchen the way you do. To make a prediction, a program needs a way to represent the text and a method for using examples to choose an answer. In this book, you will build a small version of that process in Python. You will give your program fictional household messages, teach it from labeled examples, and investigate how well its predictions hold up on messages it has not learned from.

The finished program will choose between two labels: food and cleaning. Its job is not to run a household, understand your intentions, or make decisions for you. Its job is to provide a manageable experiment: a small enough task that you can follow the data from the first example all the way to a prediction—and then examine what happens when the prediction is wrong.

You do not need to know Python or machine learning to begin. For now, the most important tools are curiosity and a willingness to ask, “What is the program actually using to make that guess?”

In everyday conversation, prediction often means a guess about the future. A weather forecast predicts whether it might rain tomorrow. Our program uses the word a little differently. Given a message, it predicts which label belongs to that message.

For example, suppose the program receives:

“Someone spilled juice on the table.”

It might return cleaning. That could be a reasonable choice if the message is being treated as a prompt to clean the spill. But someone else might focus on “juice” and choose food. The prediction is an answer produced under a particular set of rules and examples; it is not a fact that comes packaged inside the sentence.

This distinction matters because our two labels are deliberately broad. They help us learn, but they do not capture every possible meaning of a household note. A message can mention food and cleaning at the same time. It can also concern something else entirely:

“The hallway light is flickering.”

Neither of our labels fits that note well. Yet a program designed to choose only between food and cleaning will still make one of those choices. A tidy answer can hide the fact that the available choices were inadequate.

Throughout the project, you will practice looking past the answer itself. You will ask whether the message belongs in the task, whether the assigned label makes sense, and what evidence is available for evaluating the program’s choice.

Machine learning begins here with a collection of examples. Each example has two parts: a short message and the label a person has assigned to it.

Consider these fictional notes:

The message is the input: the information the program will receive when it makes a prediction. The label is the desired category for that input. Together, they form a labeled example.

Later, you will create a larger collection and save it in a file. The four examples above are only an introduction, not enough material from which to expect a useful model. Still, they let us see the shape of the task. The program will encounter text, produce one of two labels, and allow us to compare its choice with a label assigned by a person.

Notice that the labels themselves are ordinary words. They are not magic commands. We could have chosen different names, but we will consistently use food and cleaning so that our data, code, and results agree. A label such as Food may look equivalent to a person, while a program could treat it as a different piece of text. Consistency is one of the small, practical details that makes a project easier to inspect.

The fictional messages also give us room to experiment without relying on real household communications. You can invent notes for the purpose of learning rather than collecting private messages from family members or housemates. Invented data does not solve every data-quality problem, but it gives us direct control over what goes into this small exercise.

One way to build a message sorter would be to write rules by hand:

- If a message contains “bread,” choose food. - If it contains “wipe,” choose cleaning.

Those rules are understandable, but they are incomplete. A note about noodles may not mention bread. A request to scrub a sink may not use the word “wipe.” You could keep adding rules, but each new word would require another decision, and some words would cause trouble. “Wipe the jar of sauce” contains a cleaning word and a food-related word.

Our project takes a different approach. Instead of writing a separate rule for every word, we will provide labeled examples to a learning algorithm. The algorithm will use patterns in those examples to build a model: a fitted system that can produce a label for a new message.

You can think of the algorithm as the method for learning from the examples, and the model as the result after that method has been applied. The distinction will…

More More Books

Cover of Dog Breeds for Real LifeDog Breeds for Real LifeAvailable nowCover of How to Find the Right Dog Breed for Your HouseholdHow to Find the Right Dog Breed for Your HouseholdAvailable nowCover of Digital Nomad Ideas for Accessible Remote WorkDigital Nomad Ideas for Accessible Remote WorkAvailable nowCover of The Bathhouse Keeper's Day OffThe Bathhouse Keeper's Day OffAvailable nowCover of A Hurricane Guide for CaregiversA Hurricane Guide for CaregiversAvailable nowThe Fountain Pen FamiliarComing soon

About Todd Michael

Todd Michael is an American author and the founder of ToddMichael.tv. He writes practical, plain-English books on money, health, preparedness, technology, games, humor and more.

About the author Todd Michael · Todd Michael’s author site

Part of the ToddMichael.tv network.