RandomForestClassifier
A random forest classifier.
A random forest is a meta estimator that fits a number of decision tree classifiers on various sub-samples of the dataset and uses averaging to improve the predictive accuracy and control over-fitting. The sub-sample size is controlled with the max\_samples
parameter if bootstrap=True
(default), otherwise the whole dataset is used to build each tree.
For a comparison between tree-based ensemble models see the example Comparing Random Forests and Histogram Gradient Boosting models.
Read more in the User Guide.
Python Reference (opens in a new tab)
Constructors
constructor()
Signature
new RandomForestClassifier(opts?: object): RandomForestClassifier;
Parameters
Name | Type | Description |
---|---|---|
opts? | object | - |
opts.bootstrap? | boolean | Whether bootstrap samples are used when building trees. If false , the whole dataset is used to build each tree. Default Value true |
opts.ccp_alpha? | any | Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ccp\_alpha will be chosen. By default, no pruning is performed. See Minimal Cost-Complexity Pruning for details. Default Value 0 |
opts.class_weight? | any | Weights associated with classes in the form {class\_label: weight} . If not given, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. Note that for multioutput (including multilabel) weights should be defined for each class of every column in its own dict. For example, for four-class multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}]. The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as n\_samples / (n\_classes \* np.bincount(y)) The “balanced_subsample” mode is the same as “balanced” except that weights are computed based on the bootstrap sample for every tree grown. For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. |
opts.criterion? | "gini" | "entropy" | "log_loss" | The function to measure the quality of a split. Supported criteria are “gini” for the Gini impurity and “log_loss” and “entropy” both for the Shannon information gain, see Mathematical formulation. Note: This parameter is tree-specific. Default Value 'gini' |
opts.max_depth? | number | The maximum depth of the tree. If undefined , then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. |
opts.max_features? | number | "sqrt" | "log2" | The number of features to consider when looking for the best split: Default Value 'sqrt' |
opts.max_leaf_nodes? | number | Grow trees with max\_leaf\_nodes in best-first fashion. Best nodes are defined as relative reduction in impurity. If undefined then unlimited number of leaf nodes. |
opts.max_samples? | number | If bootstrap is true , the number of samples to draw from X to train each base estimator. |
opts.min_impurity_decrease? | number | A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following: Default Value 0 |
opts.min_samples_leaf? | number | The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least min\_samples\_leaf training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression. Default Value 1 |
opts.min_samples_split? | number | The minimum number of samples required to split an internal node: Default Value 2 |
opts.min_weight_fraction_leaf? | number | The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. Default Value 0 |
opts.n_estimators? | number | The number of trees in the forest. Default Value 100 |
opts.n_jobs? | number | The number of jobs to run in parallel. fit , predict , decision\_path and apply are all parallelized over the trees. undefined means 1 unless in a joblib.parallel\_backend (opens in a new tab) context. \-1 means using all processors. See Glossary for more details. |
opts.oob_score? | boolean | Whether to use out-of-bag samples to estimate the generalization score. By default, accuracy\_score is used. Provide a callable with signature metric(y\_true, y\_pred) to use a custom metric. Only available if bootstrap=True . Default Value false |
opts.random_state? | number | Controls both the randomness of the bootstrapping of the samples used when building trees (if bootstrap=True ) and the sampling of the features to consider when looking for the best split at each node (if max\_features < n\_features ). See Glossary for details. |
opts.verbose? | number | Controls the verbosity when fitting and predicting. Default Value 0 |
opts.warm_start? | boolean | When set to true , reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new forest. See Glossary and Fitting additional weak-learners for details. Default Value false |
Returns
Defined in: generated/ensemble/RandomForestClassifier.ts:27 (opens in a new tab)
Methods
apply()
Apply trees in the forest to X, return leaf indices.
Signature
apply(opts: object): Promise<ArrayLike[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The input samples. Internally, its dtype will be converted to dtype=np.float32 . If a sparse matrix is provided, it will be converted into a sparse csr\_matrix . |
Returns
Promise
<ArrayLike
[]>
Defined in: generated/ensemble/RandomForestClassifier.ts:256 (opens in a new tab)
decision_path()
Return the decision path in the forest.
Signature
decision_path(opts: object): Promise<any[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The input samples. Internally, its dtype will be converted to dtype=np.float32 . If a sparse matrix is provided, it will be converted into a sparse csr\_matrix . |
Returns
Promise
<any
[]>
Defined in: generated/ensemble/RandomForestClassifier.ts:291 (opens in a new tab)
dispose()
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Signature
dispose(): Promise<void>;
Returns
Promise
<void
>
Defined in: generated/ensemble/RandomForestClassifier.ts:239 (opens in a new tab)
fit()
Build a forest of trees from the training set (X, y).
Signature
fit(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The training input samples. Internally, its dtype will be converted to dtype=np.float32 . If a sparse matrix is provided, it will be converted into a sparse csc\_matrix . |
opts.sample_weight? | ArrayLike | Sample weights. If undefined , then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. In the case of classification, splits are also ignored if they would result in any single class carrying a negative weight in either child node. |
opts.y? | ArrayLike | The target values (class labels in classification, real numbers in regression). |
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:329 (opens in a new tab)
get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Signature
get_metadata_routing(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.routing? | any | A MetadataRequest encapsulating routing information. |
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:380 (opens in a new tab)
init()
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Signature
init(py: PythonBridge): Promise<void>;
Parameters
Name | Type |
---|---|
py | PythonBridge |
Returns
Promise
<void
>
Defined in: generated/ensemble/RandomForestClassifier.ts:171 (opens in a new tab)
predict()
Predict class for X.
The predicted class of an input sample is a vote by the trees in the forest, weighted by their probability estimates. That is, the predicted class is the one with highest mean probability estimate across the trees.
Signature
predict(opts: object): Promise<ArrayLike>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The input samples. Internally, its dtype will be converted to dtype=np.float32 . If a sparse matrix is provided, it will be converted into a sparse csr\_matrix . |
Returns
Promise
<ArrayLike
>
Defined in: generated/ensemble/RandomForestClassifier.ts:420 (opens in a new tab)
predict_log_proba()
Predict class log-probabilities for X.
The predicted class log-probabilities of an input sample is computed as the log of the mean predicted class probabilities of the trees in the forest.
Signature
predict_log_proba(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The input samples. Internally, its dtype will be converted to dtype=np.float32 . If a sparse matrix is provided, it will be converted into a sparse csr\_matrix . |
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:459 (opens in a new tab)
predict_proba()
Predict class probabilities for X.
The predicted class probabilities of an input sample are computed as the mean predicted class probabilities of the trees in the forest. The class probability of a single tree is the fraction of samples of the same class in a leaf.
Signature
predict_proba(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike | The input samples. Internally, its dtype will be converted to dtype=np.float32 . If a sparse matrix is provided, it will be converted into a sparse csr\_matrix . |
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:499 (opens in a new tab)
score()
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Signature
score(opts: object): Promise<number>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | Test samples. |
opts.sample_weight? | ArrayLike | Sample weights. |
opts.y? | ArrayLike | True labels for X . |
Returns
Promise
<number
>
Defined in: generated/ensemble/RandomForestClassifier.ts:539 (opens in a new tab)
set_fit_request()
Request metadata passed to the fit
method.
Note that this method is only relevant if enable\_metadata\_routing=True
(see sklearn.set\_config
). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
Signature
set_fit_request(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight? | string | boolean | Metadata routing for sample\_weight parameter in fit . |
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:592 (opens in a new tab)
set_score_request()
Request metadata passed to the score
method.
Note that this method is only relevant if enable\_metadata\_routing=True
(see sklearn.set\_config
). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
Signature
set_score_request(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.sample_weight? | string | boolean | Metadata routing for sample\_weight parameter in score . |
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:634 (opens in a new tab)
Properties
_isDisposed
boolean
=false
Defined in: generated/ensemble/RandomForestClassifier.ts:25 (opens in a new tab)
_isInitialized
boolean
=false
Defined in: generated/ensemble/RandomForestClassifier.ts:24 (opens in a new tab)
_py
PythonBridge
Defined in: generated/ensemble/RandomForestClassifier.ts:23 (opens in a new tab)
id
string
Defined in: generated/ensemble/RandomForestClassifier.ts:20 (opens in a new tab)
opts
any
Defined in: generated/ensemble/RandomForestClassifier.ts:21 (opens in a new tab)
Accessors
classes_
The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).
Signature
classes_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/ensemble/RandomForestClassifier.ts:726 (opens in a new tab)
estimator_
The child estimator template used to create the collection of fitted sub-estimators.
Signature
estimator_(): Promise<any>;
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:672 (opens in a new tab)
estimators_
The collection of fitted sub-estimators.
Signature
estimators_(): Promise<any>;
Returns
Promise
<any
>
Defined in: generated/ensemble/RandomForestClassifier.ts:699 (opens in a new tab)
feature_names_in_
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Signature
feature_names_in_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/ensemble/RandomForestClassifier.ts:807 (opens in a new tab)
n_classes_
The number of classes (single output problem), or a list containing the number of classes for each output (multi-output problem).
Signature
n_classes_(): Promise<number | any[]>;
Returns
Promise
<number
| any
[]>
Defined in: generated/ensemble/RandomForestClassifier.ts:753 (opens in a new tab)
n_features_in_
Number of features seen during fit.
Signature
n_features_in_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/ensemble/RandomForestClassifier.ts:780 (opens in a new tab)
n_outputs_
The number of outputs when fit
is performed.
Signature
n_outputs_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/ensemble/RandomForestClassifier.ts:834 (opens in a new tab)
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
.
Signature
oob_decision_function_(): Promise<ArrayLike[]>;
Returns
Promise
<ArrayLike
[]>
Defined in: generated/ensemble/RandomForestClassifier.ts:888 (opens in a new tab)
oob_score_
Score of the training dataset obtained using an out-of-bag estimate. This attribute exists only when oob\_score
is true
.
Signature
oob_score_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/ensemble/RandomForestClassifier.ts:861 (opens in a new tab)
py
Signature
py(): PythonBridge;
Returns
PythonBridge
Defined in: generated/ensemble/RandomForestClassifier.ts:158 (opens in a new tab)
Signature
py(pythonBridge: PythonBridge): void;
Parameters
Name | Type |
---|---|
pythonBridge | PythonBridge |
Returns
void
Defined in: generated/ensemble/RandomForestClassifier.ts:162 (opens in a new tab)