THINK FIRST·CODE LATER

← All labs

Clustering stations as aggregation

Problem

Input: a line k K, then the stations CSV (station + numeric attributes).

Standardize the attributes. For k = 1…4 print k=2: within-cluster sum of squares 6.66 using KMeans(n_clusters=k, n_init=10, random_state=0). Then cluster with K and describe the groups, numbered by size (largest first; ties by the alphabetically first station name): group 1 (4 stations): Airport, Beach, Park, Riverside and mean trips_per_day 105.00, mean_min 29.25, casual_share 0.61, km_to_centre 7.33 (means of the original, unscaled attributes, 2 decimals).

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.

v11-3-data-1.txt

k 2
station,trips_per_day,mean_min,casual_share,km_to_centre
North Gate,420,9,0.10,1.2
Library,380,8,0.12,0.8
City Hall,450,10,0.08,0.5
Riverside,120,28,0.65,4.5
Park,150,32,0.70,3.8
Beach,90,35,0.80,9.0
Airport,60,22,0.30,12.0
Station Sq,300,12,0.20,2.0
What your program must print for v11-3-data-1.txt
k=1: within-cluster sum of squares 32.00
k=2: within-cluster sum of squares 6.66
k=3: within-cluster sum of squares 2.34
k=4: within-cluster sum of squares 0.97
group 1 (4 stations): Airport, Beach, Park, Riverside
   mean trips_per_day 105.00, mean_min 29.25, casual_share 0.61, km_to_centre 7.33
group 2 (4 stations): City Hall, Library, North Gate, Station Sq
   mean trips_per_day 387.50, mean_min 9.75, casual_share 0.12, km_to_centre 1.12

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 < v11-3-data-1.txt

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

Get-Content v11-3-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("v11-3-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.