Expand source code
from typing import List
import pandas as pd
from sklearn.base import ClassifierMixin, RegressorMixin
from imodels.rule_set.rule_fit import RuleFit
from imodels.util.convert import itemsets_to_rules
from imodels.util.extract import extract_fpgrowth
class FPLasso(RuleFit):
def __init__(self,
minsupport=0.1,
maxcardinality=2,
verbose=False,
n_estimators=100,
tree_size=4,
sample_fract='default',
max_rules=2000,
memory_par=0.01,
tree_generator=None,
lin_trim_quantile=0.025,
lin_standardise=True,
exp_rand_tree_size=True,
include_linear=True,
alpha=None,
cv=True,
random_state=None):
# passed by keyword: RuleFit takes cv between alpha and random_state,
# so positional arguments silently put random_state into cv
super().__init__(n_estimators=n_estimators,
tree_size=tree_size,
sample_fract=sample_fract,
max_rules=max_rules,
memory_par=memory_par,
tree_generator=tree_generator,
lin_trim_quantile=lin_trim_quantile,
lin_standardise=lin_standardise,
exp_rand_tree_size=exp_rand_tree_size,
include_linear=include_linear,
alpha=alpha,
cv=cv,
random_state=random_state)
self.minsupport = minsupport
self.maxcardinality = maxcardinality
self.verbose = verbose
def fit(self, X, y=None, feature_names=None, undiscretized_features=[]):
self.undiscretized_features = undiscretized_features
super().fit(X, y, feature_names=feature_names)
return self
def _extract_rules(self, X, y) -> List[str]:
X = pd.DataFrame(X, columns=self.feature_placeholders)
itemsets = extract_fpgrowth(X, minsupport=self.minsupport,
maxcardinality=self.maxcardinality,
verbose=self.verbose)
return itemsets_to_rules(itemsets)
class FPLassoRegressor(FPLasso, RegressorMixin):
def _init_prediction_task(self):
self.prediction_task = 'regression'
class FPLassoClassifier(FPLasso, ClassifierMixin):
def _init_prediction_task(self):
self.prediction_task = 'classification'
Classes
class FPLasso (minsupport=0.1, maxcardinality=2, verbose=False, n_estimators=100, tree_size=4, sample_fract='default', max_rules=2000, memory_par=0.01, tree_generator=None, lin_trim_quantile=0.025, lin_standardise=True, exp_rand_tree_size=True, include_linear=True, alpha=None, cv=True, random_state=None)-
Rulefit class. Rather than using this class directly, should use RuleFitRegressor or RuleFitClassifier
Parameters
n_estimators:int, default=100- Number of trees grown to generate candidate rules.
tree_size:int, default=4- Number of terminal nodes in generated trees. If exp_rand_tree_size=True, this is the mean number of terminal nodes.
sample_fract:floator"default", default="default"- Fraction of randomly chosen training observations used to produce each tree (Friedman and Popescu 2004, Sec. 2).
max_rules:int, default=30- Total number of terms included in the final model (both linear terms and rules). The approximate number of candidate rules generated is also based on this; the actual number is usually lower because of duplicates.
memory_par:float, default=0.01- Scale multiplier (shrinkage factor) applied to each new tree when sequentially induced (Friedman and Popescu 2004, Sec. 2).
tree_generator:estimator, optional- Used as provided to generate the rules, overriding most of the parameters above. Must be GradientBoostingRegressor(), GradientBoostingClassifier() or RandomForestRegressor(). A copy is fitted on the training data, so the object passed in is left untouched (and any previous fit of it is not reused). n_estimators, max_leaf_nodes and random_state are set on that copy while the trees are grown, so tuning them on the generator has no effect.
lin_trim_quantile:float, default=0.025- If lin_standardise is True, this quantile is used to trim linear terms before standardisation.
lin_standardise:bool, default=True- If True, linear terms are standardised as in Friedman and Popescu (Sec. 3.2), by multiplying the winsorised variable by 0.4 / stdev.
exp_rand_tree_size:bool, default=True- If True, each boosted tree has a different maximum number of terminal nodes, drawn from an exponential distribution about tree_size (Sec. 3.3).
include_linear:bool, default=True- Include linear terms as well as rules.
alpha:float, optional- Regularization strength; overrides max_rules.
cv:bool, default=True- Whether to use cross-validation scores to select the regularization strength, out of all values that satisfy max_rules. If False, the least regularization possible is used.
random_state:int, optional- Seed for the random objects, for repeatability.
verbose:int, default=0- Verbosity.
Attributes
rule_ensemble:RuleEnsemble- The rule ensemble.
feature_names:listofstr, optional- The names of the features (columns).
Expand source code
class FPLasso(RuleFit): def __init__(self, minsupport=0.1, maxcardinality=2, verbose=False, n_estimators=100, tree_size=4, sample_fract='default', max_rules=2000, memory_par=0.01, tree_generator=None, lin_trim_quantile=0.025, lin_standardise=True, exp_rand_tree_size=True, include_linear=True, alpha=None, cv=True, random_state=None): # passed by keyword: RuleFit takes cv between alpha and random_state, # so positional arguments silently put random_state into cv super().__init__(n_estimators=n_estimators, tree_size=tree_size, sample_fract=sample_fract, max_rules=max_rules, memory_par=memory_par, tree_generator=tree_generator, lin_trim_quantile=lin_trim_quantile, lin_standardise=lin_standardise, exp_rand_tree_size=exp_rand_tree_size, include_linear=include_linear, alpha=alpha, cv=cv, random_state=random_state) self.minsupport = minsupport self.maxcardinality = maxcardinality self.verbose = verbose def fit(self, X, y=None, feature_names=None, undiscretized_features=[]): self.undiscretized_features = undiscretized_features super().fit(X, y, feature_names=feature_names) return self def _extract_rules(self, X, y) -> List[str]: X = pd.DataFrame(X, columns=self.feature_placeholders) itemsets = extract_fpgrowth(X, minsupport=self.minsupport, maxcardinality=self.maxcardinality, verbose=self.verbose) return itemsets_to_rules(itemsets)Ancestors
- RuleFit
- sklearn.base.BaseEstimator
- sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
- sklearn.base.TransformerMixin
- sklearn.utils._set_output._SetOutputMixin
- RuleSet
- RulesMixin
- TextMixin
Subclasses
Inherited members
class FPLassoClassifier (minsupport=0.1, maxcardinality=2, verbose=False, n_estimators=100, tree_size=4, sample_fract='default', max_rules=2000, memory_par=0.01, tree_generator=None, lin_trim_quantile=0.025, lin_standardise=True, exp_rand_tree_size=True, include_linear=True, alpha=None, cv=True, random_state=None)-
Rulefit class. Rather than using this class directly, should use RuleFitRegressor or RuleFitClassifier
Parameters
n_estimators:int, default=100- Number of trees grown to generate candidate rules.
tree_size:int, default=4- Number of terminal nodes in generated trees. If exp_rand_tree_size=True, this is the mean number of terminal nodes.
sample_fract:floator"default", default="default"- Fraction of randomly chosen training observations used to produce each tree (Friedman and Popescu 2004, Sec. 2).
max_rules:int, default=30- Total number of terms included in the final model (both linear terms and rules). The approximate number of candidate rules generated is also based on this; the actual number is usually lower because of duplicates.
memory_par:float, default=0.01- Scale multiplier (shrinkage factor) applied to each new tree when sequentially induced (Friedman and Popescu 2004, Sec. 2).
tree_generator:estimator, optional- Used as provided to generate the rules, overriding most of the parameters above. Must be GradientBoostingRegressor(), GradientBoostingClassifier() or RandomForestRegressor(). A copy is fitted on the training data, so the object passed in is left untouched (and any previous fit of it is not reused). n_estimators, max_leaf_nodes and random_state are set on that copy while the trees are grown, so tuning them on the generator has no effect.
lin_trim_quantile:float, default=0.025- If lin_standardise is True, this quantile is used to trim linear terms before standardisation.
lin_standardise:bool, default=True- If True, linear terms are standardised as in Friedman and Popescu (Sec. 3.2), by multiplying the winsorised variable by 0.4 / stdev.
exp_rand_tree_size:bool, default=True- If True, each boosted tree has a different maximum number of terminal nodes, drawn from an exponential distribution about tree_size (Sec. 3.3).
include_linear:bool, default=True- Include linear terms as well as rules.
alpha:float, optional- Regularization strength; overrides max_rules.
cv:bool, default=True- Whether to use cross-validation scores to select the regularization strength, out of all values that satisfy max_rules. If False, the least regularization possible is used.
random_state:int, optional- Seed for the random objects, for repeatability.
verbose:int, default=0- Verbosity.
Attributes
rule_ensemble:RuleEnsemble- The rule ensemble.
feature_names:listofstr, optional- The names of the features (columns).
Expand source code
class FPLassoClassifier(FPLasso, ClassifierMixin): def _init_prediction_task(self): self.prediction_task = 'classification'Ancestors
- FPLasso
- RuleFit
- sklearn.base.BaseEstimator
- sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
- sklearn.base.TransformerMixin
- sklearn.utils._set_output._SetOutputMixin
- RuleSet
- RulesMixin
- TextMixin
- sklearn.base.ClassifierMixin
Inherited members
class FPLassoRegressor (minsupport=0.1, maxcardinality=2, verbose=False, n_estimators=100, tree_size=4, sample_fract='default', max_rules=2000, memory_par=0.01, tree_generator=None, lin_trim_quantile=0.025, lin_standardise=True, exp_rand_tree_size=True, include_linear=True, alpha=None, cv=True, random_state=None)-
Rulefit class. Rather than using this class directly, should use RuleFitRegressor or RuleFitClassifier
Parameters
n_estimators:int, default=100- Number of trees grown to generate candidate rules.
tree_size:int, default=4- Number of terminal nodes in generated trees. If exp_rand_tree_size=True, this is the mean number of terminal nodes.
sample_fract:floator"default", default="default"- Fraction of randomly chosen training observations used to produce each tree (Friedman and Popescu 2004, Sec. 2).
max_rules:int, default=30- Total number of terms included in the final model (both linear terms and rules). The approximate number of candidate rules generated is also based on this; the actual number is usually lower because of duplicates.
memory_par:float, default=0.01- Scale multiplier (shrinkage factor) applied to each new tree when sequentially induced (Friedman and Popescu 2004, Sec. 2).
tree_generator:estimator, optional- Used as provided to generate the rules, overriding most of the parameters above. Must be GradientBoostingRegressor(), GradientBoostingClassifier() or RandomForestRegressor(). A copy is fitted on the training data, so the object passed in is left untouched (and any previous fit of it is not reused). n_estimators, max_leaf_nodes and random_state are set on that copy while the trees are grown, so tuning them on the generator has no effect.
lin_trim_quantile:float, default=0.025- If lin_standardise is True, this quantile is used to trim linear terms before standardisation.
lin_standardise:bool, default=True- If True, linear terms are standardised as in Friedman and Popescu (Sec. 3.2), by multiplying the winsorised variable by 0.4 / stdev.
exp_rand_tree_size:bool, default=True- If True, each boosted tree has a different maximum number of terminal nodes, drawn from an exponential distribution about tree_size (Sec. 3.3).
include_linear:bool, default=True- Include linear terms as well as rules.
alpha:float, optional- Regularization strength; overrides max_rules.
cv:bool, default=True- Whether to use cross-validation scores to select the regularization strength, out of all values that satisfy max_rules. If False, the least regularization possible is used.
random_state:int, optional- Seed for the random objects, for repeatability.
verbose:int, default=0- Verbosity.
Attributes
rule_ensemble:RuleEnsemble- The rule ensemble.
feature_names:listofstr, optional- The names of the features (columns).
Expand source code
class FPLassoRegressor(FPLasso, RegressorMixin): def _init_prediction_task(self): self.prediction_task = 'regression'Ancestors
- FPLasso
- RuleFit
- sklearn.base.BaseEstimator
- sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
- sklearn.base.TransformerMixin
- sklearn.utils._set_output._SetOutputMixin
- RuleSet
- RulesMixin
- TextMixin
- sklearn.base.RegressorMixin
Inherited members