liveBook · Manning

Decision tree

As explained in Outlier Detection in Python

A decision tree is a hierarchical predictive model that recursively partitions observations using feature-based decision rules.

A decision tree makes predictions by following a hierarchy of feature-based rules. A small tree can be read as a set of conditions leading to a prediction; a large one can become difficult to interpret. For outlier detection, a tree can either distinguish real records from synthetic ones or serve as an interpretable proxy for another detector. section 3.2.5 section 13.2.2

Two uses call for different targets:

  • Real versus fake: generate synthetic rows, label their origin, and train a classifier. Real rows predicted to be fake are candidates for closer examination. Results depend on both the synthetic-data method and the tree settings. section 3.2.5
  • Detector proxy: train a regression tree to approximate outlier scores, or a classifier to approximate labels obtained by thresholding those scores. Compare its predictions with the detector before treating its rules as an explanation. section 13.2.2

Real versus synthetic breast-cancer records

Synthetic rows are sampled separately for each feature using that feature’s mean and standard deviation. The resulting values can look plausible individually without preserving associations between features. With maximum depth set to seven, the tree classifies 544 of 569 real rows as real and 561 of 569 synthetic rows as fake; it flags 25 real rows as fake. These are outlier candidates, not automatically meaningful anomalies. section 3.2.5

Listing 13.5 Using a classifier to predict a binary form of the detector score
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
 
clf = DecisionTreeClassifier(random_state=0)
 
df['IF Binary'] = df['IF Score'] < -0.60                           #1
 
cv_results = cross_validate(clf, df[orig_features], df['IF Binary'], cv=3, 
                            scoring='f1_macro')                    #2
print(cv_results)
 
clf = DecisionTreeClassifier(random_state=0, max_leaf_nodes=10)    #3
cv_results = cross_validate(clf, df[orig_features], df['IF Binary'], cv=3, 
                            scoring='f1_macro')
print(cv_results)
 
clf = DecisionTreeClassifier(random_state=0, max_leaf_nodes=10)    #4
clf.fit(df[orig_features], df['IF Binary'])
df['DT Binary Prediction'] = clf.predict(df[orig_features])
print(tree.export_text(clf, feature_names=orig_features))

The classifier proxy converts Isolation Forest scores below −0.60 into binary labels, compares unrestricted and ten-leaf trees with cross-validation, and exports the fitted rules. section 13.2.2

Tree size is a practical trade-off. Restricting a score-predicting proxy to ten leaves keeps it more readable but limits it to ten distinct predicted scores. For a binary proxy, inspecting exported paths and the labels within each leaf can reveal groups associated with different kinds of outlier predictions. A readable tree remains an approximation of the detector, not its underlying mechanism. section 13.2.2

How Outlier Detection in Python covers it

How deep it goes
Works through synthetic-data classification and proxy fitting with evaluation, but does not derive tree-splitting internals.
Examples
Uses breast-cancer records for real-versus-fake classification and feature-threshold rules for an Isolation Forest proxy.
What it assumes
Assumes tabular features and, for proxy modeling, scores from an existing detector; demonstrates fitting rather than deriving tree construction.
What you can do after
Apply it via scikit-learn to identify real-versus-synthetic outlier candidates or approximate a detector’s scores and binary labels.
How it connects
Connects decision trees to synthetic data, binary classification, Isolation Forest, outlier scores, and rule sets.

Also explained in

Compare how 13 books explain decision tree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.