BaseRepresentation#

class mlquantify.representations.BaseRepresentation[source]#

Base class for quantification representations.

Subclasses must implement transform and _fit. The latter must set the class_representations_ attribute to an array of shape (n_classes, n_representation_features) containing the representations for each class.

Examples

>>> from mlquantify.representations import BaseRepresentation
>>> class DummyRepresentation(BaseRepresentation):
...     def _fit(self, X, y, sample_weight=None):
...         self.class_representations_ = np.array([[0], [1]])
...     def transform(self, X):
...         return X
>>> X = [[0], [1], [0], [1]]
>>> y = [0, 1, 0, 1]
>>> rep = DummyRepresentation().fit(X, y)
>>> rep.class_representations_
array([[0],
       [1]])
fit(X, y, classes=None, sample_weight=None)[source]#

Fit the representation to labelled training data.

Validates shapes, stores the class labels, delegates internal fitting to _fit, and verifies that the subclass set class_representations_ during that call.

Parameters:
Xarray-like of shape (n_samples, n_features) or (n_samples,)

Feature matrix or pre-computed score array for the training instances.

yarray-like of shape (n_samples,)

Class labels for each training instance.

classesarray-like of shape (n_classes,) or None, default=None

Explicit list of class labels. If None, the unique values in y are used.

sample_weightarray-like of shape (n_samples,) or None, default=None

Per-sample weights forwarded to _fit.

Returns:
selfBaseRepresentation

The fitted representation object.

Raises:
ValueError

If X and y have inconsistent lengths or X is zero-dimensional.

AttributeError

If the subclass did not define class_representations_ inside _fit.

Examples

>>> from mlquantify.representations import HistogramRepresentation
>>> import numpy as np
>>> X = np.random.default_rng(0).uniform(0, 1, (100, 1))
>>> y = (X[:, 0] > 0.5).astype(int)
>>> rep = HistogramRepresentation(bins=(5,)).fit(X, y)
>>> rep.class_representations_.shape
(2, 5)
abstract transform(X)[source]#

Transform data into the representation space.