Local Outlier Factor
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 36 of 51.
Some odd points hide inside the data's range. The local outlier factor, LOF, compares how crowded each point's spot is with its neighbors' spots:
Shoppers stay 6 hours sometimes. But this car's neighbors are commuters, packed tight, and it sits far from their crowd:
from sklearn.neighbors import LocalOutlierFactor
lof = LocalOutlierFactor(n_neighbors=20, contamination=0.02)
flags = lof.fit_predict(X)Same answers as before: -1 odd, 1 normal. n_neighbors sets how local the comparison is.
Challenge
MediumA phone company looks for accounts unlike their neighbors. The starter scales the data and flags accounts with an isolation forest; the program reads k.
- Flag accounts with LOF:
n_neighbors=k,contamination=0.03. - Print each detector's count and how many accounts both flag.
- List the accounts only LOF flags, in table order, as below.
For a smaller company and 10:
Isolation forest: 5 accounts
LOF: 5 accounts
Both: 4 accounts
Only LOF:
A118: 960 minutes, 6.2 GBTry it yourself
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
# One row per phone account: a month's call minutes and mobile data
accounts = pd.read_csv("accounts.csv")
X = StandardScaler().fit_transform(accounts[["call_minutes", "data_gb"]])
k = int(input())
# From Isolation Forest: -1 for the accounts the forest finds odd
forest_flags = IsolationForest(contamination=0.03, random_state=0).fit_predict(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