scikit-learn / scikit-learn/scikit-learn

ENH: Random Forest Classifier oob scaling/parallel

Open
#28,059 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement
Dominant language
Python
Stars
67.3k
Forks
27.4k
Avg merge
1d 15h
Merged PRs (30d)
58

Description

My team, working on a bioinformatics problem with high feature count (columns/dimensions in X), noticed that the RandomForestClassifier out of bag scoring doesn't scale with n_jobs. To be fair, n_jobs clearly says what it does support, though I do wonder if the out of bag predictions under the hood might also benefit from parallel support. Someone on my team seems to have found that it does help, but implemented externally to sklearn using the exposed base estimators. I suppose it might be nice to have that internally at some point, if there are no design reasons not to?

Sample reproducer code with latest stable release (1.3.2) on 16 cores/x86_64 Linux box (i9-13900K) is below the fold, and the scaling plot is underneath that. We also use far more estimators and features than that, so the delta is much greater, but the scaling trend is the main observation in any case.

from time import perf_counter
import numpy as np
# sklearn 1.3.2
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt


timings_oob = []
timings_base = []
feature_counts = np.linspace(10, 180_000, 20, dtype=np.int64)

for feature_count in feature_counts:
    X, y = make_classification(n_samples=1_000,
                               n_features=feature_count,
                               random_state=0)
    for use_oob, timing_list in zip([True, False], [timings_oob, timings_base]):
        start = perf_counter()
        clf = RandomForestClassifier(n_estimators=50,
                                     random_state=0,
                                     oob_score=use_oob,
                                     n_jobs=16)
        clf.fit(X, y)
        timing_list.append(perf_counter() - start)

fig, ax = plt.subplots(1, 1)
ax.set_title(f"Random Forest OOB scaling performance")
ax.plot(feature_counts,
        timings_oob,
        label="WITH OOB",
        marker=".")
ax.plot(feature_counts,
        timings_base,
        label="NO oob",
        marker=".")
ax.set_xlabel("num features")
ax.set_ylabel("Time (s)")
ax.legend()
fig.savefig("bench_feat.png", dpi=300)

image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from RandomForestClassifier.fit with oob_score=True and n_jobs set, using the issue's reproducer to compare OOB and non-OOB timing as feature counts grow. Read the existing OOB scoring path and its parallelization behavior; done means an agreed implementation or documented design decision, with tests or benchmarks showing the expected n_jobs scaling.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, scikit-learn
Domain
machine-learning, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.