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,RandomSGForestRandom forest of Shape Generalized Trees for classification.
Bootstrap ensemble of
sgtlearn.SGTClassifierbase estimators. Prediction semantics matchsklearn.ensemble.RandomForestClassifier: class probabilities are the mean of per-treepredict_proba(), andpredict()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 followRandomForestClassifierconvention. Seesgtlearn.SGTClassifierfor the full semantics.bootstrap (bool, default=True) – If
True, each tree is fit on a bootstrap resample (with replacement) of the training set. IfFalse, every tree is fit on the full training set — diversity then comes only fromrandom_stateandmax_features.max_samples (int or float, optional) – Size of each bootstrap sample.
intgives an absolute count;floatin(0, 1]gives a fraction ofn_samples.None(default) usesn_samples. Only valid whenbootstrap=True.random_state (int, RandomState, optional) – Controls bootstrap resampling and the per-tree seeds.
class_weight (dict, optional) – Per-class weights multiplied into
sample_weightbefore training. Keys are class labels as iny.n_jobs (int, optional) – Number of joblib workers used to fit trees.
Nonemeans one job (sequential);-1uses all processors. Joblib’s threading backend is used (the per-tree work is dominated by the native C++ trainer, same rationale asRandomForestClassifier).verbose (int, default=0) – Verbosity of the joblib
Paralleldriver.tao_n_runs (int)
tao_lambda (float)
- estimators_¶
The collection of fitted base estimators.
- Type:
- classes_¶
Class labels in original training label space.
- Type:
ndarray of shape (n_classes,)
- mean_feature_importances_¶
Mean of per-tree
feature_importances_, aligned withprocessed_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:
See also
sgtlearn.SGTClassifierSingle-tree base estimator.
sgtlearn.ensemble.RandomSGForestRegressorRegression counterpart.
sklearn.ensemble.RandomForestClassifierStandard 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
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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_dictparameter infit.processed_features (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
processed_featuresparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.self (RandomSGForestClassifier)
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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_weightparameter inscore.self (RandomSGForestClassifier)
- Returns:
self – The updated object.
- Return type:
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,RandomSGForestRandom forest of Shape Generalized Trees for regression.
Bootstrap ensemble of
sgtlearn.SGTRegressorbase estimators. Prediction semantics matchsklearn.ensemble.RandomForestRegressor:predict()returns the mean of the per-treepredict()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 thatcoordinate_descent_smart_initis ignored by the regression trainer.coordinate_descent_patience (see
sgtlearn.SGTRegressor) – Coordinate-descent controls forwarded to each base estimator. Note thatcoordinate_descent_smart_initis ignored by the regression trainer.coordinate_descent_smart_init (see
sgtlearn.SGTRegressor) – Coordinate-descent controls forwarded to each base estimator. Note thatcoordinate_descent_smart_initis 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 followRandomForestRegressorconvention. Seesgtlearn.SGTRegressorfor the full semantics.bootstrap (bool, default=True) – If
True, each tree is fit on a bootstrap resample (with replacement) of the training set. IfFalse, every tree is fit on the full training set — diversity then comes only fromrandom_stateandmax_features.max_samples (int or float, optional) – Size of each bootstrap sample.
intgives an absolute count;floatin(0, 1]gives a fraction ofn_samples.None(default) usesn_samples. Only valid whenbootstrap=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.
Nonemeans one job (sequential);-1uses all processors. Joblib’s threading backend is used.verbose (int, default=0) – Verbosity of the joblib
Paralleldriver.tao_n_runs (int)
tao_lambda (float)
- estimators_¶
The collection of fitted base estimators.
- Type:
list of SGTRegressor
- mean_feature_importances_¶
Mean of per-tree
feature_importances_, aligned withprocessed_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:
See also
sgtlearn.SGTRegressorSingle-tree base estimator.
sgtlearn.ensemble.RandomSGForestClassifierClassification counterpart.
sklearn.ensemble.RandomForestRegressorStandard 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
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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_dictparameter infit.processed_features (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
processed_featuresparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.self (RandomSGForestRegressor)
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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_weightparameter inscore.self (RandomSGForestRegressor)
- Returns:
self – The updated object.
- Return type: