THINK FIRST·CODE LATER

← All labs

Sampling can delete the outliers

Problem

Input: fraction F, seed S, then a CSV sensor,pm25. Outliers are readings above Q3 + 1.5 × IQR.

  1. Print 200 readings, 4 outliers above 51.
  2. Draw a random sample of round(F × n) readings without replacement with rng = np.random.default_rng(S) and rng.choice(n, size, replace=False) (rows kept in original order). Print random 10% sample: 20 readings, outliers kept 0 of 4, max 45 (true max 140).
  3. With the same generator (next call), keep all outliers plus a random round(F × number of normal readings) of the normal ones: outliers + 10% of the rest: 24 readings, outliers kept 4 of 4, max 140.
  4. Print mean pm25: all 36.1, random sample 34.4.

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-4-data-1.txt — first 14 lines of 203

fraction 0.1
seed 0
sensor,pm25
S0,37
S1,29
S2,40
S3,41
S4,23
S5,27
S6,36
S7,33
S8,35
S9,30
S10,40
What your program must print for v11-4-data-1.txt
200 readings, 4 outliers above 51
random 10% sample: 20 readings, outliers kept 0 of 4, max 45 (true max 140)
outliers + 10% of the rest: 24 readings, outliers kept 4 of 4, max 140
mean pm25: all 36.1, random sample 34.4

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

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

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