Menu
CoddyTech

Isolation Forest

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 35 of 51.

Odd points are easy to cut off. Slice the data at random, again and again, until one point stands alone:

The same scatter twice, sliced by gray random cuts. Left: a far point in the top corner stands alone after 2 cuts. Right: a point in the middle of the crowd needs 8 cuts

The far point needed 2 cuts, the crowded one 8. An isolation forest averages the cuts over 100 random trees:

from sklearn.ensemble import IsolationForest
forest = IsolationForest(contamination=0.02, random_state=0)
flags = forest.fit_predict(X)

fit_predict gives -1 for odd rows, 1 for the rest. contamination is the share of rows you expect to be odd.

challenge icon

Challenge

Medium

A port logged 160 crane shifts: moves per hour and energy per move. Which shifts were strange?

  1. Flag the shifts with an isolation forest, contamination=0.03, random_state=0. Print the count as below.
  2. Scatter the normal shifts as dots labelled normal.
  3. Circle the flagged ones: s=150, facecolors="none", edgecolors="red", labelled flagged. Add a legend and show.

For another port, with 200 shifts:

Flagged: 6 of 200 shifts

Crane shifts as dots in a falling cloud, faster cranes using less energy per move, and six lonely shifts circled in red around it, with a legend

Coddy then describes your chart.

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import IsolationForest

# One row per crane shift at the port: speed and energy use
shifts = pd.read_csv("cranes.csv")
X = shifts[["moves_per_hour", "kwh_per_move"]]

# Don't change the lines below
fig, ax = plt.subplots()
ax.set_title("Crane Shifts")
ax.set_xlabel("Moves per hour")
ax.set_ylabel("Energy per move (kWh)")

# 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