Unit 8 · AI & the Impact of Computing · Lesson 8.3 · Code

Build a Tiny Classifier

No neural networks. You already know everything this needs: lists, averages, a procedure, and an if. Then the most important demonstration of the unit — 90% accurate, and broken for a quarter of the people.

Big Idea 5 — Impact of ComputingCED 5.3 · Create PT rehearsal

On this page: AP-TESTABLE sections marked with a BI5 tag  ·  CONTEXT — NOT ON THE AP EXAM anything with this label. Exam-tagged questions never test the context material.

Before this lesson

No reading — code. Keep 3.9 (average of a list), 3.12 (procedures with parameters), and 5.7 (filter by category) open.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Impact, in code. You’ll see bias appear in a program you can read line by line.

2

The Hook

“Accuracy: 90%.” Is it good? Ask one more question before you answer: 90% for whom?
3

The Idea

CONTEXT — NOT ON THE AP EXAM

A classifier sorts items into categories. The simplest one you can build:

  1. For each category in the labeled training data, compute the average of its values.
  2. For a new item, predict the category whose average is closer.
  3. Check it on test data — labeled examples the model never saw — and report accuracy: correct predictions ÷ total.

Step 2 in a different costume — deciding whether a song is slow or fast by tempo. (AP Pseudocode has no absolute-value procedure, so both versions compare squared distances.)

Python

def closer_to(value, avg_a, avg_b):
    if (value - avg_a) ** 2 < (value - avg_b) ** 2:
        return "A"
    return "B"

# slow songs average 75 bpm, fast average 140
print(closer_to(100, 75, 140))   # A (slow)

AP Pseudocode

PROCEDURE closerTo(value, avgA, avgB)
{
   IF ((value - avgA) * (value - avgA) <
       (value - avgB) * (value - avgB))
   {
      RETURN("A")
   }
   RETURN("B")
}
DISPLAY(closerTo(100, 75, 140))
A model’s performance is a number you check, not a claim you make. And one number can hide a lot: 90% overall can mean 100% for most people and 20% for some.
4

Try It — The Bias Demonstration

CONTEXT — NOT ON THE AP EXAM   the idea it demonstrates is AP-TESTABLE

A classifier decides whether a mango is ripe using two measurements: redness and softness. There are two varieties. Variety A turns red when ripe. Variety B stays green when ripe but gets soft. The test set is 75% Variety A and 25% Variety B — like a product used mostly, but not only, by one group. Train it two ways and compare.

training set (40 mangoes):
color: red = ripe Variety A, green = ripe Variety B, grey = unripe · faint dots = training mangoes · ◆ = test mangoes · yellow ring = predicted wrong · ✖ = learned averages

Your program (Lab 8.3)

Build the one-feature version on the dataset your teacher gives you: a procedure that averages each category, closer_to, a loop that classifies every test item and counts correct ones, and accuracy reported overall and for each group. Then swap in the skewed training set and report what changes.

AP connection. A list, a student-developed procedure with a parameter, selection, and iteration — the Create PT four, again.
5

Vocabulary

classifier Impact
A program that sorts items into categories. CONTEXT — NOT ON THE AP EXAM
test data Impact
Labeled examples kept separate from training, used to check the model. CONTEXT — NOT ON THE AP EXAM
accuracy Impact
Correct predictions divided by total predictions. Always ask: accuracy for whom? CONTEXT — NOT ON THE AP EXAM
6

Check

Five questions. Compute on paper.

0 of 5 answered
7

Connections

Came from 3.9 — average of a list — now it’s a learned rule.
Came from 5.7 — grouping by category — now it’s how you find who the model fails.
Next in 8.4 — this demonstration has a name, three sources, and real-world cases.
Returns in 8.11 — Practice PT #2 uses the same four required parts.
← 8.2 Training Data Decides Everything