THINK FIRST·CODE LATER

← All labs

Anscombe's quartet: when statistics lie

Problem

Read a CSV from standard input with columns dataset,x,y (several datasets, in order of first appearance).

For each dataset print one line:

I: n=11, mean x 9.00, mean y 7.50, var x 11.00, var y 4.13, r 0.816, y = 3.00 + 0.500x

— the number of points, the means and sample variances (2 decimals), the Pearson correlation (3 decimals) and the least-squares line (intercept 2 decimals, slope 3 decimals). Then print Summary statistics identical (2 decimals): yes if all datasets have the same mean x and mean y when rounded to 2 decimals, otherwise no.

Use pandas (groupby, mean, var, corr) and numpy.polyfit.

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.

v1-1-data-1.csv — first 14 lines of 45

dataset,x,y
I,10,8.04
I,8,6.95
I,13,7.58
I,9,8.81
I,11,8.33
I,14,9.96
I,6,7.24
I,4,4.26
I,12,10.84
I,7,4.82
I,5,5.68
II,10,9.14
II,8,8.14
What your program must print for v1-1-data-1.csv
I: n=11, mean x 9.00, mean y 7.50, var x 11.00, var y 4.13, r 0.816, y = 3.00 + 0.500x
II: n=11, mean x 9.00, mean y 7.50, var x 11.00, var y 4.13, r 0.816, y = 3.00 + 0.500x
III: n=11, mean x 9.00, mean y 7.50, var x 11.00, var y 4.12, r 0.816, y = 3.00 + 0.500x
IV: n=11, mean x 9.00, mean y 7.50, var x 11.00, var y 4.12, r 0.817, y = 3.00 + 0.500x
Summary statistics identical (2 decimals): yes

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 < v1-1-data-1.csv

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

Get-Content v1-1-data-1.csv | 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("v1-1-data-1.csv", 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.