Classifier#

class mvpy.estimators.Classifier(estimator: BaseEstimator, method: str = 'OvR', arguments: List[Any] = [], kwarguments: Dict[Any, Any] = {})[source]#

Implements a wrapper for classifiers that handle one-versus-one (OvO) and one-versus-rest (OvR) classification schemes.

While this class is exposed publically, there are few (if any) direct use cases for this class. In principle, it exists for other classifiers that want to handle multi-class cases as OvO or OvR as a wrapper function that can either be inherited or created as a super class, specifying the desired estimator (recommended option).

One-versus-rest (OvR) classification computes the decision functions over inputs \(X\) and then takes the maximum value across decision values to predict the most likely classes \(\hat{y}\).

One-versus-one (OvO) classification computes all decision functions from binary classifiers (e.g., \(c_0\) vs \(c_1\), \(c_0\) vs \(c_2\), \(c_1\) vs \(c_2\), …). For each individual classification problem, the maximum value is recorded as one vote for the winning class. Votes are then aggregated across all classifiers and the maximum number of votes decides the most likely classes \(\hat{y}\).

Warning

When calling predict_proba(), probabilities are computed from expit() over outputs of decision_function(). While this outputs valid probabilities, they are consequently based on decision values that are on arbitrary scales. This may lead to ill-callibrated probability estimates. If accurate probability estimates are desired, please consider using CalibratedClassifier (to be implemented).

Parameters:
estimatorsklearn.base.BaseEstimator

The estimator type wrapped by this class.

method{‘OvR’, ‘OvO’}, default=’OvR’

For multiclass problems, which method should we use? One-versus-one (OvO) or one-versus-rest (OvR)?

argumentsList[Any], default=[]

Arguments to pass to the estimator at initialisation.

kwargumentsDict[str, Any], default=dict()

Keyword arguments to pass to the estimator at initialisation.

Attributes:
estimatorsklearn.base.BaseEstimator

The estimator type wrapped by this class.

method{‘OvR’, ‘OvO’}, default=’OvR’

For multiclass problems, which method should we use? One-versus-one (OvO) or one-versus-rest (OvR)?

argumentsList[Any], default=[]

Arguments to pass to the estimator at initialisation.

kwargumentsDict[str, Any], default=dict()

Keyword arguments to pass to the estimator at initialisation.

estimators_sklearn.base.BaseEstimator | List[sklearn.base.BaseEstimator]

All instances of the estimator class (only of type list if OvO).

binariser_mvpy.estimators.LabelBinariser

Label binariser used internally.

coef_np.ndarray | torch.Tensor

If available, coefficients from all classifiers ([n_classifiers,] n_channels, n_classes).

intercept_np.ndarray | torch.Tensor

If available, intercepts from all classifiers ([n_classifiers,] n_classes).

pattern_np.ndarray | torch.Tensor

If available, patterns from all classifiers ([n_classifiers,] n_channels, n_classes).

offsets_np.ndarray | torch.Tensor

Numerical offsets for each feature in outputs, used internally.

metric_mvpy.metrics.Accuracy

The default metric to use.

See also

mvpy.estimators.RidgeClassifier, mvpy.estimators.SVC

Classifiers that use this class as a wrapper.

mvpy.preprocessing.LabelBinariser

Label binariser used internally to generated one-hot encodings.

clone() Classifier[source]#

Clone this class.

Returns:
clfClassifier

The cloned object.

copy() Classifier[source]#

Clone this class.

Returns:
clfClassifier

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:
clfmvpy.estimators.Classifier

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]#

Compute probabilities assigned to each class.

Parameters:
Xnp.ndarray | torch.Tensor

The features of shape (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 of decision_function() where, for method OvR, we use Wu-Lin coupling. Consequently, probability estimates returned by this class are not calibrated.

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 to metric_.

Returns:
scorenp.ndarray | torch.Tensor | Dict[str, np.ndarray] | Dict[str, torch.Tensor]

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.

to_numpy() BaseEstimator[source]#

Obtain the estimator with numpy as backend.

Returns:
clfmvpy.estimators.classifier._Classifier_numpy

The estimator.

to_torch() BaseEstimator[source]#

Obtain the estimator with torch as backend.

Returns:
clfmvpy.estimators.classifier._Classifier_torch

The estimator.