๐๐๐ผ๐น๐ฎ๐๐ถ๐ผ๐ป ๐๐ผ๐ฟ๐ฒ๐๐: ๐๐ป๐ผ๐บ๐ฎ๐น๐ ๐๐ฒ๐๐ฒ๐ฐ๐๐ถ๐ผ๐ป ๐๐ ๐๐๐ธ๐ถ๐ป๐ด, โ๐๐ผ๐ ๐๐ฎ๐๐ ๐๐ ๐ง๐ต๐ถ๐ ๐ฃ๐ผ๐ถ๐ป๐ ๐ง๐ผ ๐๐๐ผ๐น๐ฎ๐๐ฒ?โ
Many anomaly detection algorithms try to model what normal data looks like and identify deviations.
Isolation Forest takes a different approach: instead of explicitly modeling normality, it measures how easily a data point can be isolated from the rest of the dataset.
The key intuition is simple:
Anomalies are easier to isolate. Normal points are harder to isolate.
๐น ๐๐ผ๐ ๐๐๐ผ๐น๐ฎ๐๐ถ๐ผ๐ป ๐๐ผ๐ฟ๐ฒ๐๐ ๐ช๐ผ๐ฟ๐ธ๐
๐ญ. ๐ฅ๐ฎ๐ป๐ฑ๐ผ๐บ ๐ฆ๐ฝ๐น๐ถ๐๐๐ถ๐ป๐ด
Each tree is built using a small, randomly selected subset of the data. It then repeatedly:
- Randomly selects a feature.
- Randomly selects a split value between that featureโs minimum and maximum within the current node.
Unlike traditional decision trees, these splits are not optimized using criteria such as Gini impurity or entropy. They are purely random.
๐ฎ. ๐ช๐ต๐ฒ๐ป ๐๐ผ๐ฒ๐ ๐ฆ๐ฝ๐น๐ถ๐๐๐ถ๐ป๐ด ๐ฆ๐๐ผ๐ฝ?
A branch stops growing when:
- The node contains a single data point.
- The points in the node have identical feature values.
- The maximum tree depth is reached.
๐ฏ. ๐ฃ๐ฎ๐๐ต ๐๐ฒ๐ป๐ด๐๐ต: ๐ง๐ต๐ฒ ๐๐ฒ๐ ๐ฆ๐ถ๐ด๐ป๐ฎ๐น
Path length is the number of splits required to isolate a data point.
- Anomalies tend to exist in sparse regions, so they are often isolated quickly, resulting in shorter paths.
- Normal points tend to belong to dense regions, requiring more splits to separate them from neighboring points.
๐ฐ. ๐๐๐ฒ๐ฟ๐ฎ๐ด๐ถ๐ป๐ด ๐๐ฐ๐ฟ๐ผ๐๐ ๐ง๐ต๐ฒ ๐๐ผ๐ฟ๐ฒ๐๐
A single tree can produce noisy results depending on its random splits.
Isolation Forest therefore builds many randomized trees and calculates the average path length for each data point.
This average is then converted into an anomaly score:
- Shorter average path โ higher anomaly likelihood
- Longer average path โ lower anomaly likelihood
๐ก ๐ช๐ต๐ ๐๐๐ผ๐น๐ฎ๐๐ถ๐ผ๐ป ๐๐ผ๐ฟ๐ฒ๐๐ ๐๐ ๐จ๐๐ฒ๐ณ๐๐น
- It does not require labeled anomalies.
- Random subsampling can help reduce the effects of masking and swamping.
- Trees can be built independently, making the method highly parallelizable.
- It does not require a specific probability distribution for normal data.
- It can efficiently identify unusual observations in high-dimensional datasets.
๐ ๐ฆ๐๐๐ฃ: ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ถ๐ป๐ด ๐ ๐ผ๐ฑ๐ฒ๐น ๐ฃ๐ฟ๐ฒ๐ฑ๐ถ๐ฐ๐๐ถ๐ผ๐ป๐
After detecting an anomaly, the next question is:
โWhy did the model flag this particular data point?โ
That is where SHAP โ SHapley Additive exPlanations โ can help.
Get Affan Ahmedโs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
SHAP is based on Shapley values from cooperative game theory, where a total payout is distributed among players according to their contributions.
In machine learning:
- The players are the features.
- The payout is the modelโs output.
- The SHAP values represent each featureโs contribution to that output.
๐น ๐๐ผ๐ ๐ฆ๐๐๐ฃ ๐ช๐ผ๐ฟ๐ธ๐
Conceptually, SHAP evaluates how a feature changes a modelโs output when added to different combinations of other features.
It considers possible feature subsets, measures the featureโs marginal contribution, and averages those contributions using Shapley-value weighting.
The result is a SHAP value for each feature and each prediction.
๐น ๐๐ฒ๐ ๐ฃ๐ฟ๐ผ๐ฝ๐ฒ๐ฟ๐๐ถ๐ฒ๐
- Additive: The baseline output plus the SHAP values sum to the modelโs output, within the chosen SHAP explanation framework.
- Local: SHAP explains individual predictions, so different observations can have different feature contributions.
- Global insights: Aggregating the absolute SHAP values across observations can provide an overview of feature importance.
๐ก ๐ช๐ต๐ ๐ง๐ต๐ถ๐ ๐ ๐ฎ๐๐๐ฒ๐ฟ๐
For models such as XGBoost, Random Forest, or Isolation Forest, SHAP can help identify which features influenced a particular output.
For many tree-based models, optimized approaches such as TreeSHAP can make explanations considerably faster.
However, the computational cost and available explanation methods depend on the specific model and SHAP implementation.
For example:
โThis observation received a high anomaly score partly because of unusual values in features X and Y.โ
Remember: SHAP explains the modelโs behavior, not necessarily the real-world cause of an anomaly.
๐ ๐ง๐ต๐ฒ ๐๐ถ๐ด ๐ฃ๐ถ๐ฐ๐๐๐ฟ๐ฒ
Isolation Forest helps identify unusual data points.
SHAP helps explain which features influenced the modelโs output.
Together, they provide a useful workflow:
Detect the anomaly โ Investigate the contributing features โ Understand the modelโs reasoning.





