Expand source code
from sklearn.ensemble import AdaBoostClassifier, AdaBoostRegressor
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from imodels.util.arguments import check_fit_arguments
class BoostedRulesClassifier(AdaBoostClassifier):
'''An easy-interpretable classifier optimizing simple logical rules.
Params
------
estimator: object with fit and predict methods
Defaults to DecisionTreeClassifier with AdaBoost.
For SLIPPER, should pass estimator=imodels.SlipperBaseEstimator
'''
def __init__(
self,
estimator=DecisionTreeClassifier(max_depth=1),
*,
n_estimators=15,
learning_rate=1.0,
random_state=None,
):
try: # sklearn version >= 1.2
super().__init__(
estimator=estimator,
n_estimators=n_estimators,
learning_rate=learning_rate,
random_state=random_state,
)
except: # sklearn version < 1.2
super().__init__(
base_estimator=estimator,
n_estimators=n_estimators,
learning_rate=learning_rate,
random_state=random_state,
)
self.estimator = estimator
def get_rules(self, feature_names=None):
"""Return this model's rules as a DataFrame (see imodels.get_rules)."""
from imodels.util.get_rules import get_rules
return get_rules(self, feature_names=feature_names)
def apply(self, X):
"""Return the leaf each sample reaches (see imodels.util.apply.apply_leaves)."""
from imodels.util.apply import apply_leaves
return apply_leaves(self, X)
def fit(self, X, y, feature_names=None, **kwargs):
X, y, feature_names = check_fit_arguments(self, X, y, feature_names)
classes = self.classes_ # super().fit overwrites this with the encoded labels
names_in = getattr(self, 'feature_names_in_', None)
super().fit(X, y, **kwargs)
self.classes_ = classes
if names_in is not None: # super().fit strips this when passed a plain array
self.feature_names_in_ = names_in
self.complexity_ = len(self.estimators_)
return self
class BoostedRulesRegressor(AdaBoostRegressor):
'''An easy-interpretable regressor optimizing simple logical rules.
Params
------
estimator: object with fit and predict methods
Defaults to DecisionTreeRegressor with AdaBoost.
'''
def __init__(
self,
estimator=DecisionTreeRegressor(max_depth=1),
*,
n_estimators=15,
learning_rate=1.0,
random_state=13,
):
try: # sklearn version >= 1.2
super().__init__(
estimator=estimator,
n_estimators=n_estimators,
learning_rate=learning_rate,
random_state=random_state,
)
except: # sklearn version < 1.2
super().__init__(
base_estimator=estimator,
n_estimators=n_estimators,
learning_rate=learning_rate,
random_state=random_state,
)
self.estimator = estimator
def get_rules(self, feature_names=None):
"""Return this model's rules as a DataFrame (see imodels.get_rules)."""
from imodels.util.get_rules import get_rules
return get_rules(self, feature_names=feature_names)
def apply(self, X):
"""Return the leaf each sample reaches (see imodels.util.apply.apply_leaves)."""
from imodels.util.apply import apply_leaves
return apply_leaves(self, X)
def fit(self, X, y, feature_names=None, **kwargs):
X, y, feature_names = check_fit_arguments(self, X, y, feature_names)
names_in = getattr(self, 'feature_names_in_', None)
super().fit(X, y, **kwargs)
if names_in is not None: # super().fit strips this when passed a plain array
self.feature_names_in_ = names_in
self.complexity_ = len(self.estimators_)
return self
Classes
class BoostedRulesClassifier (estimator=DecisionTreeClassifier(max_depth=1), *, n_estimators=15, learning_rate=1.0, random_state=None)-
An easy-interpretable classifier optimizing simple logical rules.
Params
estimator: object with fit and predict methods Defaults to DecisionTreeClassifier with AdaBoost. For SLIPPER, should pass estimator=imodels.SlipperBaseEstimator
Expand source code
class BoostedRulesClassifier(AdaBoostClassifier): '''An easy-interpretable classifier optimizing simple logical rules. Params ------ estimator: object with fit and predict methods Defaults to DecisionTreeClassifier with AdaBoost. For SLIPPER, should pass estimator=imodels.SlipperBaseEstimator ''' def __init__( self, estimator=DecisionTreeClassifier(max_depth=1), *, n_estimators=15, learning_rate=1.0, random_state=None, ): try: # sklearn version >= 1.2 super().__init__( estimator=estimator, n_estimators=n_estimators, learning_rate=learning_rate, random_state=random_state, ) except: # sklearn version < 1.2 super().__init__( base_estimator=estimator, n_estimators=n_estimators, learning_rate=learning_rate, random_state=random_state, ) self.estimator = estimator def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def apply(self, X): """Return the leaf each sample reaches (see imodels.util.apply.apply_leaves).""" from imodels.util.apply import apply_leaves return apply_leaves(self, X) def fit(self, X, y, feature_names=None, **kwargs): X, y, feature_names = check_fit_arguments(self, X, y, feature_names) classes = self.classes_ # super().fit overwrites this with the encoded labels names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, **kwargs) self.classes_ = classes if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self.complexity_ = len(self.estimators_) return selfAncestors
- sklearn.ensemble._weight_boosting.AdaBoostClassifier
- sklearn.utils._metadata_requests._RoutingNotSupportedMixin
- sklearn.base.ClassifierMixin
- sklearn.ensemble._weight_boosting.BaseWeightBoosting
- sklearn.ensemble._base.BaseEnsemble
- sklearn.base.MetaEstimatorMixin
- sklearn.base.BaseEstimator
- sklearn.utils._repr_html.base.ReprHTMLMixin
- sklearn.utils._repr_html.base._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
Subclasses
Methods
def apply(self, X)-
Return the leaf each sample reaches (see imodels.util.apply.apply_leaves).
Expand source code
def apply(self, X): """Return the leaf each sample reaches (see imodels.util.apply.apply_leaves).""" from imodels.util.apply import apply_leaves return apply_leaves(self, X) def fit(self, X, y, feature_names=None, **kwargs)-
Build a boosted classifier/regressor from the training set (X, y).
Parameters
X:{array-like, sparse matrix}ofshape (n_samples, n_features)- The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. COO, DOK, and LIL are converted to CSR.
y:array-likeofshape (n_samples,)- The target values.
sample_weight:array-likeofshape (n_samples,), default=None- Sample weights. If None, the sample weights are initialized to 1 / n_samples.
Returns
self:object- Fitted estimator.
Expand source code
def fit(self, X, y, feature_names=None, **kwargs): X, y, feature_names = check_fit_arguments(self, X, y, feature_names) classes = self.classes_ # super().fit overwrites this with the encoded labels names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, **kwargs) self.classes_ = classes if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self.complexity_ = len(self.estimators_) return self def get_rules(self, feature_names=None)-
Return this model's rules as a DataFrame (see imodels.get_rules).
Expand source code
def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def set_fit_request(self: BoostedRulesClassifier, *, feature_names: bool | str | None = '$UNCHANGED$') ‑> BoostedRulesClassifier-
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 :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>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_names:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
feature_namesparameter infit.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -
def set_score_request(self: BoostedRulesClassifier, *, sample_weight: bool | str | None = '$UNCHANGED$') ‑> BoostedRulesClassifier-
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 :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>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,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
sample_weightparameter inscore.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -
class BoostedRulesRegressor (estimator=DecisionTreeRegressor(max_depth=1), *, n_estimators=15, learning_rate=1.0, random_state=13)-
An easy-interpretable regressor optimizing simple logical rules.
Params
estimator: object with fit and predict methods Defaults to DecisionTreeRegressor with AdaBoost.
Expand source code
class BoostedRulesRegressor(AdaBoostRegressor): '''An easy-interpretable regressor optimizing simple logical rules. Params ------ estimator: object with fit and predict methods Defaults to DecisionTreeRegressor with AdaBoost. ''' def __init__( self, estimator=DecisionTreeRegressor(max_depth=1), *, n_estimators=15, learning_rate=1.0, random_state=13, ): try: # sklearn version >= 1.2 super().__init__( estimator=estimator, n_estimators=n_estimators, learning_rate=learning_rate, random_state=random_state, ) except: # sklearn version < 1.2 super().__init__( base_estimator=estimator, n_estimators=n_estimators, learning_rate=learning_rate, random_state=random_state, ) self.estimator = estimator def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def apply(self, X): """Return the leaf each sample reaches (see imodels.util.apply.apply_leaves).""" from imodels.util.apply import apply_leaves return apply_leaves(self, X) def fit(self, X, y, feature_names=None, **kwargs): X, y, feature_names = check_fit_arguments(self, X, y, feature_names) names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, **kwargs) if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self.complexity_ = len(self.estimators_) return selfAncestors
- sklearn.ensemble._weight_boosting.AdaBoostRegressor
- sklearn.utils._metadata_requests._RoutingNotSupportedMixin
- sklearn.base.RegressorMixin
- sklearn.ensemble._weight_boosting.BaseWeightBoosting
- sklearn.ensemble._base.BaseEnsemble
- sklearn.base.MetaEstimatorMixin
- sklearn.base.BaseEstimator
- sklearn.utils._repr_html.base.ReprHTMLMixin
- sklearn.utils._repr_html.base._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
Methods
def apply(self, X)-
Return the leaf each sample reaches (see imodels.util.apply.apply_leaves).
Expand source code
def apply(self, X): """Return the leaf each sample reaches (see imodels.util.apply.apply_leaves).""" from imodels.util.apply import apply_leaves return apply_leaves(self, X) def fit(self, X, y, feature_names=None, **kwargs)-
Build a boosted classifier/regressor from the training set (X, y).
Parameters
X:{array-like, sparse matrix}ofshape (n_samples, n_features)- The training input samples. Sparse matrix can be CSC, CSR, COO, DOK, or LIL. COO, DOK, and LIL are converted to CSR.
y:array-likeofshape (n_samples,)- The target values.
sample_weight:array-likeofshape (n_samples,), default=None- Sample weights. If None, the sample weights are initialized to 1 / n_samples.
Returns
self:object- Fitted estimator.
Expand source code
def fit(self, X, y, feature_names=None, **kwargs): X, y, feature_names = check_fit_arguments(self, X, y, feature_names) names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, **kwargs) if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self.complexity_ = len(self.estimators_) return self def get_rules(self, feature_names=None)-
Return this model's rules as a DataFrame (see imodels.get_rules).
Expand source code
def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def set_fit_request(self: BoostedRulesRegressor, *, feature_names: bool | str | None = '$UNCHANGED$') ‑> BoostedRulesRegressor-
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 :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>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_names:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
feature_namesparameter infit.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -
def set_score_request(self: BoostedRulesRegressor, *, sample_weight: bool | str | None = '$UNCHANGED$') ‑> BoostedRulesRegressor-
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 :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>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,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
sample_weightparameter inscore.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -