RidgeClassifier#
- class mvpy.estimators.RidgeClassifier(alphas: Tensor | ndarray | float | int = 1, method: str = 'OvR', fit_intercept: bool = True, normalise: bool = True, alpha_per_target: bool = False)[source]#
Implements a linear ridge classifier.
Ridge classifiers effectively frame a classification problem as a simple linear ridge regression, mapping from neural data \(X\) to labels \(y\) through spatial filters \(\beta\):
\[y = \beta X + \varepsilon\quad\textrm{where}\quad y\in\{-1, 1\}\]Consequently, we solve for spatial filters through:
\[\arg\min_{\beta} \sum_{i}(y_i - \beta^TX_i)^2 + \alpha_\beta\lvert\lvert\beta\rvert\rvert^2\]where \(\alpha_\beta\) are the penalties to test in LOO-CV.
This linear filter estimation is extremely convenient for neural decoding because, unlike other decoding approaches such as
SVC, this can be solved extremely efficiently and, for many decoding tasks, will perform well.- Parameters:
- alphanp.ndarray | torch.Tensor
The penalties to use for estimation.
- fit_interceptbool, default=True
Whether to fit an intercept.
- normalisebool, default=True
Whether to normalise the data.
- alpha_per_targetbool, default=False
Whether to fit individual alphas per target.
- Attributes:
- alphanp.ndarray | torch.Tensor
The penalties to use for estimation.
- fit_interceptbool, default=True
Whether to fit an intercept.
- normalisebool, default=True
Whether to normalise the data.
- alpha_per_targetbool, default=False
Whether to fit individual alphas per target.
- estimatormvpy.estimators.RidgeDecoder
The ridge estimator.
- binariser_mvpy.preprocessing.LabelBinariser
The label binariser used internally.
- intercept_np.ndarray | torch.Tensor
The intercepts of the classifier.
- coef_np.ndarray | torch.Tensor
The coefficients of the classifier.
- pattern_np.ndarray | torch.Tensor
The patterns of the classifier.
- metric_mvpy.metrics.Accuracy
The default metric to use.
Notes
By default, this will not allow alpha values to differ between targets. In certain situations, this may be desirable, however. In the multi-class case, it should be carefully evaluated whether or not
alpha_per_targetshould be enabled, as here it may also hurt decoding performance if penalties are on different scales andmethodisOvR.Coefficients are transformed to patterns to facilitate interpretation thereof. For more information, please see [1].
References
[1]Haufe, S., Meinecke, F., Görgen, K., Dähne, S., Haynes, J.D., Blankertz, B., & Bießmann, F. (2014). On the interpretation of weight vectors of linear models in multivariate neuroimaging. NeuroImage, 87, 96-110. 10.1016/j.neuroimage.2013.10.067
Examples
We can either do classification over a single feature, like so:
>>> import torch >>> from mvpy.estimators import RidgeClassifier >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y = True) >>> X, y = torch.from_numpy(X).float(), torch.from_numpy(y).float() >>> clf = RidgeClassifier(torch.logspace(-5, 10, 20)).fit(X, y) >>> y_h = clf.predict(X) >>> mv.math.accuracy(y_h.squeeze(), y) tensor(0.8533)
Or we can also do classification over multiple features, like so:
>>> import torch >>> from mvpy.estimators import RidgeClassifier >>> from sklearn.datasets import make_classification >>> X0, y0 = make_classification(n_classes = 3, n_informative = 6) >>> X1, y1 = make_classification(n_classes = 4, n_informative = 8) >>> X = torch.from_numpy(np.concatenate((X0, X1), axis = -1)).float() >>> y = torch.from_numpy(np.stack((y0, y1), axis = -1)).float() >>> clf = RidgeClassifier(torch.logspace(-5, 10, 20)).fit(X, y) >>> y_h = clf.predict(X) >>> mv.math.accuracy(y_h.T, y.T) torch.tensor([0.82, 0.75])
- clone() RidgeClassifier[source]#
Clone this class.
- Returns:
- clfRidgeClassifier
The cloned object.
- copy() RidgeClassifier[source]#
Clone this class.
- Returns:
- clfRidgeClassifier
The cloned object.
- decision_function(X: ndarray | Tensor) ndarray | Tensor[source]#
Predict from the estimator.
- Parameters:
- Xnp.ndarray | torch.Tensor
The features
(n_samples, n_channels).
- Returns:
- dfnp.ndarray | torch.Tensor
The predictions of shape
(n_samples, n_classes).
- fit(X: ndarray | Tensor, y: ndarray | Tensor) BaseEstimator[source]#
Fit the estimator.
- Parameters:
- Xnp.ndarray | torch.Tensor
The features of shape
(n_samples, n_channels).- ynp.ndarray | torch.Tensor
The targets of shape
(n_samples[, n_features]).
- Returns:
- clfClassifier
The classifier.
- predict(X: ndarray | Tensor) ndarray | Tensor[source]#
Predict from the estimator.
- Parameters:
- Xnp.ndarray | torch.Tensor
The features (n_samples, n_channels).
- Returns:
- y_hnp.ndarray | torch.Tensor
The predictions of shape
(n_samples, n_features).
- predict_proba(X: ndarray | Tensor) ndarray | Tensor[source]#
Predict from the estimator.
- Parameters:
- Xnp.ndarray | torch.Tensor
The features
(n_samples, n_channels).
- Returns:
- pnp.ndarray | torch.Tensor
The predictions of shape
(n_samples, n_classes).
Warning
Probabilities are computed from
expit()over outputs ofdecision_function(). Consequently, probability estimates returned by this class are not calibrated. SeeClassifierfor more information.
- score(X: ndarray | Tensor, y: ndarray | Tensor, metric: Metric | Tuple[Metric] | None = None) ndarray | Tensor | Dict[str, ndarray] | Dict[str, Tensor][source]#
Make predictions from \(X\) and score against \(y\).
- Parameters:
- Xnp.ndarray | torch.Tensor
Input data of shape
(n_samples, n_channels).- ynp.ndarray | torch.Tensor
Output data of shape
(n_samples, n_features).- metricOptional[Metric | Tuple[Metric]], default=None
Metric or tuple of metrics to compute. If
None, defaults tometric_.
- Returns:
- scorenp.ndarray | torch.Tensor | Dict[str, np.ndarray], Dict[str, torch.Tensor]
Scores of shape
(n_features,)or, for multiple metrics, a dictionary of metric names and scores of shape(n_features,).
Warning
If multiple values are supplied for
metric, this function will output a dictionary of{Metric.name: score, ...}rather than a stacked array. This is to provide consistency across cases where metrics may or may not differ in their output shapes.