CBuilderProject· about 90 min· in the browser
Project 1: Teach a computer to recognise patterns
Build a fruit classifier from two measurements (weight, colour value) entirely by hand: features, distance, nearest neighbour, accuracy and error analysis.
Theory
Every fruit is a point (weight, colour). A new fruit gets the same label as its nearest neighbour. Accuracy is measured on fruits the model has not seen — and the mistakes it makes tell you more than the number.
Sub-tasks
- features —
features(frukt)→ [vikt, farg] (weight, colour) as numbers from a dict. - distance and nearest —
avstand(a, b)(Euclidean distance) andnarmaste(punkt, traning)→ the label of the nearest training fruit. - accuracy —
traffsakerhet(traning, test)→ fraction correct. - error analysis —
felanalys(traning, test)→ dict {(actual, guessed): count} for the mistakes — which fruits get confused?
Passes when: accuracy >= 0.8
The starter code
runs in your browserfrom data import TRANING, TEST
def features(frukt):
# TODO: [frukt["vikt"], frukt["farg"]]
...
def avstand(a, b):
# TODO: euklidiskt avstånd mellan två featurelistor
...
def narmaste(punkt, traning):
# TODO: etiketten hos den träningsfrukt vars features ligger närmast punkt
...
def traffsakerhet(traning, test):
# TODO: andel av test där narmaste(features(f), traning) == f["etikett"]
...
def felanalys(traning, test):
# TODO: dict {(ratt, gissat): antal} bara för felen
...
You write the code; tests you cannot see decide whether it holds up. Create a free account to run the lab.
Try the diagnosticCreate a free accountExpected results
Accuracy ≥ 0.8 on the test fruits; the error analysis shows that apples and pears are confused most often (they have similar weights).
Common mistakes
- Compares colour (0–1) and weight (grams) without thinking about scale — the weight dominates the distance. Try scaling!
- Counts the test fruit itself among the neighbours.