skmultiflow.rules.VeryFastDecisionRulesClassifier

class skmultiflow.rules.VeryFastDecisionRulesClassifier(expand_confidence=1e-07, ordered_rules=True, grace_period=200, tie_threshold=0.05, rule_prediction='first_hit', nominal_attributes=None, max_rules=1000, nb_threshold=0, nb_prediction=True, drift_detector=None, expand_criterion='info_gain', remove_poor_atts=False, min_weight=100)[source]

Very Fast Decision Rules classifier.

The Very Fast Decision Rules (VFDR) [1] is an incremental rule learning classifier. The learning process of VFDR is similar to that of Hoeffding Tree, but instead of a tree it uses a collection of rules. The core of VFDR is its rules that aim to create a highly interpretable classifier thanks to their nature. Each rule is a conjunction of conditions based on attribute values and the structure for keeping sufficient statistics. The sufficient statistics will determine the class predicted by the rule

IF \(att_1 < 1\) and \(att_2 = 0\) THEN class 0.

The Adaptive Very Fast Decision Rules (AVFDR) is an extension of VFDR capable to adapt to evolving data streams. To adapt with the concept every rule is equipped with a drift detector to monitor it’s performance. If a change is detected then the rule is removed and a new one will be learned.

Parameters
expand_confidence: float (default=0.0000001)
Allowed error in split decision, a value closer to 0 takes longer to decide.
ordered_rules: Bool (default=True)
Whether the created rule set is ordered or not. An ordered set only expands the first rule that fires while the unordered set expands all the rules that fire.
grace_period: int (default=200)
Number of instances a leaf should observe between split attempts.
tie_threshold: float (default=0.05)
Threshold below which a split will be forced to break ties.
rule_prediction: string (default=’first_hit’)
How the class votes are retrieved for prediction. Since more than one rule can fire statistics can be gathered in three ways:
  • ‘first_hit’ - Uses the votes of the first rule that fires.

  • ‘weighted_max’ - Uses the votes of the rules with highest weight that fires.

  • ‘weighted_sum’ - Uses the weighted sum of votes of all the rules that fire.

nominal_attributes: list, optional
List of Nominal attributes. If emtpy, then assume that all attributes are numerical.
max_rules: int (default=20)
Maximum number of rules the model can have.
nb_threshold: int (default=0)
Number of instances a leaf should observe before allowing Naive Bayes.
nb_prediction: Bool (default=True)
Use Naive Bayes as prediction strategy in the leafs, else majority class is uses.
drift_detector: BaseDriftDetector (Default=None)
The drift detector to use in rules. If None detection will be ignored.
If set, the estimator is effectively the Adaptive Very Fast Decision Rules classifier.
Supported detectors: ADWIN, DDM and EDDM.
expand_criterion: SplitCriterion (Default=’info_gain’)
Expand criterion to use:
  • ‘info_gain’ - Information Gain

  • ‘hellinger’ - Hellinger Distance

  • ‘foil_gain’ - Foil Gain

remove_poor_atts: boolean (default=False)
If True, disable poor attributes.

Notes

When using an ordered set, the rules need to be read in order as each rule contain a hidden conjunction which is the opposite of all the rules before it. Also only one rule can fire which restricts the prediction to first hit.

References

1

Petr Kosina and João Gama. 2015. Very fast decision rules for classification in data streams. Data Min. Knowl. Discov. 29, 1 (January 2015), 168-202. DOI=http://dx.doi.org/10.1007/s10618-013-0340-z

Examples

>>> from skmultiflow.rules import VeryFastDecisionRulesClassifier
>>> from skmultiflow.data import AGRAWALGenerator
>>> # Setup the stream
>>> stream = AGRAWALGenerator()
>>> X, y = stream.next_sample(20000)
>>> # Setup the learner
>>> learner = VeryFastDecisionRulesClassifier()
>>> # Train
>>> learner.partial_fit(X, y)
>>> # Print rules
>>> print(learner.get_model_description())
    Rule 0 :Att (2) <= 36.360| class :0  {0: 13867.746092302219}
    Rule 1 :Att (2) > 60.450| class :0  {0: 16174.452447424192}
    Rule 2 :Att (2) <= 39.090| class :0  {0: 2374.5506811568403}
    Rule 3 :Att (2) <= 58.180| class :1  {1: 15082.368305403843}
    Rule 4 :Att (2) <= 59.090| class :1  {1: 767.0}
    Default Rule :| class :0  {0: 837.0}
>>> # Predict
>>> X, y = stream.next_sample(100)
>>> learner.predict(X)
    [0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0
     0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0
     1 1 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 1]

