Right most of the time, and useless
Problem 29 predicted a number and judged it with R². This one predicts a category — is this student at risk of failing, yes or no — and judging it turns out to need more care than a single score can give.
Forty students, ten of them genuinely at risk. The model is trained on twenty-eight and tested on twelve, of whom three are at risk.
KNeighborsClassifier(n_neighbors=3) predicts a label rather than a number. To classify a new student it finds the three most similar students it has seen and goes with the majority.The idea is as simple as it sounds, and it is a reasonable first thing to try on almost any labelled data.
| predicted safe | predicted at risk | |
|---|---|---|
| really safe | TN | FP — a false alarm |
| really at risk | FN — a student missed | TP — caught |
confusion_matrix(y_test, predicted) prints them in that layout: truth down the side, predictions across the top. The order is truth first. Swap the arguments and the table comes back transposed, which turns every missed student into a false alarm and reads perfectly plausibly..ravel() flattens the four numbers into a row, reading left to right and top to bottom, so they come out as tn, fp, fn, tp. Unpack them in any other order and every line you write afterwards is confidently mislabelled — there is no error, only four numbers wearing each other's names.That is roughly what happens here. Watch the accuracy and the number caught in the same output: the accuracy looks respectable and the model found nobody. Whenever one outcome is much rarer than the other — fraud, disease, students at risk — accuracy quietly rewards ignoring it.
tp out of tp + fn, the bottom row of the table.Of the students we flagged, how many really were at risk? — that is
tp out of tp + fp, the right-hand column.They are different questions with different answers, and which one matters depends entirely on what it costs to miss somebody versus what it costs to raise a false alarm.
What you are building
Build the programme
Your plan
The steps still to place
The whole programme is laid out below. Five pieces are missing. Two stop the programme; the rest report numbers that are perfectly real and answer a question you did not ask.
Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.
Run and read
Here is the whole programme. One file, one fixed split, so the answer is the same every run. Read the accuracy and the last line together — they are describing the same model.