Home · Academy · Robotics & Coding · Introduction to Data and AI · Project: Simple Classification Experiment

Project: Simple Classification Experiment

Build a simple rule-based classifier on a small dataset and honestly evaluate its accuracy and bias.

PROJECT COMPASS

What will you use this page for?

Core idea

We will build a simple rule-based (threshold) classifier on a small dataset, test it on training and test data, measure its accuracy, and honestly discuss the limits of its results.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Mixing up training and test If you choose the threshold by looking at the test data, high accuracy will mislead you. The test data must stay unseen while you decide. Trusting accuracy blindly If the data had 90 oranges and 10 apples, even a rule that says "everything is an orange" would score 90% accuracy. But that…

Next connection

Project Workshop module: A workshop where you combine the coding, robotics, and data ideas you have learned to design and build your own project from start to finish.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration45–75 min
PrerequisiteAI Ethics
ContentProject guide · 1,582 words
Last updated

One-sentence summary

We will build a simple rule-based (threshold) classifier on a small dataset, test it on training and test data, measure its accuracy, and honestly discuss the limits of its results.

Why does it matter?

Classification means placing an example into the correct group: sorting an email into "spam" or "not spam," or labelling a fruit as "apple" or "orange." Most artificial-intelligence tools do some kind of grouping like this behind the scenes.

In this lesson we will build a working classifier with our own hands. But let us be honest from the start: we are not training a real AI model here. We will write the rules ourselves. This is the best way to see that AI is not mysterious magic; in the end it rests on simple decisions that catch a pattern in the data. When you write the rule yourself, you also see with your own eyes where a machine can go wrong.

What is classification?

Example and label

Classification involves two things:

Our goal is to find a rule that looks at an example and predicts the correct label.

Two everyday examples

Example 1 — Fruit basket. A basket holds apples and oranges. Even with our eyes closed, weight alone often lets us tell them apart: oranges are usually heavier. A rule like "if the weight is above 150 grams it is an orange, otherwise an apple" gets most of the job right.

Example 2 — Message inbox. If a message contains words like "free," "click now," or "you won," it is probably an unwanted (spam) message. The rule "if any of these keywords appear, it is spam" is simple but useful.

In both cases we do the same thing: we look at one feature (weight, a keyword) and decide based on a threshold or rule.

A threshold-based classifier

The idea

Suppose we have each fruit's weight (in grams) and its true label. We pick a threshold: examples above it are oranges, examples below it are apples. Finding a good threshold is what "building" the classifier means.

Why split training and test data?

We divide the data into two parts:

Why? Because we can only tell whether a rule is truly good by trying it on examples it has not seen. It is easy to look good on examples whose answers we already know; the real question is whether it gets a new fruit right.

Code: create the data and split it

# Each example: (weight_grams, true_label)
data = [
    (120, "apple"), (130, "apple"), (140, "apple"),
    (135, "apple"), (125, "apple"), (110, "apple"),
    (160, "orange"), (170, "orange"), (180, "orange"),
    (155, "orange"), (175, "orange"), (165, "orange"),
]

# First half for training, second half for testing
training = data[:6] + data[6:9]   # some apples + some oranges
test = data[3:6] + data[9:]       # the remaining examples

Code: predict from the threshold

def predict(weight, threshold):
    # Above the threshold is orange, otherwise apple
    if weight > threshold:
        return "orange"
    else:
        return "apple"

Code: measure the accuracy

Accuracy is the number of examples we get right divided by the total number of examples.

def accuracy(dataset, threshold):
    correct = 0
    for weight, actual in dataset:
        if predict(weight, threshold) == actual:
            correct += 1
    return correct / len(dataset)

threshold = 150
print("Training accuracy:", accuracy(training, threshold))
print("Test accuracy:", accuracy(test, threshold))

The operation correct / len(...) gives a number between 0 and 1. A value of 1.0 means "got them all right," and 0.5 means "got half right."

Mini practice

Try the steps below on your own computer (with an adult nearby):

  1. Paste the three code blocks above, in order, into a single Python file.
  2. Run it and note the training and test accuracy.
  3. Change the line threshold = 150: try 145, then 135. How does the accuracy change?
  4. Find the threshold that gives the best test accuracy and write one sentence explaining why you chose it.