Methods

compute_hoeffding_bound(range_val, confidence, n)

Compute the Hoeffding bound, used to decide how many samples are necessary at each node.

first_hit(self, X)

Get class votes from the first rule that fires.

fit(self, X, y[, classes, sample_weight])

Fit the model.

get_info(self)

Collects and returns the information about the configuration of the estimator

get_model_description(self)

Returns the rules of the model

get_model_measurements(self)

Collect metrics corresponding to the current status of the model.

get_model_rules(self)

Get the rules that describe the model

get_params(self[, deep])

Get parameters for this estimator.

get_votes_for_instance(self, X)

Get class votes for a single instance.

measure_model_size(self[, unit])

new_rule(self[, class_distribution, …])

Create a new rule.

partial_fit(self, X, y[, classes, sample_weight])

Incrementally trains the model.

predict(self, X)

Predicts the label of the instance(s).

predict_proba(self, X)

Predicts probabilities of all label of the instance(s).

reset(self)

Resets the model to its initial state.

score(self, X, y[, sample_weight])

Returns the mean accuracy on the given test data and labels.

set_params(self, **params)

Set the parameters of this estimator.

weighted_max(self, X)

Get class votes from the rule with highest vote weight.

weighted_sum(self, X)

Get class votes from the sum of rules that fires.

Attributes

classes

expand_confidence

grace_period

nb_threshold

nominal_attributes

ordered_rules

remove_poor_atts

rule_prediction

tie_threshold

class Rule(class_distribution, drift_detector, class_idx)[source]

Rule class

A rule is collection of predicates(conditions) that make up the conjunction (the IF part of the rule). The conditions are in the form of: \(Att_{idx} > value\), \(Att_{idx} <= value\) and \(Att_{idx} = value\).

The rule can also track the class distribution and use a drift detector to track change in concept.

Parameters
class_distribution: dict (class_value, weight)

Class observations collected from the instances seen in the rule.

drift_detector: BaseDriftDetector (Default=None)

The drift detector used to signal the change in the concept.

property class_idx
covers_instance(self, X)[source]

Check if the rule covers the instance X.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes to test on the rule.

Returns
Boolean

True if the rule covers the instance else False.

disable_attribute(self, att_idx)[source]

Disable an attribute observer.

Parameters
att_idx: int

Attribute index.

property drift_detector
get_best_expand_suggestion(self, criterion, class_idx)[source]

Find possible expand candidates.

Parameters
criterion: Splitriterion

The criterion used to chose the best expanding suggestion.

class_idx: int or None

if foil gain is used as a criterion class_idx is the class rule tries to learn.

Returns
list

expand candidates.

get_class_votes(self, X, vfdr)[source]

Get the votes per class for a given instance.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes.

vfdr: AVFDR

Very Fast Decision Rules.

Returns
dict (class_value, weight)

Class votes for the given instance.

get_info(self)[source]

Collects and returns the information about the configuration of the estimator

Returns
string

Configuration of the estimator.

get_params(self, deep=True)[source]

Get parameters for this estimator.

Parameters
deepboolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsmapping of string to any

Parameter names mapped to their values.

get_rule(self)[source]

Get the rule

Returns
string

Full description of the rule.

get_weight_seen(self)[source]

Calculate the total weight seen by the node.

Returns
float

Total weight seen.

learn_from_instance(self, X, y, weight, avfdr)[source]

Update the rule with the provided instance. The model class distribution of the model and each attribute are updated. The one for the model is used for prediction and the distributions for the attributes are used for learning Gaussian estimators are used to track distribution of numeric attributes and dict with class count for nominal and the model distributions.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes for updating the node.

y: int

Instance class.

weight: float

Instance weight.

avfdr: AVFDR

Very Fast Decision Rule model.

property observed_class_distribution
predict(self, y)[source]

Provides information about the classification of the rule for the drift detector in order to follow it’s performance.

Parameters
y: int

The true label

Returns
int

1 if the prediction is correct else 0

reset(self)[source]

Resets the estimator to its initial state.

Returns
self
restart(self)[source]

Restarts the rule with initial values

set_params(self, **params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns
self
property weight_seen_at_last_expand

Retrieve the weight seen at last expand evaluation.

Returns
float

Weight seen at last expand evaluation.

static compute_hoeffding_bound(range_val, confidence, n)[source]

Compute the Hoeffding bound, used to decide how many samples are necessary at each node.

Parameters
range_val: float

Range value.

confidence: float

Confidence of choosing the correct attribute.

n: float

Number of samples.

Returns
float

The Hoeffding bound.

Notes

The Hoeffding bound is defined as:

\[\epsilon = \sqrt{\frac{R^2\ln(1/\delta))}{2n}}\]

