Extra-Trees Classifier With Random Thresholds
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `draw_random_thresholds(X_node, features, rng)` and an `ExtraTreesScratch` classifier. Each tree is grown to purity on all training rows (no bootstrap), splitting on random features at random thresholds.
The class is constructed as `ExtraTreesScratch(n_estimators=10, max_features="sqrt", min_samples_split=2, random_state=0)`. It exposes `fit(X, y)`, which returns `self`, plus `predict_proba_per_tree(X)`, `predict_proba(X)`, `predict(X)` and the attribute `classes_`.
Requirements:
- `draw_random_thresholds` returns one threshold per feature, in the order of `features`, drawn with `rng.uniform` in [min, max) of that feature over the rows of `X_node`.
- At each node, sample `k` distinct non-constant features. `max_features` sets k: `None` means all p features, `"sqrt"` means `max(1, int(sqrt(p)))`, an int is a count and a float is a fraction of p (both kept between 1 and p). If fewer than k features are non-constant, use all of them. Draw one threshold per sampled feature, keep the split with the lowest weighted Gini impurity, and send rows with `x <= t` to the left child.
- A node becomes a leaf when it is pure, has fewer than `min_samples_split` rows, has only constant features, or none of its candidate splits puts rows on both sides. A leaf stores the class-frequency vector of its rows.
- `predict_proba_per_tree` returns an array of shape `(n_estimators, n_samples, n_classes)`, `predict_proba` is its mean over trees, and `predict` returns `classes_[argmax]`, so ties go to the lowest label.
- `classes_` holds the sorted unique labels. Seed a single `np.random.default_rng(random_state)` so that equal seeds reproduce the same forest.
Example:
```python
X = [[0, 0], [0, 0], [1, 1]]
y = [7, 3, 7]
m = ExtraTreesScratch(n_estimators=4, max_features=None).fit(X, y)
m.predict_proba([[0, 0], [1, 1]]) # [[0.5, 0.5], [0.0, 1.0]]
m.predict([[0, 0], [1, 1]]) # [3, 7]
```
Here `classes_` is `[3, 7]`. Any threshold in [0, 1) sends the two `[0, 0]` rows left and the `[1, 1]` row right. The left node holds identical rows labeled 3 and 7, so it is a leaf with frequencies `[0.5, 0.5]`, and every tree comes out the same whatever the seed. The tie in the first prediction goes to the lower label, 3.
```hint Why splits make progress
Check which side the row holding a feature's minimum and the row holding its maximum can land on when the threshold is drawn in [min, max). Then think about what floating-point rounding could do to that guarantee.
```
```hint Per-tree probabilities
A subtree may never see some classes. Make every stored frequency vector the same width, indexed by position in `classes_`, so the per-tree arrays stack.
```
### Constraints & Assumptions
- `X` is numeric with shape (n, p), and `y` holds n labels of any sortable type.
- Trees have no depth limit; `min_samples_split` is the only size-based stopping rule.
### What a Strong Answer Covers
- Drawing one threshold per feature over the node's rows, and keeping constant features out of the candidates.
- Resolving each accepted form of `max_features` and sampling distinct features at every node.
- Choosing among the random candidates by weighted Gini, and all four leaf conditions.
- Output shapes, averaging over trees, the tie rule in `predict`, and reproducibility from one seeded generator.
- Fit and predict cost compared with a tree that searches every threshold.
### Follow-up Questions
- Why does Extra-Trees skip bootstrapping while Random Forest uses it?
- How does the bias-variance trade-off compare to Random Forest?
- How would you add feature importances?
Overview: Build an Extra-Trees classifier from scratch with NumPy: grow unpruned trees on the full training set, pick random features and uniform random thresholds, and choose among them by Gini impurity. Tests tree construction, stopping rules, probability averaging and reproducible seeding.