learning-it by example

#194 Machine learning and data science 6.4 min

Anomaly Detection Basics: z-Score, IQR, Isolation Forest

A z-score found 1 of 3 planted bad readings; a median-based check found all 3.

1. In 200 made-up temperatures with 3 planted bad rows, how many did the plain z-score check (more than 3 away) find?
2. Why did the glitch reading of 250 hide the other two bad temperatures from the z-score?
3. What does the modified z score use in place of the mean and the standard deviation?
4. On 300 made-up machines with 4 planted odd rows, what did the isolation forest with contamination 0.02 flag?
5. Rows 90 and 91 have a normal temperature and a normal vibration when each column is looked at alone. Why could the column-by-column z-score not find them?
6. What does the video say to do with rows that a check has flagged as outliers?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Predict a smaller glitch

    Copy the code folder first. In data.py change temp[17] = 250.0 to temp[17] = 100.0. Before you run, predict how the mean and standard deviation on the first line will change and whether the plain z-score will now find more than row 17. Then run python run.py one and compare.

    A hint

    A smaller wild value drags the mean and the standard deviation less.

  2. Stricter box plot fences

    In run.py, in the one function, change iqr_flags(x) to iqr_flags(x, 3.0), which uses fences 3 interquartile ranges out instead of 1.5. Predict whether row 174, the false alarm, will still be flagged. Run python run.py one to check.

    A hint

    The video says NIST calls points beyond 1.5 ranges mild outliers and beyond 3 ranges extreme ones.

  3. Ask the forest for fewer rows

    In run.py, in the forest function, change contamination=0.02 to contamination=0.01. Predict how many rows the isolation forest will flag out of 300, and whether all 4 planted rows are still among them. Run python run.py forest and compare.

    A hint

    The video says contamination asks it to flag about that share of the rows, and that you choose it.

  4. Plant your own bad row

    In data.py, in make_readings, add the line temp[100] = 70.0 after the other planted rows. Predict which of the three checks (z-score, modified z, IQR fences) will flag row 100. Run python run.py one and read the rows listed on each line.

    A hint

    Remember the z-score cut is 3 standard deviations, and the glitch at row 17 still inflates the standard deviation.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

Made-up data with planted anomalies, one seed for the isolation forest, and a contamination value chosen by hand.

Up next in Machine learning and data science

More in Machine learning and data science

All Machine learning and data science videos

Related videos