Module imodels.rule_list.corels_wrapper
Expand source code
# This is just a simple wrapper around pycorels: https://github.com/corels/pycorels
from corels import CorelsClassifier
from sklearn.base import BaseEstimator
class CorelsRuleListClassifier(BaseEstimator, CorelsClassifier):
"""Certifiably Optimal RulE ListS classifier.
This class implements the CORELS algorithm, designed to produce human-interpretable, optimal
rulelists for binary feature data and binary classification. As an alternative to other
tree based algorithms such as CART, CORELS provides a certificate of optimality for its
rulelist given a training set, leveraging multiple algorithmic bounds to do so.
In order to use run the algorithm, create an instance of the `CorelsClassifier` class,
providing any necessary parameters in its constructor, and then call `fit` to generate
a rulelist. `printrl` prints the generated rulelist, while `predict` provides
classification predictions for a separate test dataset with the same features. To determine
the algorithm's accuracy, run `score` on an evaluation dataset with labels.
To save a generated rulelist to a file, call `save`. To load it back from the file, call `load`.
Attributes
----------
c : float, optional (default=0.01)
Regularization parameter. Higher values penalize longer rulelists.
n_iter : int, optional (default=10000)
Maximum number of nodes (rulelists) to search before exiting.
map_type : str, optional (default="prefix")
The type of prefix map to use. Supported maps are "none" for no map,
"prefix" for a map that uses rule prefixes for keys, "captured" for
a map with a prefix's captured vector as keys.
policy : str, optional (default="lower_bound")
The search policy for traversing the tree (i.e. the criterion with which
to order nodes in the queue). Supported criteria are "bfs", for breadth-first
search; "curious", which attempts to find the most promising node;
"lower_bound" which is the objective function evaluated with that rulelist
minus the default prediction error; "objective" for the objective function
evaluated at that rulelist; and "dfs" for depth-first search.
verbosity : list, optional (default=["rulelist"])
The verbosity levels required. A list of strings, it can contain any
subset of ["rulelist", "rule", "label", "minor", "samples", "progress", "mine", "loud"].
An empty list ([]) indicates 'silent' mode.
- "rulelist" prints the generated rulelist at the end.
- "rule" prints a summary of each rule generated.
- "label" prints a summary of the class labels.
- "minor" prints a summary of the minority bound.
- "samples" produces a complete dump of the rules, label, and/or minor data. You must also provide at least one of "rule", "label", or "minor" to specify which data you want to dump, or "loud" for all data. The "samples" option often spits out a lot of output.
- "progress" prints periodic messages as corels runs.
- "mine" prints debug information while mining rules, including each rule as it is generated.
- "loud" is the equivalent of ["progress", "label", "rule", "mine", "minor"].
ablation : int, optional (default=0)
Specifies addition parameters for the bounds used while searching. Accepted
values are 0 (all bounds), 1 (no antecedent support bound), and 2 (no
lookahead bound).
max_card : int, optional (default=2)
Maximum cardinality allowed when mining rules. Can be any value greater than
or equal to 1. For instance, a value of 2 would only allow rules that combine
at most two features in their antecedents.
min_support : float, optional (default=0.01)
The fraction of samples that a rule must capture in order to be used. 1 minus
this value is also the maximum fraction of samples a rule can capture.
Can be any value between 0.0 and 0.5.
References
----------
Elaine Angelino, Nicholas Larus-Stone, Daniel Alabi, Margo Seltzer, and Cynthia Rudin.
Learning Certifiably Optimal Rule Lists for Categorical Data. KDD 2017.
Journal of Machine Learning Research, 2018; 19: 1-77. arXiv:1704.01701, 2017
Examples
--------
>>> import numpy as np
>>> from corels import CorelsClassifier
>>> X = np.array([ [1, 0, 1], [0, 1, 0], [1, 1, 1] ])
>>> y = np.array([ 1, 0, 1])
>>> c = CorelsRuleListClassifier(verbosity=[])
>>> c.fit(X, y)
...
>>> print(c.predict(X))
[ True False True ]
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_complexity(self):
return None
def fit(self, X, y, feature_names=[], prediction_name="prediction"):
"""
Build a CORELS classifier from the training set (X, y).
Parameters
----------
X : array-like, shape = [n_samples, n_features]
The training input samples. All features must be binary, and the matrix
is internally converted to dtype=np.uint8.
y : array-line, shape = [n_samples]
The target values for the training input. Must be binary.
feature_names : list, optional(default=[])
A list of strings of length n_features. Specifies the names of each
of the features. If an empty list is provided, the feature names
are set to the default of ["feature1", "feature2"... ].
prediction_name : string, optional(default="prediction")
The name of the feature that is being predicted.
Returns
-------
self : obj
"""
super().fit(X, y, features=feature_names, prediction_name=prediction_name)
def predict(self, X):
"""
Predict classifications of the input samples X.
Arguments
---------
X : array-like, shape = [n_samples, n_features]
The training input samples. All features must be binary, and the matrix
is internally converted to dtype=np.uint8. The features must be the same
as those of the data used to train the model.
Returns
-------
p : array[int] of shape = [n_samples].
The classifications of the input samples.
"""
return super().predict(X).astype(int)
Classes
class CorelsRuleListClassifier (*args, **kwargs)
-
Certifiably Optimal RulE ListS classifier. This class implements the CORELS algorithm, designed to produce human-interpretable, optimal rulelists for binary feature data and binary classification. As an alternative to other tree based algorithms such as CART, CORELS provides a certificate of optimality for its rulelist given a training set, leveraging multiple algorithmic bounds to do so. In order to use run the algorithm, create an instance of the
CorelsClassifier
class, providing any necessary parameters in its constructor, and then callfit
to generate a rulelist.printrl
prints the generated rulelist, whilepredict
provides classification predictions for a separate test dataset with the same features. To determine the algorithm's accuracy, runscore
on an evaluation dataset with labels. To save a generated rulelist to a file, callsave
. To load it back from the file, callload
. Attributes
c
:float
, optional (default=0.01
)- Regularization parameter. Higher values penalize longer rulelists.
n_iter
:int
, optional (default=10000
)- Maximum number of nodes (rulelists) to search before exiting.
map_type
:str
, optional (default="prefix"
)- The type of prefix map to use. Supported maps are "none" for no map, "prefix" for a map that uses rule prefixes for keys, "captured" for a map with a prefix's captured vector as keys.
policy
:str
, optional (default="lower_bound"
)- The search policy for traversing the tree (i.e. the criterion with which to order nodes in the queue). Supported criteria are "bfs", for breadth-first search; "curious", which attempts to find the most promising node; "lower_bound" which is the objective function evaluated with that rulelist minus the default prediction error; "objective" for the objective function evaluated at that rulelist; and "dfs" for depth-first search.
verbosity
:list
, optional (default=["rulelist"
])- The verbosity levels required. A list of strings, it can contain any subset of ["rulelist", "rule", "label", "minor", "samples", "progress", "mine", "loud"]. An empty list ([]) indicates 'silent' mode. - "rulelist" prints the generated rulelist at the end. - "rule" prints a summary of each rule generated. - "label" prints a summary of the class labels. - "minor" prints a summary of the minority bound. - "samples" produces a complete dump of the rules, label, and/or minor data. You must also provide at least one of "rule", "label", or "minor" to specify which data you want to dump, or "loud" for all data. The "samples" option often spits out a lot of output. - "progress" prints periodic messages as corels runs. - "mine" prints debug information while mining rules, including each rule as it is generated. - "loud" is the equivalent of ["progress", "label", "rule", "mine", "minor"].
ablation
:int
, optional (default=0
)- Specifies addition parameters for the bounds used while searching. Accepted values are 0 (all bounds), 1 (no antecedent support bound), and 2 (no lookahead bound).
max_card
:int
, optional (default=2
)- Maximum cardinality allowed when mining rules. Can be any value greater than or equal to 1. For instance, a value of 2 would only allow rules that combine at most two features in their antecedents.
min_support
:float
, optional (default=0.01
)- The fraction of samples that a rule must capture in order to be used. 1 minus this value is also the maximum fraction of samples a rule can capture. Can be any value between 0.0 and 0.5.
References
Elaine Angelino, Nicholas Larus-Stone, Daniel Alabi, Margo Seltzer, and Cynthia Rudin. Learning Certifiably Optimal Rule Lists for Categorical Data. KDD 2017. Journal of Machine Learning Research, 2018; 19: 1-77. arXiv:1704.01701, 2017 Examples
>>> import numpy as np >>> from corels import CorelsClassifier >>> X = np.array([ [1, 0, 1], [0, 1, 0], [1, 1, 1] ]) >>> y = np.array([ 1, 0, 1]) >>> c = CorelsRuleListClassifier(verbosity=[]) >>> c.fit(X, y) ... >>> print(c.predict(X)) [ True False True ]
Expand source code
class CorelsRuleListClassifier(BaseEstimator, CorelsClassifier): """Certifiably Optimal RulE ListS classifier. This class implements the CORELS algorithm, designed to produce human-interpretable, optimal rulelists for binary feature data and binary classification. As an alternative to other tree based algorithms such as CART, CORELS provides a certificate of optimality for its rulelist given a training set, leveraging multiple algorithmic bounds to do so. In order to use run the algorithm, create an instance of the `CorelsClassifier` class, providing any necessary parameters in its constructor, and then call `fit` to generate a rulelist. `printrl` prints the generated rulelist, while `predict` provides classification predictions for a separate test dataset with the same features. To determine the algorithm's accuracy, run `score` on an evaluation dataset with labels. To save a generated rulelist to a file, call `save`. To load it back from the file, call `load`. Attributes ---------- c : float, optional (default=0.01) Regularization parameter. Higher values penalize longer rulelists. n_iter : int, optional (default=10000) Maximum number of nodes (rulelists) to search before exiting. map_type : str, optional (default="prefix") The type of prefix map to use. Supported maps are "none" for no map, "prefix" for a map that uses rule prefixes for keys, "captured" for a map with a prefix's captured vector as keys. policy : str, optional (default="lower_bound") The search policy for traversing the tree (i.e. the criterion with which to order nodes in the queue). Supported criteria are "bfs", for breadth-first search; "curious", which attempts to find the most promising node; "lower_bound" which is the objective function evaluated with that rulelist minus the default prediction error; "objective" for the objective function evaluated at that rulelist; and "dfs" for depth-first search. verbosity : list, optional (default=["rulelist"]) The verbosity levels required. A list of strings, it can contain any subset of ["rulelist", "rule", "label", "minor", "samples", "progress", "mine", "loud"]. An empty list ([]) indicates 'silent' mode. - "rulelist" prints the generated rulelist at the end. - "rule" prints a summary of each rule generated. - "label" prints a summary of the class labels. - "minor" prints a summary of the minority bound. - "samples" produces a complete dump of the rules, label, and/or minor data. You must also provide at least one of "rule", "label", or "minor" to specify which data you want to dump, or "loud" for all data. The "samples" option often spits out a lot of output. - "progress" prints periodic messages as corels runs. - "mine" prints debug information while mining rules, including each rule as it is generated. - "loud" is the equivalent of ["progress", "label", "rule", "mine", "minor"]. ablation : int, optional (default=0) Specifies addition parameters for the bounds used while searching. Accepted values are 0 (all bounds), 1 (no antecedent support bound), and 2 (no lookahead bound). max_card : int, optional (default=2) Maximum cardinality allowed when mining rules. Can be any value greater than or equal to 1. For instance, a value of 2 would only allow rules that combine at most two features in their antecedents. min_support : float, optional (default=0.01) The fraction of samples that a rule must capture in order to be used. 1 minus this value is also the maximum fraction of samples a rule can capture. Can be any value between 0.0 and 0.5. References ---------- Elaine Angelino, Nicholas Larus-Stone, Daniel Alabi, Margo Seltzer, and Cynthia Rudin. Learning Certifiably Optimal Rule Lists for Categorical Data. KDD 2017. Journal of Machine Learning Research, 2018; 19: 1-77. arXiv:1704.01701, 2017 Examples -------- >>> import numpy as np >>> from corels import CorelsClassifier >>> X = np.array([ [1, 0, 1], [0, 1, 0], [1, 1, 1] ]) >>> y = np.array([ 1, 0, 1]) >>> c = CorelsRuleListClassifier(verbosity=[]) >>> c.fit(X, y) ... >>> print(c.predict(X)) [ True False True ] """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def get_complexity(self): return None def fit(self, X, y, feature_names=[], prediction_name="prediction"): """ Build a CORELS classifier from the training set (X, y). Parameters ---------- X : array-like, shape = [n_samples, n_features] The training input samples. All features must be binary, and the matrix is internally converted to dtype=np.uint8. y : array-line, shape = [n_samples] The target values for the training input. Must be binary. feature_names : list, optional(default=[]) A list of strings of length n_features. Specifies the names of each of the features. If an empty list is provided, the feature names are set to the default of ["feature1", "feature2"... ]. prediction_name : string, optional(default="prediction") The name of the feature that is being predicted. Returns ------- self : obj """ super().fit(X, y, features=feature_names, prediction_name=prediction_name) def predict(self, X): """ Predict classifications of the input samples X. Arguments --------- X : array-like, shape = [n_samples, n_features] The training input samples. All features must be binary, and the matrix is internally converted to dtype=np.uint8. The features must be the same as those of the data used to train the model. Returns ------- p : array[int] of shape = [n_samples]. The classifications of the input samples. """ return super().predict(X).astype(int)
Ancestors
- sklearn.base.BaseEstimator
- corels.corels.CorelsClassifier
Methods
def fit(self, X, y, feature_names=[], prediction_name='prediction')
-
Build a CORELS classifier from the training set (X, y). Parameters
X
:array
-like
,shape
= [n_samples
,n_features
]- The training input samples. All features must be binary, and the matrix is internally converted to dtype=np.uint8.
y
:array
-line
,shape
= [n_samples
]- The target values for the training input. Must be binary.
feature_names
:list
, optional(default=[])- A list of strings of length n_features. Specifies the names of each of the features. If an empty list is provided, the feature names are set to the default of ["feature1", "feature2"… ].
prediction_name
:string
, optional(default="prediction"
)- The name of the feature that is being predicted.
Returns
self
:obj
Expand source code
def fit(self, X, y, feature_names=[], prediction_name="prediction"): """ Build a CORELS classifier from the training set (X, y). Parameters ---------- X : array-like, shape = [n_samples, n_features] The training input samples. All features must be binary, and the matrix is internally converted to dtype=np.uint8. y : array-line, shape = [n_samples] The target values for the training input. Must be binary. feature_names : list, optional(default=[]) A list of strings of length n_features. Specifies the names of each of the features. If an empty list is provided, the feature names are set to the default of ["feature1", "feature2"... ]. prediction_name : string, optional(default="prediction") The name of the feature that is being predicted. Returns ------- self : obj """ super().fit(X, y, features=feature_names, prediction_name=prediction_name)
def get_complexity(self)
-
Expand source code
def get_complexity(self): return None
def predict(self, X)
-
Predict classifications of the input samples X. Arguments
X
:array
-like
,shape
= [n_samples
,n_features
]- The training input samples. All features must be binary, and the matrix is internally converted to dtype=np.uint8. The features must be the same as those of the data used to train the model.
Returns
p : array[int] of shape = [n_samples]. The classifications of the input samples.
Expand source code
def predict(self, X): """ Predict classifications of the input samples X. Arguments --------- X : array-like, shape = [n_samples, n_features] The training input samples. All features must be binary, and the matrix is internally converted to dtype=np.uint8. The features must be the same as those of the data used to train the model. Returns ------- p : array[int] of shape = [n_samples]. The classifications of the input samples. """ return super().predict(X).astype(int)