Anomaly Scores
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 37 of 51.
A yes or no hides how odd a row is. score_samples gives every row a score: the lower, the stranger. For a pharmacy's prescriptions:
forest = IsolationForest(random_state=0).fit(X)
rx["score"] = forest.score_samples(X)
print(rx.sort_values("score").head(3))This prints the three strangest:
pills refills price score
17 300 0 120.0 -0.881478
88 28 11 11.5 -0.708524
120 30 1 64.0 -0.700620Where to cut is your choice: how many rows can a person check? Sort to rank, compare with a threshold to flag.
Challenge
MediumAn auditor has time for only a few expense claims. The starter fits an isolation forest; the program reads n and a threshold.
- Score every claim into a new column
score. - Print the
nmost unusual claims, strangest first, as below. - Print how many claims score below the threshold.
For another company, 2 and -0.6:
Most unusual claims:
C003: 1960 EUR, score -0.763
C040: 80 EUR, score -0.757
Below -0.6: 6 claimsTry it yourself
import pandas as pd
from sklearn.ensemble import IsolationForest
# One row per expense claim: amount, receipts, the hour it was filed and days after the trip
claims = pd.read_csv("claims.csv")
X = claims[["amount_eur", "items", "hour", "days_late"]]
n = int(input())
threshold = float(input())
# From Isolation Forest: the forest, fitted on every claim
forest = IsolationForest(random_state=0).fit(X)
# 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