RandomForestRegressor#

class cuml.ensemble.RandomForestRegressor(
*,
n_estimators=100,
split_criterion='mse',
bootstrap=True,
max_samples=1.0,
max_depth=None,
max_leaves=-1,
max_features=1.0,
n_bins=128,
min_samples_leaf=1,
min_samples_split=2,
min_impurity_decrease=0.0,
max_batch_size=4096,
random_state=None,
n_streams=4,
oob_score=False,
verbose=False,
output_type=None,
)[source]#

Implements a Random Forest regressor model which fits multiple decision trees in an ensemble.

Note

Note that the underlying algorithm for tree node splits differs from that used in scikit-learn. By default, the cuML Random Forest uses a quantile-based algorithm to determine splits, rather than an exact count. You can tune the size of the quantiles with the n_bins parameter

Note

You can export cuML Random Forest models and run predictions with them on machines without an NVIDIA GPUs. See https://docs.nvidia.com/cuml/latest/pickling_cuml_models/ for more details.

Parameters:
n_estimatorsint (default = 100)

Number of trees in the forest. (Default changed to 100 in cuML 0.11)

split_criterionstr or int (default = 'mse')

The criterion used to split nodes.

  • 'mse' or 2 for mean squared error

  • 'poisson' or 4 for poisson half deviance

  • 'gamma' or 5 for gamma half deviance

  • 'inverse_gaussian' or 6 for inverse gaussian deviance

bootstrapboolean (default = True)

Control bootstrapping.

  • If True, each tree in the forest is built on a bootstrapped sample with replacement.

  • If False, the whole dataset is used to build each tree.

max_samplesfloat (default = 1.0)

Ratio of dataset rows used while fitting each tree.

max_depthint or None (default = None)

Maximum tree depth. Use None for unlimited depth (trees grow until all leaves are pure). Must be a positive integer or None.

Changed in version 26.08: The default of max_depth changed from 16 to None.

max_leavesint (default = -1)

Maximum leaf nodes per tree. A positive limit grows the tree greedily, splitting the leaf with the largest weighted impurity decrease first. If -1, use unlimited batched growth.

max_features{‘sqrt’, ‘log2’, None}, int or float (default = 1.0)

The number of features to consider per node split:

  • If an int then max_features is the absolute count of features to be used.

  • If a float then max_features is used as a fraction.

  • If 'sqrt' then max_features=1/sqrt(n_features).

  • If 'log2' then max_features=log2(n_features)/n_features.

  • If None then max_features=n_features

Changed in version 24.06: The default of max_features changed from "auto" to 1.0.

n_binsint (default = 128)

Maximum number of bins used by the split algorithm per feature. For large problems, particularly those with highly-skewed input data, increasing the number of bins may improve accuracy.

n_streamsint (default = 4 )

Number of parallel streams used for forest building

min_samples_leafint or float (default = 1)

The minimum number of samples (rows) in each leaf node.

  • If type int, then min_samples_leaf represents the minimum number.

  • If float, then min_samples_leaf represents a fraction and ceil(min_samples_leaf * n_rows) is the minimum number of samples for each leaf node.

min_samples_splitint or float (default = 2)

The minimum number of samples required to split an internal node.

  • If type int, then min_samples_split represents the minimum number.

  • If type float, then min_samples_split represents a fraction and max(2, ceil(min_samples_split * n_rows)) is the minimum number of samples for each split.

min_impurity_decreasefloat (default = 0.0)

A node will be split if this split induces a decrease of the weighted impurity greater than this value.

Changed in version 26.12: MSE split gains now use full variance instead of half variance to match scikit-learn. For all criteria, impurity decrease is now weighted by the node’s fraction of the tree’s total training weight. Existing positive thresholds may need retuning. The default 0.0 needs no adjustment.

max_batch_sizeint (default = 4096)

Maximum number of nodes that can be processed in a given batch.

random_stateint (default = None)

Seed for the random number generator. Unseeded by default.

oob_scorebool (default = False)

Whether to use out-of-bag samples to estimate the generalization performance. Only available if bootstrap=True. The out-of-bag estimate provides a way to evaluate the model without requiring a separate validation set. The OOB score is computed using R² (coefficient of determination).

verboseint or boolean, default=False

Sets logging level. It must be one of cuml.common.logger.level_*. See Verbosity Levels for more info.

output_type{None, ‘input’, ‘cupy’, ‘numpy’, ‘cudf’, ‘pandas’}, default=None

Return results and set estimator attributes to the indicated output type. If None, the output type set at the module level (cuml.global_settings.output_type) will be used. See Output Data Type Configuration for more info.

Attributes:
oob_score_float

Score of the training dataset obtained using an out-of-bag estimate. This attribute exists only when oob_score is True.

oob_prediction_ndarray of shape (n_samples,) or (n_samples, n_outputs)

Prediction computed with out-of-bag estimate on the training set. This attribute exists only when oob_score is True.

feature_importances_ndarray of shape (n_features,)

The impurity-based feature importances.

Methods

fit(X, y[, sample_weight])

Perform Random Forest Regression on the input data

predict(X, *[, layout, default_chunk_size, ...])

Predicts the values for X.

