KernelMeanRepresentation#

class mlquantify.representations.KernelMeanRepresentation(kernel='rbf', gamma=None, degree=3, coef0=0.0)[source]#

Kernel mean embedding representation.

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)
pairwise(X, Y)[source]#

Compute a pairwise kernel matrix between two arrays.

Dispatches to sklearn.metrics.pairwise.pairwise_kernels using the kernel and hyperparameters configured at construction.

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

First set of samples.

Yarray-like of shape (n_y, n_features)

Second set of samples.

Returns:
Kndarray of shape (n_x, n_y)

Kernel matrix where K[i, j] = k(X[i], Y[j]).

Examples

>>> from mlquantify.representations._kernel import KernelMeanRepresentation
>>> import numpy as np
>>> rep = KernelMeanRepresentation(kernel="rbf", gamma=1.0)
>>> X = np.array([[0.0], [1.0]])
>>> Y = np.array([[0.5]])
>>> rep.pairwise(X, Y).shape
(2, 1)
transform(X)[source]#

Compute the empirical mean embedding of a set of instances.

Returns the column-wise mean of the feature matrix, which is the kernel mean embedding under a linear kernel and an approximation under non-linear kernels.

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

Feature matrix.

Returns:
embeddingndarray of shape (n_features,)

Mean feature vector.

Examples

>>> from mlquantify.representations._kernel import KernelMeanRepresentation
>>> import numpy as np
>>> rep = KernelMeanRepresentation()
>>> X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
>>> rep.transform(X)
array([3., 4.])