# 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]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestregressor.py#L11)

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/](https://docs.nvidia.com/cuml/latest/pickling_cuml_models/)
for more details.

* **Parameters:**
  **n_estimators**
  : Number of trees in the forest. (Default changed to 100 in cuML 0.11)

  **split_criterion**
  : 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

  **bootstrap**
  : 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_samples**
  : Ratio of dataset rows used while fitting each tree.

  **max_depth**
  : Maximum tree depth. Use `None` for unlimited depth (trees grow
    until all leaves are pure). Must be a positive integer or `None`.
    <!-- rapids-pre-commit-hooks: disable-next-line -->
    <br/>
    #### Versionchanged
    Changed in version 26.08: The default of `max_depth` changed from `16` to [`None`](https://docs.python.org/3/builtins/constants.html#None).

  **max_leaves**
  : Maximum leaf nodes per tree. Soft constraint. Unlimited,
    If `-1`.

  **max_features**
  : 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`
    <br/>
    #### Versionchanged
    Changed in version 24.06: The default of `max_features` changed from `"auto"` to 1.0.

  **n_bins**
  : 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_streams**
  : Number of parallel streams used for forest building

  **min_samples_leaf**
  : 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_split**
  : 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_decrease**
  : A node will be split if this split induces a decrease of the weighted impurity
    greater than this value.
    <br/>
    #### Versionchanged
    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_size**
  : Maximum number of nodes that can be processed in a given batch.

  **random_state**
  : Seed for the random number generator. Unseeded by default.

  **oob_score**
  : 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).

  **verbose**
  : Sets logging level. It must be one of `cuml.common.logger.level_*`.
    See [Verbosity Levels](../../cuml/index.html.md#verbosity-levels) for more info.

  **output_type**
  : 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](../../cuml/index.html.md#output-data-type-configuration) for more info.
* **Attributes:**
  **oob_score_**
  : Score of the training dataset obtained using an out-of-bag estimate.
    This attribute exists only when `oob_score` is True.

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

  **feature_importances_**
  : The impurity-based feature importances.

### Methods

| [`fit`](#cuml.ensemble.RandomForestRegressor.fit)(X, y[, sample_weight])                        | Perform Random Forest Regression on the input data   |
|----------------------------------------------------------------------------------------------------|------------------------------------------------------|
| [`predict`](#cuml.ensemble.RandomForestRegressor.predict)(X, \*[, layout, default_chunk_size, ...]) | Predicts the values for X.                           |
| [`score`](#cuml.ensemble.RandomForestRegressor.score)(X, y[, sample_weight, layout, ...])         | Calculates the r2 score of the model on test data.   |

### Notes

For additional docs, see [scikitlearn’s RandomForestRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html).

When converting to sklearn using [`as_sklearn()`](#cuml.ensemble.RandomForestRegressor.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

```python
>>> 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
```

<!-- !! processed by numpydoc !! -->

#### as_nvforest(self, layout='depth_first', default_chunk_size=None, align_bytes=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforest_common.pyx#L392)

Create a nvForest model from the cuML model.

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

  **default_chunk_size**
  : 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_bytes**
  : 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_model**
  : A forest inference model which can be used to perform
    inferencing on the random forest model.

<!-- !! processed by numpydoc !! -->

#### as_sklearn()[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/interop.py#L206)

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.

<!-- !! processed by numpydoc !! -->

#### as_treelite(self)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforest_common.pyx#L380)

Converts this estimator to a Treelite model.

* **Returns:**
  treelite.Model

<!-- !! processed by numpydoc !! -->

#### fit(X, y, sample_weight=None) → [RandomForestRegressor](#cuml.ensemble.RandomForestRegressor)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestregressor.py#L203)

Perform Random Forest Regression on the input data

* **Parameters:**
  **X**
  : 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.

  **y**
  : 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_weight**
  : 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.

<!-- !! processed by numpydoc !! -->

#### *classmethod* from_sklearn(model)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/interop.py#L222)

Create a cuml estimator from a scikit-learn estimator.

* **Parameters:**
  **model**
  : 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.

<!-- !! processed by numpydoc !! -->

#### get_params(deep=True)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L191)

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

<!-- !! processed by numpydoc !! -->

#### predict(X, , layout='depth_first', default_chunk_size=None, align_bytes=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestregressor.py#L229)

Predicts the values for X.

* **Parameters:**
  **X**
  : 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.

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

  **default_chunk_size**
  : 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_bytes**
  : 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:**
  **y**

<!-- !! processed by numpydoc !! -->

#### score(X, y, sample_weight=None, , layout='depth_first', default_chunk_size=None, align_bytes=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestregressor.py#L289)

Calculates the r2 score of the model on test data.

* **Parameters:**
  **X**
  : 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.

  **y**
  : 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_weight**
  : Sample weights for weighted R^2.

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

  **default_chunk_size**
  : 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_bytes**
  : 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_score**

<!-- !! processed by numpydoc !! -->

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L200)

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

<!-- !! processed by numpydoc !! -->
