THINK FIRST·CODE LATER

← All labs

How bin width changes a histogram

Problem

Input: a line steps W1 W2 … (bin widths to try), then a CSV with a column duration_min.

Print n=16, min 4, max 31, mean 16.9, median 11.5. For each width w: bins start at ⌊min/w⌋·w and are half-open [a, a+w); use ⌊(max − start)/w⌋ + 1 bins (edges start + w * np.arange(nbins + 1)) and np.histogram; print step 5: [0,5):1 [5,10):5 … and peaks: 2 -> bimodal or multimodal (or unimodal), where a peak is a non-empty bin larger than its left neighbour (or first) and at least as large as its right neighbour (or last).

Finally build an Altair histogram with the last width (alt.Bin(step=w), y = count()) and print altair bin: {'step': 5.0} | y: count.

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.

v7-1-data-1.txt — first 14 lines of 18

steps 20 5
duration_min
4
6
7
8
8
9
10
11
12
24
26
27
What your program must print for v7-1-data-1.txt
n=16, min 4, max 31, mean 16.9, median 11.5
step 20: [0,20):9 [20,40):7
  peaks: 1 -> unimodal
step 5: [0,5):1 [5,10):5 [10,15):3 [15,20):0 [20,25):1 [25,30):4 [30,35):2
  peaks: 2 -> bimodal or multimodal
altair bin: {'step': 5.0} | y: count

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

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

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