Menu
CoddyTech

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:

Cars in a parking garage, arrival time across and hours parked up: commuters packed tight near 8:00 and 9 hours, shoppers spread all day for a few hours, and one car at 8:18 parked 6.6 hours circled red, flagged by LOF

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 icon

Challenge

Medium

A phone company looks for accounts unlike their neighbors. The starter scales the data and flags accounts with an isolation forest; the program reads k.

  1. Flag accounts with LOF: n_neighbors=k, contamination=0.03.
  2. Print each detector's count and how many accounts both flag.
  3. 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 GB

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler