BaseQuantifier#

class mlquantify.base.BaseQuantifier[source]#

Base class for all quantifiers in mlquantify.

Inhering from this class provides default implementations for

  • setting and getting parameters used by GridSearchQ and friends;

  • saving/loading quantifier instances;

  • parameter validation.

Read more in User Guide.

Notes

All quantifiers should specify all the parameters that can be set at the class level in their __init__ as explicit keyword arguments. (No *args or **kwargs allowed.)

Examples

>>> from mlquantify.base import BaseQuantifier
>>> import numpy as np
>>> class MyQuantifier(BaseQuantifier):
...     def __init__(self, param1=42, param2='default'):
...         self.param1 = param1
...         self.param2 = param2
...     def fit(self, X, y):
...         self.classes_ = np.unique(y)
...         return self
...     def predict(self, X):
...         _, counts = np.unique(self.classes_, return_counts=True)
...         prevalence = counts / counts.sum()
...         return prevalence
>>> quantifier = MyQuantifier(param1=10, param2='custom')
>>> quantifier.get_params()
{'param1': 10, 'param2': 'custom'}
>>> X = np.random.rand(100, 10)
>>> y = np.random.randint(0, 2, size=100)
>>> quantifier.fit(X, y).predict(X)
[0.5 0.5]
get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

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

Returns:
paramsdict

Parameter names mapped to their values.

save_quantifier(path: str | None = None) None[source]#

Save the quantifier instance to a file.

set_params(**params)[source]#

Set the parameters of this estimator.

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

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.