A mistake and its fix

In my first try I set the threshold too low:

Goal: Separate apples and oranges
Problem: With threshold = 100 everything was called "orange"; apples came out wrong
Check: The lightest orange is 155 g and the heaviest apple is 140 g; the border must sit between them
Fix: I set threshold = 150; now both groups are separated correctly

Lesson: The threshold must sit inside the gap that separates the two groups. We chose the rule by looking at the data itself, not by guessing.

An idea to improve it

So far we used a single feature (weight). You could add a second feature, such as colour. A two-condition rule like "orange if the weight is above 150 and the colour is orange" may make fewer mistakes on borderline examples. Before making the rule more complex, measure the accuracy of the single feature and ask whether the extra rule is really needed.

Common mistakes

Mixing up training and test

If you choose the threshold by looking at the test data, high accuracy will mislead you. The test data must stay unseen while you decide.

Trusting accuracy blindly

If the data had 90 oranges and 10 apples, even a rule that says "everything is an orange" would score 90% accuracy. But that rule never recognises a single apple. Accuracy alone is not enough; you also need to see which group the mistakes fall in.

Drawing big conclusions from small data

Twelve examples do not represent the real world. Our threshold works in this basket; it may fail on another orchard's fruit. A rule found from little data does not mean "correct everywhere."

Ignoring bias

If, by chance, all the oranges in our training data are very heavy, the classifier will mistake small oranges for apples. The rule learns whatever the data shows; if the data is biased, the rule becomes biased too.

Safety note

Inspect the errors, not only the score

Project test matrix
TestConditionExpected behaviourObserved resultNext decision
NormalStandard input and complete connectionThe core task is completedFill in during testingKeep it or make a small improvement
BoundaryLowest or highest accepted valueThe system remains stableFill in during testingReview the threshold or rule
FailureMissing, incorrect or unexpected inputA safe and understandable responseFill in during testingAdd error handling
RepeatAt least three trials under the same conditionSimilar resultsFill in during testingInvestigate the source of inconsistency

A single accuracy percentage can hide very different failure patterns. Separate true results, false positives and false negatives for each class, then look for shared conditions such as background, lighting, angle or unequal training examples. Change only one part of the data or model at a time and reuse the same held-out test set so that the comparison remains fair.

The activity must avoid personal or sensitive data. The result is a classroom model trained on a limited dataset, not a dependable system for judging people or making important decisions. Its limitations and unsuccessful examples belong in the project evidence.

Lesson summary

Check questions

  1. What is the difference between training data and test data, and why do we keep the test set separate?
  2. What does our threshold-based classifier look at to make its decision?
  3. If the accuracy comes out as 0.5, what does that mean?
  4. On data with 90 oranges and 10 apples, what accuracy does the rule "everything is an orange" get, and why is this misleading?
  5. Why do we say that we did not train a real AI model in this experiment?

Answers

  1. Training data are the examples we look at while choosing the threshold; test data are examples we have not seen before the choice. We keep the test set separate because we can only tell whether a rule is truly good on examples it has not seen.
  2. It looks at a single feature, the fruit's weight; if the weight is above the threshold it says "orange," otherwise "apple."
  3. It means the rule got half of the examples right and half wrong. In a two-group problem that is only as good as random guessing, so the rule is not really working.
  4. The accuracy is 0.90 (90%) because 90% of the examples are already oranges. It is misleading because the rule never gets a single apple right; the high number hides the real performance.
  5. Because we chose the rule (the threshold) ourselves by looking at the pattern in the data; the machine did not learn on its own. It is an honest, instructive experiment, not a truly trained model.

Source and verification note

For “Project: Simple Classification Experiment”, verification focuses on whether the relationship between What is classification? and Two everyday examples remains consistent across examples. Datasets in this module are small and educational; real personal data should not be used. An AI result should be evaluated not only for accuracy but also for data balance, error distribution and explainability.

Next lesson

Project Workshop module: A workshop where you combine the coding, robotics, and data ideas you have learned to design and build your own project from start to finish.

Start QuizBack to Introduction to Data and AI
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.