yobx.sklearn.ensemble.isolation_forest#

yobx.sklearn.ensemble.isolation_forest.sklearn_isolation_forest(g: GraphBuilderExtendedProtocol, sts: Dict, outputs: List[str], estimator: IsolationForest, X: str, name: str = 'isolation_forest') str | Tuple[str, str][source]#

Converts a sklearn.ensemble.IsolationForest into ONNX.

Algorithm overview

Each isolation tree assigns a sample a path-length contribution equal to depth(leaf) + c(n_leaf) - 1, where c(n) is sklearn.ensemble._iforest._average_path_length(). These contributions are averaged across trees to give an average isolation path length avg_depth. The anomaly score is then:

score_samples(X) = -2^(-avg_depth / c(max_samples))
decision_function(X) = score_samples(X) - offset_
predict(X) = 1 if decision_function(X) >= 0 else -1

ONNX graph structure

The path-length contributions are precomputed at conversion time and stored as leaf weights in a TreeEnsembleRegressor (opset ≤ 4) or TreeEnsemble (opset 5+) node. Feature indices are remapped from per-tree local indices to the original feature space via estimators_features_.

X ──TreeEnsembleRegressor(AVERAGE) / TreeEnsemble(SUM)──► avg_depth
        Reshape──► avg_depth (N,)
        Mul(-log(2)/c(max_samples))──► exponent (N,)
        Exp──► score (N,)          [= 2^(-avg/c)]
        Neg──► score_samples (N,)
        Sub(offset_)──► decision (N,)
        Where(>=0, 1, -1)──► label (N,)
Parameters:
  • g – the graph builder to add nodes to

  • sts – shapes and types defined by scikit-learn

  • outputs – desired output tensor names; two entries (label, scores) or one entry (label,)

  • estimator – a fitted IsolationForest

  • X – name of the input tensor

  • name – prefix used for names of nodes added by this converter

Returns:

label tensor name, or tuple (label, scores)