Certified Racket AI Specialist Β· The Heart of AI
Hello, young teacher! π So far, we've been telling the computer exactly what to do. We gave it rules, and it followed them. But what if we could teach the computer to figure out the rules by itself?
Think about how you learn to ride a bicycle. Nobody gives you a manual with all the rules. You try, you fall, you try again, and slowly you get better. That's how Machine Learning works β the computer learns from examples and improves over time.
In this module, we will build our own simple machine learning system. We'll teach a computer to recognise patterns and make predictions. You'll see how AI really learns!
After this module, you will be able to:
Imagine you work at a fruit market. Your job is to sort apples and oranges. You don't have a rule book. Instead, a farmer shows you examples: "This is an apple. It's red and round. This is an orange. It's orange and round."
After seeing many examples, you learn the pattern. When you see a new fruit, you can guess if it's an apple or orange. That's exactly how Machine Learning works β you show the computer lots of examples, and it learns the pattern.
Today, we'll build a fruit sorter in Racket β a machine learning system that learns to recognise fruits!
Training phase: [Apple, red, round] β apple [Orange, orange, round] β orange [Apple, red, round] β apple Testing phase: [New fruit, red, round] β guess: apple!
Definition: Machine Learning is a way for computers to learn from examples without being explicitly programmed with rules.
Why important: This is how AI becomes smart β by learning from data instead of following fixed rules.
Simple explanation: It's like teaching a dog new tricks. You don't give it a book; you show it what to do and reward it when it gets it right.
Real-life: Spam filters learn which emails are spam by looking at examples.
School: You learn math by doing many practice problems.
Home: You learn to cook by trying recipes and learning from mistakes.
Nigerian: Learning to speak your mother tongue by listening to your family.
Illustration:
+-------------------+ +-------------------+
| Examples (data) | ---> | Machine Learning | ---> Rules (model)
+-------------------+ +-------------------+
apples, oranges can classify new fruits
Definition: Training is when we show the computer examples to learn from. Testing is when we check how well it learned.
Why important: We need to know if our model works on new data, not just the examples it already saw.
Simple explanation: It's like studying for a test (training) and then taking the test (testing).
Real-life: A student practices math problems (training) and then takes an exam (testing).
School: Doing homework is training; the final exam is testing.
Home: Practicing a dance routine (training) and performing it (testing).
Nigerian: Learning to cook from your mother (training) and cooking for guests (testing).
Illustration:
+------------+ +--------------+ +------------+
| Data | --> | Split data | --> | Training | --> Model
+------------+ +--------------+ +------------+
| |
V V
+------------+ +------------+
| Testing | | Evaluate |
+------------+ +------------+
Definition: Features are the characteristics of the data we use for learning. For a fruit, features could be colour, shape, and size.
Why important: The right features help the computer learn better.
Simple explanation: If you describe a person, you might use features like height, hair colour, and eye colour.
Real-life: To sort fruits, features are colour, shape, and size.
School: To describe a student, features are age, grade, and favourite subject.
Home: To describe a pet, features are size, colour, and breed.
Nigerian: To describe a car, features are brand, model, and colour.
Illustration:
Fruit β [colour, shape, size] Apple β [red, round, medium] Orange β [orange, round, medium] Banana β [yellow, long, small]
Definition: The label is the answer we want the computer to predict. For a fruit, the label is the type of fruit (apple, orange, etc.).
Why important: The computer learns to map features to labels.
Simple explanation: It's like the answer key for a test.
Real-life: In a photo of a cat, the label is "cat".
School: In a test, the label is the correct answer.
Home: When sorting laundry, the label is "dark" or "light".
Nigerian: In a market, the label is the price.
Illustration:
Training example: [red, round, medium] β apple (label) Training example: [orange, round, medium] β orange (label)
We can use lists to represent examples. Each example is a list of features, and we can store the label separately.
(define apple-example '(red round medium))
(define orange-example '(orange round medium))
(define training-data
'((red round medium apple)
(orange round medium orange)
(red round medium apple)
(yellow long small banana)))
Or we can use structures for cleaner code.
Definition: Nearest Neighbour is a simple machine learning algorithm that classifies a new example by finding the most similar example in the training data.
Why important: It's simple, powerful, and easy to understand.
Simple explanation: If you want to know what a new fruit is, find the fruit that looks most like it and call it that.
Real-life: If you see a new bird, compare it to birds you know.
School: If you have a new word, find the one that looks most like it.
Home: If you find a toy you don't recognise, compare it to toys you know.
Nigerian: If you see a new type of cloth, compare it to known fabrics.
Illustration:
New fruit: [red, round, medium] Compare to training: [red, round, medium] β apple (distance 0) [orange, round, medium] β orange (distance 1) [yellow, long, small] β banana (distance 3) Nearest is apple β predict apple!
We need a way to measure how similar two examples are. A simple way is to count how many features are different.
(define (distance a b)
(length (filter (lambda (x) (not (equal? x (car b))))
(map list a b))))
; Or simpler: count differences
(define (diff-count a b)
(if (null? a)
0
(if (equal? (car a) (car b))
(diff-count (cdr a) (cdr b))
(+ 1 (diff-count (cdr a) (cdr b))))))
Now we can write the full classifier.
(define (classify data features)
(define (distance-to-example ex)
(diff-count features (drop-right ex 1)))
(define sorted-data
(sort data (lambda (a b) (< (distance-to-example a) (distance-to-example b)))))
(last (car sorted-data))) ; the label is the last element
Let's train our model and test it.
(define training-data
'((red round medium apple)
(orange round medium orange)
(red round medium apple)
(yellow long small banana)
(green round small grape)
(purple round medium grape)))
(classify training-data '(red round medium)) β apple
(classify training-data '(orange round medium)) β orange
(classify training-data '(yellow long small)) β banana
Definition: Accuracy is the percentage of correct predictions our model makes on test data.
Why important: It tells us how well our model will work in the real world.
Simple explanation: If you get 8 out of 10 questions right on a test, your accuracy is 80%.
Real-life: A doctor's diagnostic accuracy is how often they are correct.
School: Your score on a test is your accuracy.
Home: How often you correctly guess the weather is your accuracy.
Nigerian: How often you correctly predict the price of goods is your accuracy.
Illustration:
Test examples: 10 Correct predictions: 8 Accuracy = 8/10 = 80%
Instead of just looking at the nearest neighbour, we can look at the K nearest neighbours and take a vote. This is called K-Nearest Neighbours (KNN).
Why important: It makes our model more robust to noisy data.
Simple explanation: Instead of asking one friend for advice, ask five friends and go with the majority opinion.
(define (k-classify data features k)
(define (label-counts lst)
; count how many of each label in the list
(define (count-label label lst)
(length (filter (lambda (x) (equal? (last x) label)) lst)))
; ... implement majority vote
)
Let's build a classifier that recognises animals based on features: size, habitat, and diet.
(define animal-data
'((large land herbivore elephant)
(small land herbivore rabbit)
(large water carnivore shark)
(medium land carnivore lion)
(small air herbivore bird)
(medium land omnivore bear)))
(classify animal-data '(medium land carnivore)) β lion
Let's build a complete system that: 1) Loads data, 2) Splits it into training and testing, 3) Trains a model, 4) Tests it, 5) Reports accuracy.
(define (ml-system data test-data)
(define correct
(length (filter (lambda (test)
(equal? (classify data (drop-right test 1))
(last test)))
test-data)))
(/ correct (length test-data)))
You built a complete machine learning system!
How to build a machine learning system:
Use physical examples β show different fruits and ask students to identify them. Explain that the computer does this with numbers. Emphasize that data quality matters. Let students create their own small datasets and train classifiers.
Ask your child to teach you how the classifier works. Create a simple dataset at home (e.g., toy names with features: colour, size, type). Let them show you how the computer learns.
Machine learning models can learn to play games better than humans. The AlphaGo model beat the world champion at the game of Go!
Machine Learning Flow:
+------------------+
| Collect Data |
+--------+---------+
|
V
+--------+---------+
| Split Data |
| (Train/Test) |
+--------+---------+
|
V
+--------+---------+
| Train Model |
+--------+---------+
|
V
+--------+---------+
| Test Model |
+--------+---------+
|
V
+--------+---------+
| Evaluate (Accuracy)|
+--------+---------+
|
V
+--------+---------+
| Predict New Data |
+------------------+
KNN Voting:
New example β find K nearest
|
V
+-----+-----+-----+
| | | |
Apple Orange Apple
(2) (1)
Vote: Apple!
| Algorithm | How it works | Good for |
|---|---|---|
| Nearest Neighbour | Find closest example | Small datasets |
| K-Nearest Neighbours | Vote among K neighbours | Noisy data |
You've built your first machine learning system! You learned about training and testing, features and labels, and implemented the Nearest Neighbour algorithm. You even built a K-Nearest Neighbours classifier. These are the same ideas used in real AI systems β you're now a machine learning creator!
| Term | Match |
|---|---|
| 1. Features | A. what we predict |
| 2. Label | B. characteristics |
| 3. Training | C. learning phase |
Answers: 1-B, 2-A, 3-C
Scenario: You are building a system to predict if it will rain tomorrow. Features: temperature, humidity, wind speed. Label: rain/no rain. How would you set up the machine learning system?
Answer: Collect historical data with these features and the label, split into training/testing, use Nearest Neighbour or KNN, evaluate accuracy.
Each group creates a dataset of 20 examples for a classification problem (e.g., animal recognition, food classification). Implement a Nearest Neighbour classifier and test it. Present results.
Build a classifier that predicts whether a student will pass an exam based on study hours and attendance. Create your own data and test it.
Build a "PokΓ©mon type classifier" that predicts the type of a PokΓ©mon (fire, water, grass) based on features like colour, height, and weight.
Implement a K-Nearest Neighbours classifier with K=3. Test it on a dataset of your choice and report the accuracy.
Implement a weighted KNN where closer neighbours have more influence on the vote.
Multiple choice answers: 1-A, 2-A, 3-A, 4-A, 5-A, 6-A, 7-A, 8-A, 9-A, 10-A, 11-A, 12-A, 13-A, 14-A, 15-A.
In Module Eight, we'll explore AI in the Real World β how AI is used in robotics, healthcare, games, and more. We'll also talk about the ethics of AI and how to build responsible systems. Get ready to see your AI skills in action!
π You've completed Module Seven Β· You are now a Machine Learning creator! π