# RandomForestClassifier

### *class* cuml.ensemble.RandomForestClassifier(, n_estimators=100, split_criterion='gini', bootstrap=True, max_samples=1.0, max_depth=None, max_leaves=-1, max_features='sqrt', 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, class_weight=None, verbose=False, output_type=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestclassifier.py#L16)

Implements a Random Forest classifier model which fits multiple decision
tree classifiers 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 i 0.11)

  **split_criterion**
  : The criterion used to split nodes.
    > * `'gini'` or `0` for gini impurity
    > * `'entropy'` or `1` for information gain (entropy)

  **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 `"sqrt"`.

  **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: Impurity decrease is now weighted by the node’s fraction of the
    tree’s total training weight. Positive thresholds may produce
    smaller trees and different predictions. 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
    accuracy. 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 accuracy.

  **class_weight**
  : Weights associated with classes. If `'balanced'`, class weights are
    computed from the training labels.

  **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:**
  **classes_**
  : A sorted array of the class labels.

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

  **oob_decision_function_**
  : Decision function computed with out-of-bag estimate on the training
    set. If n_estimators is small it might be possible that a data point
    was never left out during the bootstrap. In this case,
    `oob_decision_function_` might contain NaN. This attribute exists
    only when `oob_score` is True.

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

### Methods

| [`fit`](#cuml.ensemble.RandomForestClassifier.fit)(X, y[, sample_weight])                   | Perform Random Forest Classification on the input data   |
|-----------------------------------------------------------------------------------------------|----------------------------------------------------------|
| [`predict`](#cuml.ensemble.RandomForestClassifier.predict)(X, \*[, threshold, layout, ...])     | Predicts the labels for X.                               |
| [`predict_log_proba`](#cuml.ensemble.RandomForestClassifier.predict_log_proba)(X, \*[, layout, ...])      | Predicts log class probabilities for X.                  |
| [`predict_proba`](#cuml.ensemble.RandomForestClassifier.predict_proba)(X, \*[, layout, ...])          | Predicts class probabilities for X.                      |
| [`score`](#cuml.ensemble.RandomForestClassifier.score)(X, y[, sample_weight, threshold, ...]) | Calculates the accuracy score of the model on test data. |

### Notes

While training the model for multi class classification problems, using
deep trees or `max_features=1.0` provides better performance.

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

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

>>> X = cp.random.normal(size=(10,4)).astype(cp.float32)
>>> y = cp.asarray([0,1]*5, dtype=cp.int32)

>>> cuml_model = cuRFC(max_features=1.0,
...                    n_bins=8,
...                    n_estimators=40)
>>> cuml_model.fit(X,y)
RandomForestClassifier()
>>> cuml_predict = cuml_model.predict(X)

>>> print("Predicted labels : ", cuml_predict)
Predicted labels :  [0. 1. 0. 1. 0. 1. 0. 1. 0. 1.]
```

<!-- !! 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) → [RandomForestClassifier](#cuml.ensemble.RandomForestClassifier)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestclassifier.py#L260)

Perform Random Forest Classification 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 int32.
    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, , threshold=0.5, layout='depth_first', default_chunk_size=None, align_bytes=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/ensemble/randomforestclassifier.py#L306)

Predicts the labels 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.

  **threshold**
  : Threshold used for classification.

  **layout**
  : Forest layout for GPU inference. Options: ‘depth_first’, ‘layered’,
    ‘breadth_first’.

  **default_chunk_size**
  : Controls batch subdivision for parallel processing. Optimal value depends
    on hardware, model and batch size. If None, determined automatically.

  **align_bytes**
  : If specified, trees will be padded to this byte alignment, which can
    improve performance. Typical values are 0 or 128 on GPU.
* **Returns:**
  **y**

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

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

Predicts log class probabilities 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 on GPU and 0 or 64 on CPU.
* **Returns:**
  **y**

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

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

Predicts class probabilities 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 on GPU and 0 or 64 on CPU.
* **Returns:**
  **y**

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

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

Calculates the accuracy 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 int32.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.

  **sample_weight**
  : Sample weights for weighted mean accuracy.

  **threshold**
  : Threshold used for classification predictions

  **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 on GPU and 0 or 64 on CPU.
* **Returns:**
  **accuracy**
  : Accuracy of the model [0.0 - 1.0]

<!-- !! 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 !! -->
