Ensembles

Bootstrap-aggregated random forests over Shape Generalized Trees.

Both forest estimators accept tao_n_runs and tao_lambda; these are forwarded to each base tree and TAO runs on that tree’s bootstrap sample (or the full training set when bootstrap=False) at the end of each tree’s fit. See TAO refinement for post-hoc refinement on the full (X, y).

mean_feature_importances_ and std_feature_importance_ (and the regressor counterparts) summarize per-tree feature_importances_ across the forest, aligned with the shared processed_features_.

RandomSGForestClassifier

class sgtlearn.RandomSGForestClassifier(n_estimators=100, *, criterion='gini', num_partitions=2, max_depth=None, max_leaf_nodes=None, min_samples_leaf=1, min_impurity_decrease=0.0, inner_max_depth=3, inner_max_leaf_nodes=8, inner_min_samples_leaf=1, inner_min_impurity_decrease=0.0, coordinate_descent_max_iters=20, coordinate_descent_patience=5, coordinate_descent_smart_init=True, max_features='sqrt', bootstrap=True, max_samples=None, random_state=None, class_weight=None, tao_n_runs=10, tao_lambda=0.0, n_jobs=None, verbose=0)[source]

Bases: ClassifierMixin, RandomSGForest

Random forest of Shape Generalized Trees for classification.

Bootstrap ensemble of sgtlearn.SGTClassifier base estimators. Prediction semantics match sklearn.ensemble.RandomForestClassifier: class probabilities are the mean of per-tree predict_proba(), and predict() returns the argmax class label.

Parameters:
  • n_estimators (int, default=100) – Number of trees in the forest.

  • criterion ({"gini", "entropy"}, default="gini") – Impurity criterion forwarded to each base tree’s outer splits.

  • num_partitions (int, default=2) – Arity of the shape function at each outer split. See sgtlearn.SGTClassifier.

  • max_depth (see sgtlearn.SGTClassifier) – Outer-tree stopping criteria forwarded to each base estimator.

  • max_leaf_nodes (see sgtlearn.SGTClassifier) – Outer-tree stopping criteria forwarded to each base estimator.

  • min_samples_leaf (see sgtlearn.SGTClassifier) – Outer-tree stopping criteria forwarded to each base estimator.

  • min_impurity_decrease (see sgtlearn.SGTClassifier) – Outer-tree stopping criteria forwarded to each base estimator.

  • inner_max_depth (see sgtlearn.SGTClassifier) – Inner-tree (shape function) controls forwarded to each base estimator.

  • inner_max_leaf_nodes (see sgtlearn.SGTClassifier) – Inner-tree (shape function) controls forwarded to each base estimator.

  • inner_min_samples_leaf (see sgtlearn.SGTClassifier) – Inner-tree (shape function) controls forwarded to each base estimator.

  • inner_min_impurity_decrease (see sgtlearn.SGTClassifier) – Inner-tree (shape function) controls forwarded to each base estimator.

  • coordinate_descent_max_iters (see sgtlearn.SGTClassifier) – Coordinate-descent controls forwarded to each base estimator.

  • coordinate_descent_patience (see sgtlearn.SGTClassifier) – Coordinate-descent controls forwarded to each base estimator.

  • coordinate_descent_smart_init (see sgtlearn.SGTClassifier) – Coordinate-descent controls forwarded to each base estimator.

  • max_features (int, float, {"sqrt", "log2"} or None, default="sqrt") – Per-split feature subsampling for each base tree. Defaults to "sqrt" to follow RandomForestClassifier convention. See sgtlearn.SGTClassifier for the full semantics.

  • bootstrap (bool, default=True) – If True, each tree is fit on a bootstrap resample (with replacement) of the training set. If False, every tree is fit on the full training set — diversity then comes only from random_state and max_features.

  • max_samples (int or float, optional) – Size of each bootstrap sample. int gives an absolute count; float in (0, 1] gives a fraction of n_samples. None (default) uses n_samples. Only valid when bootstrap=True.

  • random_state (int, RandomState, optional) – Controls bootstrap resampling and the per-tree seeds.

  • class_weight (dict, optional) – Per-class weights multiplied into sample_weight before training. Keys are class labels as in y.

  • n_jobs (int, optional) – Number of joblib workers used to fit trees. None means one job (sequential); -1 uses all processors. Joblib’s threading backend is used (the per-tree work is dominated by the native C++ trainer, same rationale as RandomForestClassifier).

  • verbose (int, default=0) – Verbosity of the joblib Parallel driver.

  • tao_n_runs (int)

  • tao_lambda (float)

