API Reference / algorithms / rules /

zero_rule.py

ZeroRuleClassifier classifier implementation.

Classes

ZeroRuleClassifier

class algorithms.rules.zero_rule.ZeroRuleClassifier(Classifier)

Zero Rule classifier that predicts the most frequent class, ignoring all input features.

ZeroRuleClassifier is the simplest classification method which relies only on the target distribution and ignores all predictors. It simply predicts the majority class (mode) for classification tasks. This classifier is useful as a baseline -- any classifier that cannot beat ZeroRuleClassifier should be discarded.
Constructor
__init__(
    self,
)

Overview

The algorithm operates as follows:

  1. During training, compute the frequency of each class label
  2. Store the majority class (the class with the highest count)
  3. At prediction time, return the majority class for every instance

Theory

The prediction for all instances is the mode of the training labels:

\hat{y} = \arg\max_{c \in C} \sum_{i=1}^{n} \mathbb{1}[y_i = c]

The expected error rate of ZeroRuleClassifier equals the proportion of non-majority instances:

\text{Error} = 1 - \frac{\max_{c} n_c}{n}

where n_c is the count of class c and n is the total number of training instances.

Attributes

majority_class_
any
The most frequent class in training data.
class_counts_
dict
Count of each class in training data.
classes_
np.ndarray
Unique classes in training data.

Notes

Complexity:

  • Training: O(n) where n = number of samples
  • Prediction: O(1) per sample
When to use ZeroRuleClassifier:
  • As the absolute baseline for any classification task
  • To establish the minimum acceptable accuracy threshold
  • When comparing classifier performance: any useful model must beat it
  • For sanity-checking evaluation pipelines

References

Witten2011
Witten, I.H., Frank, E. and Hall, M.A. (2011). Data Mining: Practical Machine Learning Tools and Techniques. Morgan Kaufmann, 3rd Edition. DOI: 10.1016/C2009-0-19715-5

See Also

Basic usage as a baseline classifier:

python
>>> from tuiml.algorithms.rules import ZeroRuleClassifier
>>> import numpy as np
>>>
>>> # Create sample data
>>> X_train = np.array([[1, 2], [2, 3], [3, 1], [4, 3], [5, 2]])
>>> y_train = np.array([0, 0, 1, 1, 1])
>>>
>>> # Fit the model
>>> clf = ZeroRuleClassifier()
>>> clf.fit(X_train, y_train)
ZeroRuleClassifier(...)
>>> # Predicts majority class (1) for all instances
>>> predictions = clf.predict(X_train)
>>> print(predictions)
[1 1 1 1 1]

Methods

__init__ (self)

Initialize ZeroRuleClassifier classifier.

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema (ZeroRuleClassifier has no parameters).

get_capabilities (cls) -> List[str]

Return classifier capabilities.

get_complexity (cls) -> str

Return time/space complexity.

get_references (cls) -> List[str]

Return academic references.

fit (self, X: np.ndarray, y: np.ndarray) -> 'ZeroRuleClassifier'

Fit the ZeroRuleClassifier classifier.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Training features. Ignored by ZeroRuleClassifier.
y
np.ndarray of shape (n_samples,)
Target labels.
Returns
self
ZeroRuleClassifier
Returns the fitted instance.
predict (self, X: np.ndarray) -> np.ndarray

Predict the majority class for all samples.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Test features.
Returns
y_pred
np.ndarray of shape (n_samples,)
Array of predictions containing the majority class value.
predict_proba (self, X: np.ndarray) -> np.ndarray

Predict class probabilities.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Test features.
Returns
proba
np.ndarray of shape (n_samples, n_classes)
Class probabilities based on training distribution.
__repr__ (self) -> str

String representation.