define_binary#

mlquantify.multiclass.define_binary(cls)[source]#

Decorator to enable binary quantification extensions (One-vs-Rest or One-vs-One).

This decorator dynamically extends a quantifier class to handle multiclass quantification tasks by decomposing them into multiple binary subproblems, following either the One-vs-Rest (OvR) or One-vs-One (OvO) strategy.

It automatically replaces the class methods fit, predict, and aggregate with binary-aware versions from BinaryQuantifier, while preserving access to the original implementations via _original_fit, _original_predict, and _original_aggregate.

Parameters:
clsclass

A subclass of BaseQuantifier implementing standard binary quantification methods (fit, predict, and aggregate).

Returns:
class

The same class with binary quantification capabilities added.

Examples

>>> from mlquantify.base import BaseQuantifier
>>> from mlquantify.binary import define_binary
>>> @define_binary
... class MyQuantifier(BaseQuantifier):
...     def fit(self, X, y):
...         # Custom binary training logic
...         self.classes_ = np.unique(y)
...         return self
...
...     def predict(self, X):
...         # Return dummy prevalences
...         return np.array([0.4, 0.6])
...
...     def aggregate(self, preds, y_train):
...         # Example aggregation method
...         return np.mean(preds, axis=0)
>>> qtf = MyQuantifier()
>>> qtf.strategy = 'ovr'  # or 'ovo'
>>> X = np.random.randn(10, 5)
>>> y = np.random.randint(0, 3, 10)
>>> qtf.fit(X, y)
MyQuantifier(...)
>>> qtf.predict(X)
array([...])