Finding the Odd Ones
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 34 of 51.
This chapter hunts odd rows. By its end, you will circle a server's strange minutes:
An anomaly is a rare row unlike the rest: a fault, a fraud, a typo. Each new one differs, so nobody can label them ahead.
Start with z-scores. StandardScaler turns every column into z-scores, so one line flags rows with any value past 3:
z = StandardScaler().fit_transform(X)
odd = (np.abs(z) > 3).sum(axis=1) > 0But a row can be ordinary in each column and odd only in its mix. Column z-scores miss it.
Challenge
MediumA supermarket logged a day's averages for its 80 freezer cases. Which need a technician? The program reads a cutoff.
- Turn every column of
Xinto z-scores. - For each column, print how many freezers it flags: z-score more than the cutoff from 0.
- Print every freezer flagged by any column, in table order.
For another store and 3:
temp_c: 1 flagged
power_kw: 2 flagged
door_openings: 0 flagged
defrost_min: 0 flagged
Freezers to check:
F05
F13
F51Try it yourself
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
# One row per freezer case: a day's averages
freezers = pd.read_csv("freezers.csv")
columns = ["temp_c", "power_kw", "door_openings", "defrost_min"]
X = freezers[columns]
cutoff = float(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd Ones8Anomaly Detection
Finding the Odd OnesIsolation ForestLocal Outlier FactorAnomaly ScoresRecap: Server WatchPractice on your own: Online Python compiler