score(X, y[, sample_weight, layout, ...])

Calculates the r2 score of the model on test data.

Notes

For additional docs, see scikitlearn’s RandomForestRegressor.

When converting to sklearn using as_sklearn(), the feature_importances_ attribute will return NaN values. If you need feature importances, save them before conversion: importances = cuml_model.feature_importances_

Examples

>>> import cupy as cp
>>> from cuml.ensemble import RandomForestRegressor as curfr
>>> X = cp.asarray([[0,10],[0,20],[0,30],[0,40]], dtype=cp.float32)
>>> y = cp.asarray([0.0,1.0,2.0,3.0], dtype=cp.float32)
>>> cuml_model = curfr(max_features=1.0, n_bins=128,
...                    min_samples_leaf=1,
...                    min_samples_split=2,
...                    n_estimators=40)
>>> cuml_model.fit(X,y)
RandomForestRegressor(n_estimators=40)
>>> cuml_score = cuml_model.score(X,y)
>>> print("R2 score of cuml : ", cuml_score)
R2 score of cuml :  0.9076250195503235
as_nvforest(
self,
layout='depth_first',
default_chunk_size=None,
align_bytes=None,
)[source]#

Create a nvForest model from the cuML model.

Parameters:
layoutstring (default = ‘depth_first’)

Specifies the in-memory layout of nodes in forests. Options: ‘depth_first’, ‘layered’, ‘breadth_first’.

default_chunk_sizeint, optional (default = None)

Determines how batches are further subdivided for parallel processing. The optimal value depends on hardware, model, and batch size. If None, will be automatically determined.

align_bytesint, optional (default = None)

If specified, trees will be padded such that their in-memory size is a multiple of this value. This can improve performance by guaranteeing that memory reads from trees begin on a cache line boundary. Typical values are 0 or 128 on GPU and 0 or 64 on CPU.

Returns:
nvforest_modelnvforest.ForestInference

A forest inference model which can be used to perform inferencing on the random forest model.

as_sklearn()[source]#

Convert this estimator into an equivalent scikit-learn (or scikit-learn extension) estimator.

Returns:
sklearn.base.BaseEstimator

A scikit-learn compatible estimator instance that mirrors the trained state of the current estimator.

as_treelite(self)[source]#

Converts this estimator to a Treelite model.

Returns:
treelite.Model
fit(
X,
y,
sample_weight=None,
) → RandomForestRegressor[source]#

Perform Random Forest Regression on the input data

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yarray-like (device or host) shape = (n_samples, 1)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

sample_weightarray-like (device or host) shape = (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

classmethod from_sklearn(model)[source]#

Create a cuml estimator from a scikit-learn estimator.

Parameters:
modelsklearn.base.BaseEstimator

A compatible scikit-learn (or scikit-learn extension) estimator.

Returns:
cls

A new instance of this cuml estimator class that mirrors the state of the input estimator.

Notes

output_type of the estimator is set to “numpy” by default, as these cannot be inferred from training arguments. If something different is required, then please use cuml’s output_type configuration utilities.

get_params(deep=True)[source]#

Returns a dict of all params owned by this class. If the child class has appropriately overridden the _get_param_names method and does not need anything other than what is there in this method, then it doesn’t have to override this method

predict(
X,
*,
layout='depth_first',
default_chunk_size=None,
align_bytes=None,
)[source]#

Predicts the values for X.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

layoutstring (default = ‘depth_first’)

Specifies the in-memory layout of nodes in FIL forests. Options: ‘depth_first’, ‘layered’, ‘breadth_first’.

default_chunk_sizeint, optional (default = None)

Determines how batches are further subdivided for parallel processing. The optimal value depends on hardware, model, and batch size. If None, will be automatically determined.

align_bytesint, optional (default = None)

If specified, trees will be padded such that their in-memory size is a multiple of this value. This can improve performance by guaranteeing that memory reads from trees begin on a cache line boundary. Typical values are 0 or 128.

Returns:
ycuDF, CuPy or NumPy object depending on cuML’s output typeconfiguration, shape =(n_samples, 1)
score(
X,
y,
sample_weight=None,
*,
layout='depth_first',
default_chunk_size=None,
align_bytes=None,
)[source]#

Calculates the r2 score of the model on test data.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yarray-like (device or host) shape = (n_samples, 1)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

sample_weightarray-like, shape=(n_samples,), default=None

Sample weights for weighted R^2.

layoutstring (default = ‘depth_first’)

Specifies the in-memory layout of nodes in FIL forests. Options: ‘depth_first’, ‘layered’, ‘breadth_first’.

default_chunk_sizeint, optional (default = None)

Determines how batches are further subdivided for parallel processing. The optimal value depends on hardware, model, and batch size. If None, will be automatically determined.

align_bytesint, optional (default = None)

If specified, trees will be padded such that their in-memory size is a multiple of this value. This can improve performance by guaranteeing that memory reads from trees begin on a cache line boundary. Typical values are 0 or 128.

Returns:
r2_scorefloat
set_params(**params)[source]#

Accepts a dict of params and updates the corresponding ones owned by this class. If the child class has appropriately overridden the _get_param_names method and does not need anything other than what is, there in this method, then it doesn’t have to override this method