AggregationMixin#
- class mlquantify.base_aggregative.AggregationMixin[source]#
Mixin class for all aggregative quantifiers.
An aggregative quantifier is a quantifier that relies on an underlying supervised learner to produce predictions on which the quantification is then performed.
Inheriting from this mixin provides learner validation and setting parameters also for the learner (used by
GridSearchQand friends).This mixin also sets the
has_estimatorandrequires_fittags toTrue.Notes
An aggregative quantifier must have a ‘learner’ attribute that is a supervised learning estimator.
Depending on the type of predictions required from the learner, the quantifier can be further classified as a ‘soft’ or ‘crisp’ aggregative quantifier.
Read more in the User Guide for more details.
Examples
>>> from mlquantify.base import BaseQuantifier, AggregationMixin >>> from sklearn.linear_model import LogisticRegression >>> import numpy as np >>> class MyAggregativeQuantifier(AggregationMixin, BaseQuantifier): ... def __init__(self, learner=None): ... self.learner = learner if learner is not None else LogisticRegression() ... def fit(self, X, y): ... self.learner.fit(X, y) ... self.classes_ = np.unique(y) ... return self ... def predict(self, X): ... preds = self.learner.predict(X) ... _, counts = np.unique(preds, return_counts=True) ... prevalence = counts / counts.sum() ... return prevalence >>> quantifier = MyAggregativeQuantifier() >>> X = np.random.rand(100, 10) >>> y = np.random.randint(0, 2, size=100) >>> quantifier.fit(X, y).predict(X) [0.5 0.5]