Skip to content

RLT for Python

Fit random forests for continuous responses, class labels, and right-censored survival times with a scikit-learn-style interface. rlt brings the tree algorithms of the RLT R package to Python.

For installation, examples, and the R interface, see the R version documentation.

The default forest uses one variable at each split. Optional reinforcement uses embedded forests to guide variable selection. Linear-combination splits split along a direction formed from several predictors and can be used with or without reinforcement.

Get started: install and fit a forest

Install from source

These guides describe the GitHub source version, 0.3.0. Install it from the source repository, using the installation instructions for shared Python dependencies and system libraries. The standard build targets Linux; Windows and macOS need additional compiler and linker configuration.

Predict a continuous response

After installation, fit on 160 simulated observations and predict the 40 held out before fitting. The response depends on the first two predictors plus noise.

import numpy as np
from rlt import RLT_reg

rng = np.random.default_rng(1)
X = rng.normal(size=(200, 6))
y = X[:, 0] - X[:, 1] + rng.normal(size=200)

model = RLT_reg(
    n_estimators=100, mtry=3, nsplit=0,
    resample_replace=True, resample_prob=0.8,
    random_state=1, n_jobs=1,
).fit(X[:160], y[:160])
pred = model.predict(X[160:])

print(f"OOB MSE: {model.oob_error_:.3f}")
print(f"Test MSE: {np.mean((pred - y[160:]) ** 2):.3f}")
OOB MSE: 1.401
Test MSE: 1.847

Both numbers are mean squared prediction errors; smaller is better. Each out-of-bag (OOB) prediction uses only trees that omitted that training row. Test MSE evaluates the separate 40-row test set. Their difference here is one small-sample result, not evidence that one evaluation method is generally better. The first-analysis walkthrough also shows individual predictions and explains the fitting controls.

Choose your analysis

Task Start here Main result
Predict a continuous response Regression Predicted response means and OOB/test MSE
Predict a class Classification Class labels and probabilities in classes_ order
Model a censored event time Survival Conditional survival curves, cumulative hazards, and OOB concordance error

Understand and inspect a fitted forest

  • Variable importance: measure the change in OOB predictive loss when a variable is perturbed.
  • Linear combinations: allow up to several predictors in a split direction.
  • Survival confidence bands: construct nominal simultaneous bands separately for each target curve over its returned time grid.
  • Forest kernels: inspect which observations share terminal nodes, with separate ordinary, training, and OOB calculations.
  • Individual trees: read named splits and terminal predictions with tree_summary().
  • Model workflows: work with labeled data, inspect OOB availability, request prediction uncertainty, and save fitted models.

See reproducibility to control simulation and forest randomness, or comparing Python and R for numerical agreement, source matching, and interface differences.

Citations and license

Cite the papers for the methods used in your analysis:

The survival paper develops the cumulative-hazard covariance and Gaussian simulation framework for the bands. See the get_surv_band reference for the current smoothing and covariance handling.

RLT for Python is distributed under GPL (>= 3), inherited from the R package.

CI Docs