Implement isolation_forest(X, n_trees, sample_size, random_state=42) for a 2-D array X of shape (n_samples, n_features). Return a 1-D array with one anomaly score per row (unrounded; higher means more anomalous).
Randomness comes from the global generator in exactly this order:
np.random.seed(random_state) once at the start.n_trees trees in turn: idx = np.random.choice(n_samples, size=min(sample_size, n_samples), replace=False), then grow a tree on X[idx] with max_depth = ceil(log2(max(sample_size, 2))).max_depth or it is a leaf of size . Otherwise draw f = np.random.randint(n_features); if feature f is constant on the node's points it becomes a leaf of size (no further draw). Otherwise draw split = np.random.uniform(min, max) of that feature, send points with value < split left and the rest right, and build the left child before the right child.Scoring: a point's path length in a tree is the depth of the leaf it reaches plus for that leaf's size , where for and otherwise. With the mean path length over all trees:
(use 1 in place of when it is 0).