estimators_

The collection of fitted base estimators.

Type:

list of SGTClassifier

classes_

Class labels in original training label space.

Type:

ndarray of shape (n_classes,)

n_classes_

Number of classes seen during fit().

Type:

int

n_features_in_

Number of features seen during fit().

Type:

int

mean_feature_importances_

Mean of per-tree feature_importances_, aligned with processed_features_.

Type:

ndarray of shape (n_logical_features,)

std_feature_importance_

Population standard deviation of per-tree importances across the forest (same alignment as mean_feature_importances_).

Type:

ndarray of shape (n_logical_features,)

processed_features_

Logical features resolved once and shared by every base tree.

Type:

ProcessedFeatures

See also

sgtlearn.SGTClassifier

Single-tree base estimator.

sgtlearn.ensemble.RandomSGForestRegressor

Regression counterpart.

sklearn.ensemble.RandomForestClassifier

Standard CART forest with the same prediction semantics.

Examples

>>> from sklearn.datasets import make_classification
>>> from sgtlearn.ensemble import RandomSGForestClassifier
>>> X, y = make_classification(n_samples=500, random_state=0)
>>> clf = RandomSGForestClassifier(n_estimators=20, random_state=0).fit(X, y)
>>> clf.predict(X[:5]).shape
(5,)
set_fit_request(*, feature_dict='$UNCHANGED$', processed_features='$UNCHANGED$', sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
  • feature_dict (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for feature_dict parameter in fit.

  • processed_features (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for processed_features parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

  • self (RandomSGForestClassifier)

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

  • self (RandomSGForestClassifier)

Returns:

self – The updated object.

Return type:

object

RandomSGForestRegressor

class sgtlearn.RandomSGForestRegressor(n_estimators=100, *, criterion='squared_error', num_partitions=2, max_depth=None, max_leaf_nodes=None, min_samples_leaf=1, min_impurity_decrease=0.0, inner_max_depth=3, inner_max_leaf_nodes=8, inner_min_samples_leaf=1, inner_min_impurity_decrease=0.0, coordinate_descent_max_iters=20, coordinate_descent_patience=5, coordinate_descent_smart_init=True, max_features='sqrt', bootstrap=True, max_samples=None, random_state=None, tao_n_runs=10, tao_lambda=0.0, n_jobs=None, verbose=0)[source]

Bases: RegressorMixin, RandomSGForest

Random forest of Shape Generalized Trees for regression.

Bootstrap ensemble of sgtlearn.SGTRegressor base estimators. Prediction semantics match sklearn.ensemble.RandomForestRegressor: predict() returns the mean of the per-tree predict() outputs.

Parameters:
  • n_estimators (int, default=100) – Number of trees in the forest.

  • criterion ({"squared_error", "mse", "absolute_error", "mae"}, default="squared_error") – Loss forwarded to each base tree’s outer splits.

  • num_partitions (int, default=2) – Arity of the shape function at each outer split. See sgtlearn.SGTRegressor.

  • max_depth (see sgtlearn.SGTRegressor) – Outer-tree stopping criteria forwarded to each base estimator.

  • max_leaf_nodes (see sgtlearn.SGTRegressor) – Outer-tree stopping criteria forwarded to each base estimator.

  • min_samples_leaf (see sgtlearn.SGTRegressor) – Outer-tree stopping criteria forwarded to each base estimator.

  • min_impurity_decrease (see sgtlearn.SGTRegressor) – Outer-tree stopping criteria forwarded to each base estimator.

  • inner_max_depth (see sgtlearn.SGTRegressor) – Inner-tree (shape function) controls forwarded to each base estimator.

  • inner_max_leaf_nodes (see sgtlearn.SGTRegressor) – Inner-tree (shape function) controls forwarded to each base estimator.

  • inner_min_samples_leaf (see sgtlearn.SGTRegressor) – Inner-tree (shape function) controls forwarded to each base estimator.

  • inner_min_impurity_decrease (see sgtlearn.SGTRegressor) – Inner-tree (shape function) controls forwarded to each base estimator.

  • coordinate_descent_max_iters (see sgtlearn.SGTRegressor) – Coordinate-descent controls forwarded to each base estimator. Note that coordinate_descent_smart_init is ignored by the regression trainer.

  • coordinate_descent_patience (see sgtlearn.SGTRegressor) – Coordinate-descent controls forwarded to each base estimator. Note that coordinate_descent_smart_init is ignored by the regression trainer.

  • coordinate_descent_smart_init (see sgtlearn.SGTRegressor) – Coordinate-descent controls forwarded to each base estimator. Note that coordinate_descent_smart_init is ignored by the regression trainer.

  • max_features (int, float, {"sqrt", "log2"} or None, default="sqrt") – Per-split feature subsampling for each base tree. Defaults to "sqrt" to follow RandomForestRegressor convention. See sgtlearn.SGTRegressor for the full semantics.

  • bootstrap (bool, default=True) – If True, each tree is fit on a bootstrap resample (with replacement) of the training set. If False, every tree is fit on the full training set — diversity then comes only from random_state and max_features.

  • max_samples (int or float, optional) – Size of each bootstrap sample. int gives an absolute count; float in (0, 1] gives a fraction of n_samples. None (default) uses n_samples. Only valid when bootstrap=True.

  • random_state (int, RandomState, optional) – Controls bootstrap resampling and the per-tree seeds.

  • n_jobs (int, optional) – Number of joblib workers used to fit trees. None means one job (sequential); -1 uses all processors. Joblib’s threading backend is used.

  • verbose (int, default=0) – Verbosity of the joblib Parallel driver.

  • tao_n_runs (int)

  • tao_lambda (float)

estimators_

The collection of fitted base estimators.

Type:

list of SGTRegressor

n_features_in_

Number of features seen during fit().

Type:

int

mean_feature_importances_

Mean of per-tree feature_importances_, aligned with processed_features_.

Type:

ndarray of shape (n_logical_features,)

std_feature_importance_

Population standard deviation of per-tree importances across the forest (same alignment as mean_feature_importances_).

Type:

ndarray of shape (n_logical_features,)

processed_features_

Logical features resolved once and shared by every base tree.

Type:

ProcessedFeatures

See also

sgtlearn.SGTRegressor

Single-tree base estimator.

sgtlearn.ensemble.RandomSGForestClassifier

Classification counterpart.

sklearn.ensemble.RandomForestRegressor

Standard CART forest with the same prediction semantics.

Examples

>>> from sklearn.datasets import make_regression
>>> from sgtlearn.ensemble import RandomSGForestRegressor
>>> X, y = make_regression(n_samples=500, random_state=0)
>>> reg = RandomSGForestRegressor(n_estimators=20, random_state=0).fit(X, y)
>>> reg.predict(X[:5]).shape
(5,)
set_fit_request(*, feature_dict='$UNCHANGED$', processed_features='$UNCHANGED$', sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
  • feature_dict (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for feature_dict parameter in fit.

  • processed_features (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for processed_features parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

  • self (RandomSGForestRegressor)

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

  • self (RandomSGForestRegressor)

Returns:

self – The updated object.

Return type:

object