THINK FIRST·CODE LATER

← All labs

Confusion matrix and per-group performance

Problem

Input: a CSV actual,predicted,group (1 = the station became/was predicted empty). Using scikit-learn (confusion_matrix(..., labels=[0, 1]), precision_score, recall_score, accuracy_score with zero_division=0), print:

confusion matrix (rows actual 0/1, columns predicted 0/1):
   11   1
    2   6
accuracy 0.85, precision 0.86, recall 0.75
  central: n=10, positives=4, recall 1.00, accuracy 0.90
  peripheral: n=10, positives=4, recall 0.50, accuracy 0.80
recall gap between groups: 0.50  <- the model misses far more problems in one group

(each matrix row: two spaces, then counts right-aligned in 3 characters separated by a space; groups alphabetical; the warning suffix only if the gap > 0.2).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v13-1-data-1.csv — first 14 lines of 21

actual,predicted,group
1,1,central
1,1,central
1,1,central
1,1,central
0,0,central
0,0,central
0,0,central
0,0,central
0,0,central
0,1,central
1,1,peripheral
1,1,peripheral
1,0,peripheral
What your program must print for v13-1-data-1.csv
confusion matrix (rows actual 0/1, columns predicted 0/1):
   11   1
    2   6
accuracy 0.85, precision 0.86, recall 0.75
  central: n=10, positives=4, recall 1.00, accuracy 0.90
  peripheral: n=10, positives=4, recall 0.50, accuracy 0.80
recall gap between groups: 0.50  <- the model misses far more problems in one group

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v13-1-data-1.csv

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v13-1-data-1.csv | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v13-1-data-1.csv", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.