THINK FIRST·CODE LATER

← All labs

PCA: what standardization changes

Problem

Input: a line scaling standard or scaling none, then a CSV with a station column and numeric attributes.

Optionally standardize (StandardScaler), fit PCA(n_components=2), and print scaling: standard; explained variance: PC1 84.2%, PC2 14.3%, total 98.5%, then for each component PC1 loadings: trips_per_day +0.53, mean_min -0.52, … — flip the sign of a component so that its largest absolute loading is positive (PCA signs are arbitrary) — and PC1 is dominated by: trips_per_day (largest absolute loading of PC1).

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

scaling standard
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-2-data-1.txt
scaling: standard; explained variance: PC1 84.2%, PC2 14.3%, total 98.5%
PC1 loadings: trips_per_day +0.53, mean_min -0.52, casual_share -0.50, km_to_centre -0.45
PC2 loadings: trips_per_day -0.21, mean_min -0.34, casual_share -0.54, km_to_centre +0.74
PC1 is dominated by: trips_per_day

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

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

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