CSBaggingClassifier#
- class empulse.models.CSBaggingClassifier(estimator=None, *, final_estimator=None, n_estimators=10, tp_cost=0.0, tn_cost=0.0, fn_cost=0.0, fp_cost=0.0, max_samples=0.5, max_features='auto', combination='majority_voting', bootstrap=True, bootstrap_features=False, n_jobs=1, verbose=False, random_state=None)[source]#
Bagging classifier to optimize instance-dependent cost loss.
- Parameters:
- estimatorestimator, default=None
The base estimator to fit on random subsets of the dataset.
- final_estimatorestimator, default=None
The estimator to train on the weighted combination of the base estimators. By default, a Cost Sensitive Logistic Regression is used. Only used if
combination
is"stacking"
or"stacking_proba"
.- n_estimatorsint, default=10
The number of base estimators in the ensemble.
- tp_costfloat or array-like, shape=(n_samples,), default=0.0
Cost of true positives. If
float
, then all true positives have the same cost. If array-like, then it is the cost of each true positive classification. Is overwritten if another tp_cost is passed to thefit
method.Note
It is not recommended to pass instance-dependent costs to the
__init__
method. Instead, pass them to thefit
method.- fp_costfloat or array-like, shape=(n_samples,), default=0.0
Cost of false positives. If
float
, then all false positives have the same cost. If array-like, then it is the cost of each false positive classification. Is overwritten if another fp_cost is passed to thefit
method.Note
It is not recommended to pass instance-dependent costs to the
__init__
method. Instead, pass them to thefit
method.- tn_costfloat or array-like, shape=(n_samples,), default=0.0
Cost of true negatives. If
float
, then all true negatives have the same cost. If array-like, then it is the cost of each true negative classification. Is overwritten if another tn_cost is passed to thefit
method.Note
It is not recommended to pass instance-dependent costs to the
__init__
method. Instead, pass them to thefit
method.- fn_costfloat or array-like, shape=(n_samples,), default=0.0
Cost of false negatives. If
float
, then all false negatives have the same cost. If array-like, then it is the cost of each false negative classification. Is overwritten if another fn_cost is passed to thefit
method.Note
It is not recommended to pass instance-dependent costs to the
__init__
method. Instead, pass them to thefit
method.- max_samplesint or float, default=1.0
The number of samples to draw from X to train each base estimator.
If None (default), then draw
X.shape[0]
samples.If int, then draw max_samples samples.
If float, then draw
max(round(n_samples * max_samples), 1)
samples. Thus,max_samples
should be in the interval(0.0, 1.0]
.
- combinationstring, default=”majority_voting”
Which combination method to use:
If
"majority_voting"
then combine by majority votingIf
"weighted_voting"
then combine by weighted voting using the out of bag savings as the weight for each estimator.If
"stacking"
then thefinal_estimator
is used to learn the combination.If
"stacking_proba"
then thefinal_estimator
is trained with the estimated probabilities is used to learn the combination.
- bootstrap: bool, default=True
Whether samples are drawn with replacement. If False, sampling without replacement is performed.
- bootstrap_features: bool, default=False
Whether features are drawn with replacement.
- n_jobsint, default=1
The number of jobs to run in parallel for both
fit
andpredict
. If -1, then the number of jobs is set to the number of cores.- verboseint, default=0
Controls the verbosity of the building process.
- random_stateint, RandomState instance or None, default=None
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used by np.random.
- Attributes:
- estimator_: estimator
The base estimator from which the ensemble is grown.
- estimators_: list of estimators
The collection of fitted base estimators.
- estimators_samples_: list of arrays
The subset of drawn samples (i.e., the in-bag samples) for each base estimator.
- estimators_features_: list of arrays
The subset of drawn features for each base estimator.
References
[1]Correa Bahnsen, A., Aouada, D., & Ottersten, B. “Ensemble of Example-Dependent Cost-Sensitive Decision Trees”, 2015, http://arxiv.org/abs/1505.04637.
- fit(X, y, *, tp_cost=Parameter.UNCHANGED, tn_cost=Parameter.UNCHANGED, fn_cost=Parameter.UNCHANGED, fp_cost=Parameter.UNCHANGED)#
Build a Bagging ensemble of estimators from the training set (X, y).
- Parameters:
- X{array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrices are accepted only if they are supported by the base estimator.
- yarray-like, shape = [n_samples]
The target values.
- tp_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$
Cost of true positives. If
float
, then all true positives have the same cost. If array-like, then it is the cost of each true positive classification.- fp_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$
Cost of false positives. If
float
, then all false positives have the same cost. If array-like, then it is the cost of each false positive classification.- tn_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$
Cost of true negatives. If
float
, then all true negatives have the same cost. If array-like, then it is the cost of each true negative classification.- fn_costfloat or array-like, shape=(n_samples,), default=$UNCHANGED$
Cost of false negatives. If
float
, then all false negatives have the same cost. If array-like, then it is the cost of each false negative classification.
- Returns:
- selfobject
Returns self.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- predict(X)#
Predict class for X.
The predicted class of an input sample is computed as the class with the highest mean predicted probability. If base estimators do not implement a
predict_proba
method, then it resorts to voting.- Parameters:
- X{array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrices are accepted only if they are supported by the base estimator.
- Returns:
- predarray of shape = [n_samples]
The predicted classes.
- predict_proba(X)#
Predict class probabilities for X.
The predicted class probabilities of an input sample is computed as the mean predicted class probabilities of the base estimators in the ensemble. If base estimators do not implement a
predict_proba
method, then it resorts to voting and the predicted class probabilities of an input sample represents the proportion of estimators predicting each class.- Parameters:
- X{array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrices are accepted only if they are supported by the base estimator.
- Returns:
- parray of shape = [n_samples, n_classes]
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
- score(X, y, sample_weight=None)#
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.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Test samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
True labels for X.
- sample_weightarray-like of shape (n_samples,), default=None
Sample weights.
- Returns:
- scorefloat
Mean accuracy of
self.predict(X)
w.r.t. y.
- set_fit_request(*, fn_cost='$UNCHANGED$', fp_cost='$UNCHANGED$', tn_cost='$UNCHANGED$', tp_cost='$UNCHANGED$')#
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- fn_coststr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
fn_cost
parameter infit
.- fp_coststr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
fp_cost
parameter infit
.- tn_coststr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
tn_cost
parameter infit
.- tp_coststr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
tp_cost
parameter infit
.
- Returns:
- selfobject
The updated object.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- set_score_request(*, sample_weight='$UNCHANGED$')#
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.