Decoders#

Some decoders following the scikit-learn API.

class cebra.integrations.sklearn.decoder.Decoder(*args, **kwargs)#

Bases: ABC, BaseEstimator

Abstract base class for implementing a decoder.

abstract fit(X, y)#

Fit the decoder.

Parameters:
Return type:

Decoder

abstract predict(X)#

Predict the data X.

Parameters:

X (Union[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Tensor]) – The data matrix to predict.

Return type:

ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]]

score(X, y)#

Returns performances of the decoder instance on the provided data X.

Parameters:
Return type:

Tuple[float, float, float]

Returns:

The R2 score on X.

class cebra.integrations.sklearn.decoder.KNNDecoder(n_neighbors=3, metric='cosine')#

Bases: Decoder

Decoder implementing the k-nearest neighbors vote.

n_neighbors#

An integer indicating the K number of neighbors to consider.

Type:

int

metric#

The metric to evaluate the KNN decoder’s performances.

Type:

str

Examples

>>> from cebra import KNNDecoder
>>> import numpy as np
>>> X = np.random.uniform(0, 1, (100, 50))
>>> y = np.random.uniform(0, 10, (100, 2))
>>> decoder = KNNDecoder()
>>> decoder.fit(X, y)
KNNDecoder()
>>> score = decoder.score(X, y)
fit(X, y)#

Fit the KNN decoder.

Parameters:
Return type:

KNNDecoder

Returns:

self, to allow chaining of operations.

predict(X)#

Predict the targets for data X.

Parameters:

X (Union[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Tensor]) – The data matrix.

Return type:

Union[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Tensor]

Returns:

A matrix with the prediction for each data sample.

iter_hyperparams()#

Create sets of parameters.

Note

It can be used for parametrized testing.

Yields:

A dictionary containing sets of parameters to be used for testing.

Return type:

Generator[dict, None, None]

class cebra.integrations.sklearn.decoder.L1LinearRegressor(alpha=1.0)#

Bases: Decoder

A linear model trained with L1 prior as regularizer (aka the Lasso).

alpha#

regularization strength coefficient.

Type:

float

Examples

>>> from cebra import L1LinearRegressor
>>> import numpy as np
>>> X = np.random.uniform(0, 1, (100, 50))
>>> y = np.random.uniform(0, 10, (100, 2))
>>> decoder = L1LinearRegressor()
>>> decoder.fit(X, y)
L1LinearRegressor()
>>> score = decoder.score(X, y)
fit(X, y)#

Fit the Lasso regressor.

Parameters:
Return type:

L1LinearRegressor

Returns:

self, to allow chaining of operations.

predict(X)#

Predict the targets for data X.

Parameters:

X (Union[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Tensor]) – The data matrix.

Return type:

Union[ndarray[tuple[int, ...], dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Tensor]

Returns:

A matrix with the prediction for each data sample.

iter_hyperparams()#

Create sets of parameters.

Note

It can be used for testing.

Yields:

A dictionary containing sets of parameters to be used for testing.

Return type:

Generator[dict, None, None]