where:

\(\epsilon\): Hoeffding bound.

\(R\): Range of a random variable. For a probability the range is 1, and for an information gain the range is log c, where c is the number of classes.

\(\delta\): Confidence. 1 minus the desired probability of choosing the correct attribute at any given node.

\(n\): Number of samples.

first_hit(self, X)[source]

Get class votes from the first rule that fires.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes.

Returns
dict (class_value, weight)

The class distribution of the fired rule.

fit(self, X, y, classes=None, sample_weight=None)[source]

Fit the model.

Parameters
Xnumpy.ndarray of shape (n_samples, n_features)

The features to train the model.

y: numpy.ndarray of shape (n_samples, n_targets)

An array-like with the class labels of all samples in X.

classes: numpy.ndarray, optional (default=None)

Contains all possible/known class labels. Usage varies depending on the learning method.

sample_weight: numpy.ndarray, optional (default=None)

Samples weight. If not provided, uniform weights are assumed. Usage varies depending on the learning method.

Returns
self
get_info(self)[source]

Collects and returns the information about the configuration of the estimator

Returns
string

Configuration of the estimator.

get_model_description(self)[source]

Returns the rules of the model

Returns
string

Description of the rules

get_model_measurements(self)[source]

Collect metrics corresponding to the current status of the model.

Returns
string

A string buffer containing the measurements of the model.

get_model_rules(self)[source]

Get the rules that describe the model

Returns
list (rule)
get_params(self, deep=True)[source]

Get parameters for this estimator.

Parameters
deepboolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsmapping of string to any

Parameter names mapped to their values.

get_votes_for_instance(self, X)[source]

Get class votes for a single instance.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes.

Returns
dict (class_value, weight)
new_rule(self, class_distribution=None, drift_detector=None, class_idx=None)[source]

Create a new rule.

Parameters
class_distribution: dict (class_value, weight)

Class observations collected from the instances seen in the rule.

drift_detector: BaseDriftDetector

The drift detector used to signal the change in the concept.

class_idx: int or None

The class the rule is describing.

Returns
Rule:

The created rule.

partial_fit(self, X, y, classes=None, sample_weight=None)[source]

Incrementally trains the model.

Train samples (instances) are composed of X attributes and their corresponding targets y.

Tasks performed before training:

  • Verify instance weight. if not provided, uniform weights (1.0) are assumed.

  • If more than one instance is passed, loop through X and pass instances one at a time.

  • Update weight seen by model.

Training tasks:

  • If the rule_set is empty, update the default_rule and if enough statistics are collected try to create rule.

  • If rules exist in the rule_set, check if they cover the instance. The statistics of the ones that fire are updated using the instance.

  • If enough statistics are collected if a rule then attempt to expand it.

Parameters
X: numpy.ndarray of shape (n_samples, n_features)

Instance attributes.

y: array_like

Classes (targets) for all samples in X.

classes: list or numpy.array

Contains the class values in the stream. If defined, will be used to define the length of the arrays returned by predict_proba

sample_weight: float or array-like

Instance weight. If not provided, uniform weights are assumed.

Returns
self
predict(self, X)[source]

Predicts the label of the instance(s).

Parameters
X: numpy.ndarray of shape (n_samples, n_features)

Samples for which we want to predict the labels.

Returns
numpy.array

Predicted labels for all instances in X.

predict_proba(self, X)[source]

Predicts probabilities of all label of the instance(s).

Parameters
X: numpy.ndarray of shape (n_samples, n_features)

Samples for which we want to predict the labels.

Returns
numpy.array

Predicted the probabilities of all the labels for all instances in X.

reset(self)[source]

Resets the model to its initial state.

Returns
StreamModel

self

score(self, X, y, sample_weight=None)[source]

Returns 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, shape = (n_samples, n_features)

Test samples.

yarray-like, shape = (n_samples) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like, shape = [n_samples], optional

Sample weights.

Returns
scorefloat

Mean accuracy of self.predict(X) wrt. y.

set_params(self, **params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns
self
weighted_max(self, X)[source]

Get class votes from the rule with highest vote weight.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes.

Returns
dict (class_value, weight)

the class distribution from the rule with highest weight.

weighted_sum(self, X)[source]
Get class votes from the sum of rules that fires.

The rules are weighted.

Parameters
X: numpy.ndarray of length equal to the number of features.

Instance attributes.

Returns
dict (class_value, weight)

The class distribution from the sum of the fired rules.