binary_quantifier#
- mlquantify.multiclass.binary_quantifier(_cls=None, *, strategy_attr='strategy')[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, andaggregatewith binary-aware versions fromBinaryQuantifier, while preserving access to the original implementations via_original_fit,_original_predict, and_original_aggregate.- Parameters:
- clsclass
A subclass of
BaseQuantifierimplementing standard binary quantification methods (fit,predict, andaggregate).- strategy_attrstr, default=”strategy”
Name of the attribute that stores the multiclass decomposition strategy.
- Returns:
- class
The same class with binary quantification capabilities added.
Examples
>>> from mlquantify.base import BaseQuantifier >>> from mlquantify.multiclass import binary_quantifier
>>> @binary_quantifier(strategy_attr="strategy") ... 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([...])