Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Certified Racket AI Expert Β· Lesson 8 of 8

Module Seven

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
8
Module 7 Β· Racket AI Specialist – Machine Learning

πŸ€– Module Seven Β· Machine Learning – Teaching Computers to Learn

Certified Racket AI Specialist Β· The Heart of AI

πŸ“– Module Introduction

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!

🧠 Fun fact: Machine Learning is how Netflix recommends movies, how self-driving cars see the road, and how your phone recognises your face!

🎯 Learning Objectives

After this module, you will be able to:

  • βœ”οΈ Understand what Machine Learning is and how it works.
  • βœ”οΈ Understand the difference between training and testing.
  • βœ”οΈ Build a simple classifier that learns from examples.
  • βœ”οΈ Use features to describe data.
  • βœ”οΈ Understand accuracy β€” how well a model performs.
  • βœ”οΈ Build a nearest neighbour classifier.
  • βœ”οΈ Train a model to make predictions on new data.

πŸ“š Warm-up Story Β· The Fruit Sorter

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!
  

🧩 Main Lessons

Lesson 1 Β· What is Machine Learning?

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
  
πŸ“Œ Mini summary: Machine Learning is learning from examples instead of following fixed rules.

Lesson 2 Β· Training vs Testing

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  |
                     +------------+        +------------+
  
πŸ“Œ Mini summary: Training is learning; testing is checking how well we learned.

Lesson 3 Β· Features – What Describes Our Data

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]
  
πŸ“Œ Mini summary: Features are the characteristics we use to describe our data.

Lesson 4 Β· Labels – What We Want to Predict

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)
  
πŸ“Œ Mini summary: Labels are the answers we want the computer to predict.

Lesson 5 Β· Representing Data in Racket

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.

πŸ“Œ Mini summary: We can represent training data as lists of features plus a label.

Lesson 6 Β· The Nearest Neighbour Algorithm

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!
  
πŸ“Œ Mini summary: Nearest Neighbour finds the most similar example and copies its label.

Lesson 7 Β· Measuring Similarity – Distance

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))))))
  
πŸ“Œ Mini summary: Distance measures how different two examples are.

Lesson 8 Β· Implementing Nearest Neighbour

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
  
πŸ“Œ Mini summary: We sort training examples by similarity and pick the closest one's label.

Lesson 9 Β· Training and Testing Our Classifier

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
  
πŸ“Œ Mini summary: Our classifier works on training data. Now we test it on new data.

Lesson 10 Β· Accuracy – How Good Is Our Model?

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%
  
πŸ“Œ Mini summary: Accuracy measures how often our model is correct.

Lesson 11 Β· Improving Our Classifier – K-Nearest Neighbours

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
       )
  
πŸ“Œ Mini summary: K-Nearest Neighbours uses multiple neighbours and takes a vote.

Lesson 12 Β· Building a Real-World Classifier – Animal Recognizer

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
  
πŸ“Œ Mini summary: We can use our classifier for any problem with features and labels.

Lesson 13 Β· Putting It All Together – A Complete ML System

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!

πŸ“Œ Mini summary: We combined everything into a complete machine learning pipeline.

πŸ“– Key Vocabulary

  • Machine Learning: Learning from examples.
  • Training: Learning phase.
  • Testing: Evaluation phase.
  • Features: Characteristics of data.
  • Label: What we want to predict.
  • Nearest Neighbour: Classifies by closest example.
  • Accuracy: Percentage of correct predictions.
  • K-Nearest Neighbours (KNN): Uses K nearest neighbours and votes.

🧠 Important Concepts

  • Data is key: Better data = better learning.
  • Features matter: Choose features that help distinguish.
  • Overfitting: When a model is too specific to training data.
  • Generalisaton: Model works well on new data.

πŸ“˜ Step-by-Step Explanations

How to build a machine learning system:

  1. Collect data (examples with features and labels).
  2. Split into training and testing sets.
  3. Choose an algorithm (like Nearest Neighbour).
  4. Train the model on the training data.
  5. Test the model on the testing data.
  6. Calculate accuracy.
  7. Use the model to make predictions on new data.

🌍 Real-life Examples

  • Spam detection: features = words in email, label = spam/not.
  • Face recognition: features = face measurements, label = person.
  • Medical diagnosis: features = symptoms, label = disease.

πŸ‡³πŸ‡¬ Nigerian Examples

  • Predicting yam prices: features = season, region, quality, label = price.
  • Traffic prediction: features = time, day, location, label = traffic level.
  • Language identification: features = words, label = language (Yoruba, Igbo, Hausa).

🎈 Fun Examples

  • Recognize PokΓ©mon: features = colour, size, type, label = PokΓ©mon name.
  • Predict movie ratings: features = genre, length, actors, label = rating.
  • Guess the animal: features = habitat, food, size, label = animal.

🏠 Everyday Examples

  • Sorting laundry: features = colour, fabric, label = light/dark/delicate.
  • Choosing a movie: features = genre, length, actors, label = watch/don't watch.
  • Deciding what to eat: features = taste, health, price, label = choose/not choose.

πŸ§‘β€πŸ« Teacher Notes

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.

πŸ‘ͺ Parent Tips

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.

🌟 Interesting Facts

  • The first machine learning program was written in 1951.
  • Machine learning is used to detect diseases from X-rays.
  • Self-driving cars use machine learning to understand the road.

πŸ€” Did You Know?

Machine learning models can learn to play games better than humans. The AlphaGo model beat the world champion at the game of Go!

