PredictionRepresentation#

class mlquantify.representations.PredictionRepresentation(method='soft', average=True, func=None, representation=None)[source]#

Representation based on classifier predictions.

Converts raw feature vectors (or pre-computed posterior probability arrays) into a fixed-length representation suitable for distribution matching. Two built-in modes are provided:

  • 'soft': treats each row of X as a soft posterior vector and returns the class-mean posterior (average soft predictions).

  • 'hard': converts posterior or label arrays to a one-hot encoding and returns the empirical class-frequency vector (equivalent to Classify-and-Count applied to the representation).

A custom transformation function can also be supplied via func, and an optional downstream representation can further map the transformed outputs.

Parameters:
method{‘soft’, ‘hard’}, default=’soft’

Built-in transformation mode. Ignored when func is provided.

averagebool, default=True

If True, the representation is averaged over instances. If False, the per-instance matrix is returned.

funccallable or None, default=None

Custom transformation func(X, representation) -> Z. When provided, method is ignored.

representationBaseRepresentation or None, default=None

Optional nested representation applied to the transformed output Z before returning.

Attributes:
priors_ndarray of shape (n_classes,)

Empirical class proportions observed during fit.

class_representations_ndarray

Per-class mean representation vectors (or nested object array).

classes_ndarray of shape (n_classes,)

Unique class labels seen during fit.

Examples

>>> from mlquantify.representations._prediction import SoftPredictionRepresentation
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> posteriors = rng.dirichlet([2, 2], size=100)
>>> y = posteriors.argmax(axis=1)
>>> rep = SoftPredictionRepresentation().fit(posteriors, y)
>>> rep.transform(posteriors[:10]).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,)