Flag outliers two ways and catch the error one of them misses
Task
Apply the interquartile range rule to the order quantities, then a validation rule, and compare what each catches. The point is the value one method misses.
Steps
- Read the quantities from
data/orders.csv. Calculate Q1 and Q3 with Python'sstatistics.quantiles(values, n=4, method='inclusive')and the IQR fences at 1.5 times the IQR. - Record in
lab/outliers/iqr.txtthe lineslower fence: N,upper fence: N,above: Nandbelow: N. - Apply the validation rule quantity > 0, and record the order_ids that fail it in
lab/outliers/invalid.csvwith headerorder_id,quantity. - Record in
lab/outliers/missed.txtany invalid order the IQR rule did not flag, asmissed: <order_id>.
Verify
These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.
grep -E '^(lower fence|upper fence|above|below): ' lab/outliers/iqr.txt
awk -F, 'NR>1 {n++} END {print n" invalid order(s)"}' lab/outliers/invalid.csv
grep -c '^missed: ' lab/outliers/missed.txt
Fences of -3.5 and 8.5; 6 orders above (the genuine bulk orders) and 2 below. The validation rule finds 3 invalid orders, and 1 of them -- a quantity of -3 -- is missed by the IQR rule because it is not unusual enough. Statistical rules find the unusual; validation rules find the impossible.
This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.