THINK FIRST·CODE LATER

← All labs

Normalize and classify for a choropleth

Problem

Input: a line classes K, then a CSV district,cases,population.

Compute rate = cases / population × 10,000. Classify the rates with K equal-interval classes (breaks np.linspace(min, max, K + 1)) and K quantile classes (breaks np.quantile(rates, np.linspace(0, 1, K + 1))), assigning classes with np.digitize(rate, breaks[1:-1], right=True). For each method print the class boundaries (1 decimal) and the districts in each class (alphabetical, - if empty):

equal interval (rate): 1.8-6.2 | 6.2-10.6 | 10.6-15.0
   class 1: Dongtou, Lucheng, Ouhai, Ruian, Yueqing; class 2: -; class 3: Longwan

Then most cases: Lucheng; highest rate per 10,000: Longwan and, if they differ, a map of raw counts would point to the wrong district.

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.

v10-2-data-1.txt

classes 3
district,cases,population
Lucheng,420,700000
Ouhai,150,450000
Longwan,300,200000
Dongtou,20,90000
Ruian,360,1200000
Yueqing,250,1400000
What your program must print for v10-2-data-1.txt
equal interval (rate): 1.8-6.2 | 6.2-10.6 | 10.6-15.0
   class 1: Dongtou, Lucheng, Ouhai, Ruian, Yueqing; class 2: -; class 3: Longwan
quantile (rate): 1.8-2.7 | 2.7-4.2 | 4.2-15.0
   class 1: Dongtou, Yueqing; class 2: Ouhai, Ruian; class 3: Longwan, Lucheng
most cases: Lucheng; highest rate per 10,000: Longwan
a map of raw counts would point to the wrong district

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 < v10-2-data-1.txt

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

Get-Content v10-2-data-1.txt | 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("v10-2-data-1.txt", 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.