HardPredictionRepresentation#

class mlquantify.representations.HardPredictionRepresentation(average=True)[source]#

Hard-prediction representation convenience class.

Shortcut for PredictionRepresentation(method='hard'). Converts posterior arrays or label vectors to one-hot encodings and returns the mean, which is equivalent to the empirical class frequency.

Parameters:
averagebool, default=True

If True, return the mean one-hot vector (class frequencies).

Examples

>>> from mlquantify.representations._prediction import HardPredictionRepresentation
>>> import numpy as np
>>> posteriors = np.array([[0.7, 0.3], [0.2, 0.8], [0.6, 0.4]])
>>> y = np.array([0, 1, 0])
>>> rep = HardPredictionRepresentation().fit(posteriors, y)
>>> rep.transform(posteriors).shape
(2,)
class_likelihoods(X)[source]#

Retrieve per-class likelihoods from the nested representation.

Delegates to the class_likelihoods method of the configured nested representation. Only available when a nested representation that exposes class_likelihoods (e.g. KDERepresentation) was supplied at construction.

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

Test instances (after the primary transformation).

Returns:
likelihoodsndarray of shape (n_classes, n_samples)

Per-class likelihoods for every test instance.

Raises:
AttributeError

If no nested representation is configured, or if the nested representation does not implement class_likelihoods.

Examples

>>> from mlquantify.representations._prediction import SoftPredictionRepresentation
>>> from mlquantify.representations._density import KDERepresentation
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> posteriors = rng.dirichlet([2, 2], size=100)
>>> y = posteriors.argmax(axis=1)
>>> kde = KDERepresentation(bandwidth=0.1)
>>> rep = SoftPredictionRepresentation(representation=kde).fit(posteriors, y)
>>> rep.class_likelihoods(posteriors[:5]).shape
(2, 5)
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)
transform(X)[source]#

Transform instances into the prediction representation.

Applies the configured transformation (soft/hard/custom) to X and optionally passes the result through a nested representation. When average=True the output is a single mean vector.

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

Posterior probabilities, raw scores, or hard labels.

Returns:
representationndarray of shape (n_classes,) or (n_samples, n_classes)

Mean representation vector (when average=True) or per-instance representation matrix.

Examples

>>> from mlquantify.representations._prediction import SoftPredictionRepresentation
>>> import numpy as np
>>> rng = np.random.default_rng(1)
>>> posteriors = rng.dirichlet([1, 3], size=50)
>>> y = posteriors.argmax(axis=1)
>>> rep = SoftPredictionRepresentation().fit(posteriors, y)
>>> rep.transform(posteriors[:5]).shape
(2,)