🧾 Remember This

  • Machine Learning learns from examples.
  • Training is learning; testing is checking.
  • Features describe the data.
  • Labels are what we want to predict.
  • Nearest Neighbour is a simple learning algorithm.

⚠️ Common Mistakes

  • Testing on the same data used for training (cheating).
  • Using too few examples (not enough to learn).
  • Using irrelevant features (confuses the model).
  • Overcomplicating when simple works.

βœ… Best Practices

  • Always split data into training and testing.
  • Use enough examples (at least 10 per class).
  • Choose features that matter.
  • Start simple (Nearest Neighbour) before complex.

πŸ“Š ASCII Illustrations

   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!
  

πŸ“‹ Comparison Table

AlgorithmHow it worksGood for
Nearest NeighbourFind closest exampleSmall datasets
K-Nearest NeighboursVote among K neighboursNoisy data

πŸ“Œ End-of-Module Summary

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!

❓ Frequently Asked Questions (10)

  1. What is Machine Learning? β€” Learning from examples.
  2. What is training? β€” The learning phase.
  3. What is testing? β€” Checking how well we learned.
  4. What are features? β€” Characteristics of the data.
  5. What is a label? β€” The answer we want to predict.
  6. What is Nearest Neighbour? β€” Classifies by closest example.
  7. What is accuracy? β€” Percentage of correct predictions.
  8. What is KNN? β€” Uses K neighbours and votes.
  9. Why do we split data? β€” To test on unseen data.
  10. Can I use this for anything? β€” Yes, any problem with features and labels.

πŸ“ Review Questions (15)

  1. What is Machine Learning?
  2. What is the difference between training and testing?
  3. What are features?
  4. What is a label?
  5. How does Nearest Neighbour work?
  6. What is accuracy?
  7. What is K-Nearest Neighbours?
  8. Why do we split data into training and testing?
  9. Write a function to calculate distance between two examples.
  10. How do you classify a new example with Nearest Neighbour?
  11. What is a good accuracy score?
  12. Give an example of a machine learning problem.
  13. What is overfitting?
  14. How can you improve a classifier?
  15. What is the difference between rules-based AI and machine learning?

✏️ Fill-in-the-Blank

  1. ______ learning learns from examples.
  2. ______ is the learning phase.
  3. ______ is the evaluation phase.
  4. ______ describe the data.
  5. ______ is what we want to predict.

βœ… True or False

  1. Machine Learning doesn't need examples. (False)
  2. Training and testing should use the same data. (False)
  3. Features describe the data. (True)
  4. A label is what we predict. (True)
  5. Nearest Neighbour is a complex algorithm. (False)

πŸ”˜ Multiple Choice (15)

  1. What is Machine Learning? A) learning from examples B) following rules C) doing math D) storing data β†’ A
  2. What is training? A) learning phase B) evaluation phase C) storing data D) deleting data β†’ A
  3. What is testing? A) evaluation phase B) learning phase C) storing data D) deleting data β†’ A
  4. What are features? A) characteristics B) labels C) rules D) examples β†’ A
  5. What is a label? A) what we predict B) a characteristic C) a rule D) a feature β†’ A
  6. How does Nearest Neighbour work? A) finds closest example B) averages all examples C) picks random D) follows rules β†’ A
  7. What is accuracy? A) % correct predictions B) number of examples C) time taken D) memory used β†’ A
  8. What is KNN? A) K nearest neighbours B) K features C) K labels D) K rules β†’ A
  9. Why split data? A) to test on unseen data B) to make it slower C) to delete data D) to add features β†’ A
  10. What is a good use of ML? A) spam detection B) adding numbers C) sorting lists D) printing text β†’ A
  11. What is overfitting? A) too specific to training B) too general C) too fast D) too slow β†’ A
  12. Which is simpler? A) Nearest Neighbour B) Deep Learning C) KNN D) Random Forest β†’ A
  13. What does distance measure? A) similarity B) size C) time D) speed β†’ A
  14. What is a feature for a fruit? A) colour B) price C) name D) taste β†’ A
  15. What is a label for a fruit? A) type (apple) B) colour C) size D) shape β†’ A

πŸ”— Matching

TermMatch
1. FeaturesA. what we predict
2. LabelB. characteristics
3. TrainingC. learning phase

Answers: 1-B, 2-A, 3-C

πŸ“ Short Answer

  1. Explain the difference between training and testing.
  2. What is the role of features in machine learning?
  3. Write a function that calculates the accuracy of a classifier.

πŸ“– Scenario-based Exercises

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.

πŸ‘₯ Group Activity

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.

πŸ§‘β€πŸ’» Individual Activity

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.

πŸ’¬ Classroom Discussion Questions

  • What makes a good feature for a classifier?
  • What happens if you have too few examples?
  • How is machine learning different from traditional programming?

πŸ› οΈ Mini Project

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.

πŸ“‹ Practical Assignment

Implement a K-Nearest Neighbours classifier with K=3. Test it on a dataset of your choice and report the accuracy.

πŸ† Challenge Exercise

Implement a weighted KNN where closer neighbours have more influence on the vote.

πŸ”‘ Quiz Answers

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.

🎁 Key Takeaways

  • Machine Learning learns from examples.
  • Training = learning, Testing = evaluation.
  • Features describe data; labels are predictions.
  • Nearest Neighbour is a simple, powerful algorithm.
  • Accuracy measures model performance.

πŸš€ Preparation for the Next Module

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! πŸŽ‰

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’