Random forest
smac.model.random_forest.random_forest
#
EPMRandomForest
#
EPMRandomForest(
n_estimators: int = 100,
*,
log_y: bool = False,
cross_trees_variance: bool = False,
criterion: str = "squared_error",
splitter: str = "random",
max_depth: int | None = None,
min_samples_split: int = 2,
min_samples_leaf: int = 1,
min_weight_fraction_leaf: float = 0.0,
max_features: float = 1.0,
max_leaf_nodes: int | None = None,
min_impurity_decrease: float = 0.0,
bootstrap: bool = False,
oob_score: bool = False,
n_jobs: int | None = None,
random_state: int | None = None,
verbose: int = 0,
warm_start: bool = False,
ccp_alpha: float = 0.0,
max_samples: int | float | None = None,
monotonic_cst: Iterable | None = None
)
Bases: ForestRegressor
Read more in the :ref:User Guide <tree>.
| PARAMETER | DESCRIPTION |
|---|---|
n_estimators
|
The number of trees in the forest. .. versionchanged:: 0.22
The default value of
TYPE:
|
criterion
|
The function to measure the quality of a split. Supported criteria are "squared_error" for the mean squared error, which is equal to variance reduction as feature selection criterion and minimizes the L2 loss using the mean of each terminal node, "friedman_mse", which uses mean squared error with Friedman's improvement score for potential splits, "absolute_error" for the mean absolute error, which minimizes the L1 loss using the median of each terminal node, and "poisson" which uses reduction in Poisson deviance to find splits. Training using "absolute_error" is significantly slower than when using "squared_error". .. versionadded:: 0.18 Mean Absolute Error (MAE) criterion. .. versionadded:: 1.0 Poisson criterion.
TYPE:
|
max_depth
|
The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.
TYPE:
|
min_samples_split
|
The minimum number of samples required to split an internal node:
.. versionchanged:: 0.18 Added float values for fractions. |
min_samples_leaf
|
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
.. versionchanged:: 0.18 Added float values for fractions. |
min_weight_fraction_leaf
|
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.
TYPE:
|
max_features
|
The number of features to consider when looking for the best split:
.. note:: The default of 1.0 is equivalent to bagged trees and more randomness can be achieved by setting smaller values, e.g. 0.3. .. versionchanged:: 1.1
The default of .. deprecated:: 1.1
The Note: the search for a split does not stop until at least one
valid partition of the node samples is found, even if it requires to
effectively inspect more than
TYPE:
|
max_leaf_nodes
|
Grow trees with
TYPE:
|
min_impurity_decrease
|
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:: where
.. versionadded:: 0.19
TYPE:
|
bootstrap
|
Whether bootstrap samples are used when building trees. If False, the whole dataset is used to build each tree.
TYPE:
|
oob_score
|
Whether to use out-of-bag samples to estimate the generalization score. Only available if bootstrap=True.
TYPE:
|
n_jobs
|
The number of jobs to run in parallel. :meth:
TYPE:
|
random_state
|
Controls both the randomness of the bootstrapping of the samples used
when building trees (if
TYPE:
|
verbose
|
Controls the verbosity when fitting and predicting.
TYPE:
|
warm_start
|
When set to
TYPE:
|
ccp_alpha
|
Complexity parameter used for Minimal Cost-Complexity Pruning. The
subtree with the largest cost complexity that is smaller than
.. versionadded:: 0.22
TYPE:
|
max_samples
|
If bootstrap is True, the number of samples to draw from X to train each base estimator.
.. versionadded:: 0.22 |
criterion
|
The function to measure the quality of a split. Supported criteria are "squared_error" for the mean squared error, which is equal to variance reduction as feature selection criterion and minimizes the L2 loss using the mean of each terminal node, "friedman_mse", which uses mean squared error with Friedman's improvement score for potential splits, "absolute_error" for the mean absolute error, which minimizes the L1 loss using the median of each terminal node, and "poisson" which uses reduction in Poisson deviance to find splits. .. versionadded:: 0.18 Mean Absolute Error (MAE) criterion. .. versionadded:: 0.24 Poisson deviance criterion.
TYPE:
|
splitter
|
The strategy used to choose the split at each node. Supported strategies are "best" to choose the best split and "random" to choose the best random split.
TYPE:
|
max_depth
|
The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.
TYPE:
|
min_samples_split
|
The minimum number of samples required to split an internal node:
.. versionchanged:: 0.18 Added float values for fractions. |
min_samples_leaf
|
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
.. versionchanged:: 0.18 Added float values for fractions. |
min_weight_fraction_leaf
|
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.
TYPE:
|
max_features
|
The number of features to consider when looking for the best split:
.. deprecated:: 1.1
The Note: the search for a split does not stop until at least one
valid partition of the node samples is found, even if it requires to
effectively inspect more than
TYPE:
|
random_state
|
Controls the randomness of the estimator. The features are always
randomly permuted at each split, even if
TYPE:
|
max_leaf_nodes
|
Grow a tree with
TYPE:
|
min_impurity_decrease
|
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:: where
.. versionadded:: 0.19
TYPE:
|
ccp_alpha
|
Complexity parameter used for Minimal Cost-Complexity Pruning. The
subtree with the largest cost complexity that is smaller than
.. versionadded:: 0.22
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
all_trees_pred
#
This function is used to parally predict the target X values. It is based on rf regressor from sklearn 1.6.1: github.com/scikit-learn/scikit-learn/blob/99bf3d8e4eed5ba5db19a1869482a238b6223ffd/sklearn/ensemble/_forest.py#L1045
| PARAMETER | DESCRIPTION |
|---|---|
X
|
input feature X
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
preds
|
Predictions from all trees
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
fit
#
Build a forest of trees from the training set (X, y). In additional to the vanilla RF fitting process, we also need to edit the estimators' parameters after the fitting process when self.log_y is True. This ensures that the model performance consistently compared to the pyrfr version. To compute the means of all the values, we first need to recover the log scaled values stored in the leave nodes to their raw scale and then compute the mean over those values. This mean value will then transformed back to the log scale.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
The training input samples. Internally, its dtype will be converted
to
TYPE:
|
y
|
The target values (class labels in classification, real numbers in regression).
TYPE:
|
sample_weight
|
Sample weights. If None, 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.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
self
|
Fitted estimator.
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
predict
#
Predict the mean and variance of X. Here mean and variances are the empirical mean and variance values from the prediction results of the other trees.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data points to be testsed.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
means: np.ndarray [#samples, 1]
|
vars: np.ndarray [#samples, 1] predicted variance. |
Source code in smac/model/random_forest/random_forest.py
predict_marginalized_over_instances_batch
#
Collects the predictions for each tree in the forest for multiple configurations over a set of instances. Each configuration vector is combined with all the instance feature vectors. Based on the response values over all these feature vectors the mean is computed. In the case of log transformation the response values are decompressed before averaging.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data points.
TYPE:
|
X_feat
|
Features (np.ndarray) of the instances (str). The features are incorporated into the X data, on which the model is trained on.
TYPE:
|
log_y
|
if log_y is applied to the predictions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
preds
|
predictions for each sample and trees. Each element in preds corresponds to the mean response values for the target estimator and configuration accross all the instances.
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
RandomForest
#
RandomForest(
configspace: ConfigurationSpace,
max_samples: int | float | None = None,
ratio_features: float = 5.0 / 6.0,
log_y: bool = False,
instance_features: (
dict[str, list[int | float]] | None
) = None,
pca_components: int | None = 7,
seed: int = 0,
n_trees: int = N_TREES,
cross_trees_variance: bool = False,
criterion: str = "squared_error",
splitter: str = "random",
max_depth: int = 2**20,
min_samples_split: int = 3,
min_samples_leaf: int = 3,
min_weight_fraction_leaf: float = 0.0,
max_leaf_nodes: int = 2**20,
min_impurity_decrease: float = 1e-08,
bootstrapping: bool = True,
oob_score: bool = False,
n_jobs: int | None = -1,
verbose: int = 0,
warm_start: bool = False,
ccp_alpha: float = 0.0,
monotonic_cst: Iterable | None = None,
)
Bases: AbstractRandomForest
Random forest that takes instance features into account.
| PARAMETER | DESCRIPTION |
|---|---|
n_trees
|
The number of trees in the random forest.
TYPE:
|
max_samples
|
Number of points per tree. If the value is None, the number of samples will be used. Otherwise, use max_samples (if it is int) or max(round(n_samples * max_samples), 1) (if it is float value)
TYPE:
|
ratio_features
|
The ratio of features that are considered for splitting.
TYPE:
|
min_samples_split
|
The minimum number of data points to perform a split.
TYPE:
|
min_samples_leaf
|
The minimum number of data points in a leaf.
TYPE:
|
max_depth
|
The maximum depth of a single tree.
TYPE:
|
max_leaf_nodes
|
The maximum total number of nodes in a tree.
TYPE:
|
bootstrapping
|
Enables bootstrapping.
TYPE:
|
log_y
|
The y values (passed to this random forest) are expected to be log(y) transformed. This will be considered during predicting.
TYPE:
|
instance_features
|
Features (list of int or floats) of the instances (str). The features are incorporated into the X data, on which the model is trained on.
TYPE:
|
pca_components
|
Number of components to keep when using PCA to reduce dimensionality of instance features.
TYPE:
|
seed
|
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
predict
#
Predicts mean and variance for a given X. Internally, calls the method _predict.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data points.
TYPE:
|
covariance_type
|
Specifies what to return along with the mean. Applied only to Gaussian Processes. Takes four valid inputs: * None: Only the mean is returned. * "std": Standard deviation at test points is returned. * "diagonal": Diagonal of the covariance matrix is returned. * "full": Whole covariance matrix between the test points is returned.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
means
|
The predictive mean.
TYPE:
|
vars
|
Predictive variance or standard deviation.
TYPE:
|
Source code in smac/model/abstract_model.py
predict_marginalized
#
Predicts mean and variance marginalized over all instances.
Under the hood: Collects the predictions for each tree in the forest for multiple configurations over a set of instances. Each configuration vector is combined with all the instance feature vectors. Based on the response values over all these feature vectors the mean is computed. In the case of log transformation the response values are decompressed before averaging.
Note
The method is random forest specific and follows the SMAC2 implementation. It requires no distribution assumption to marginalize the uncertainty estimates.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data points.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
means
|
The predictive mean.
TYPE:
|
vars
|
The predictive variance.
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
train
#
Trains the random forest on X and Y. Internally, calls the method _train.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data points.
TYPE:
|
Y
|
The corresponding target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
self
|
TYPE:
|
Source code in smac/model/abstract_model.py
accumulate_predict_over_instances
#
accumulate_predict_over_instances(
predict: Callable,
X: ndarray,
X_instance_feat: ndarray,
results: ndarray,
tree_idx: int,
n_instances: int,
lock: Lock,
) -> None
Collect predictions from a single estimator. However, we sum the results from all instances
| PARAMETER | DESCRIPTION |
|---|---|
predict
|
the prediction function, in this scenario, it is the prediction function of each tree
TYPE:
|
X
|
Input data points.
TYPE:
|
X_instance_feat
|
Features (np.ndarray) of the instances (str). The features are incorporated into the X data, on which the model is trained on.
TYPE:
|
results
|
output values from all the predictors
TYPE:
|
tree_idx
|
tree index
TYPE:
|
n_instances
|
number of instance
TYPE:
|
lock
|
threading lock
TYPE:
|
Source code in smac/model/random_forest/random_forest.py
estimator_predict
#
Collect predictions from a single estimator.
| PARAMETER | DESCRIPTION |
|---|---|
predict
|
the prediction function, in this scenario, it is the prediction function of each tree
TYPE:
|
X
|
input features
TYPE:
|
results
|
output values from all the predictors
TYPE:
|
tree_idx
|
estimator index
TYPE:
|