How to use Isolation Forest
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction to Isolation Forest
Isolation Forest is a machine learning algorithm used for anomaly detection. It is particularly effective in identifying outliers within a dataset and is widely appreciated for its efficiency and scalability. The algorithm is based on the simple yet powerful concept that anomalous data points are less frequent and more susceptible to isolation.
Core Concept
Isolation
The underlying principle of the Isolation Forest algorithm is that anomalies are less frequent and more easily isolated than normal instances. In a tree structure, since anomalies require fewer splits to be isolated from the rest of the data points, they can be identified with lesser computational effort.
Isolation Trees
An Isolation Forest consists of multiple isolation trees. Each tree is constructed by randomly selecting a feature and then randomly selecting a split value within the feature's range. This process continues recursively until each instance in the dataset is isolated from the rest. The path length of an instance (i.e., the number of splits required to isolate the instance) is used to compute an anomaly score.
Algorithm Steps
- Generate Subsamples: To ensure the algorithm is robust and scalable, a random subsample of the given data is selected to build each isolation tree.
- Construct Isolation Trees: For each tree, a feature and a split value are selected randomly. This process continues until every instance in the subsample is isolated, thus completing the tree.
- Compute Anomaly Score: For each instance fed into the forest, its path length across all the trees is collected. The anomaly score is then calculated based on the averaged path lengths. Shorter paths indicate higher likelihoods of being anomalies.
- Aggregate Results: The scores from all trees are aggregated to provide a final anomaly score for each instance. A threshold is defined to classify an instance as either an anomaly or a normal observation.
Technical Explanation
Path Length and Anomaly `Score`
The anomaly score is derived from the average path length of a data point when traversed through the isolation forest. Given that shorter path lengths correspond to anomalies, the score is computed using the formula:
Where:
• is the anomaly score. • is the expected path length of a point `x`. • is the normalization constant for `n` being the sample size.
Normalization Constant
The normalization factor is used to standardize path lengths and is calculated as:
Here, represents the harmonic number, which approximates , where is the Euler-Mascheroni constant.
Example Implementation
Here's a basic example of using the `IsolationForest` from the `sklearn.ensemble` library:
• `n_estimators`: The number of base estimators in the ensemble. More trees mean more robust results. • `max_samples`: The number of samples used to build each isolation tree. A smaller subset can speed up training but may affect accuracy. • `contamination`: This float value represents the expected proportion of anomalies in the dataset. It helps in setting the threshold for classifying outliers. • `max_features`: The number of features to consider while splitting a node. • Efficiency: Due to its linear time complexity, isolation forest scales well with large datasets. • No Need for Distance Measures: Unlike other anomaly detection methods, it does not rely on distance or density metrics. • Fraud Detection: Identifying irregular transactions in banking. • Network Security: Detecting intrusions by spotting unusual patterns. • Manufacturing: Identifying defects during production.
Related reading
- How to use k-fold cross validation in a neural network
- How to use KBinsDiscretizer to make continuous data into bins in Sklearn?
- How to use Keras TensorBoard callback for grid search
- How to use Keras Variational Autoencoder example with text data
- How to use Naive Bayes in TensorFlow?
- How to use numpy functions on a keras tensor in the loss function?
- How to use Keras with GPU?
- How to use K.get_session in Tensorflow 2.0 or how to migrate it?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.