mojolearn.MinCovDet#

class mojolearn.MinCovDet(*, store_precision=True, assume_centered=False, support_fraction=None, random_state=None, numeric_mode=None)#

Minimum covariance determinant: a robust covariance estimator.

Follows scikit-learn’s MinCovDet (FastMCD), including the consistency correction and the reweighting step. Random subsets come from mojolearn’s own generator and ties are broken by row index, so results can differ from scikit-learn’s for the same seed.

Parameters:
  • store_precision (bool, default True) – Store precision_; when False it is None.

  • assume_centered (bool, default False) – Treat the data as centered (location fixed at zero).

  • support_fraction (float or None, default None) – Fraction of points in the raw support. None uses ceil((n_samples + n_features + 1) / 2) points.

  • random_state (int or None, default None) – Seed for the random subsets; None means 0.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

Variables:
  • location_ (ndarray of shape (n_features,)) – Robust location (reweighted).

  • covariance_ (ndarray of shape (n_features, n_features)) – Robust covariance (reweighted).

  • precision_ (ndarray of shape (n_features, n_features) or None)

  • support_ (ndarray of shape (n_samples,), uint8) – 1 for points used in the reweighted estimate.

  • raw_location_ (ndarray of shape (n_features,))

  • raw_covariance_ (ndarray of shape (n_features, n_features))

  • raw_support_ (ndarray of shape (n_samples,), uint8)

  • dist_ (ndarray of shape (n_samples,)) – Squared Mahalanobis distances of the training points.

  • n_features_in_ (int)

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).normal(size=(500, 3)).astype(np.float32)
>>> mcd = mojolearn.MinCovDet(random_state=0).fit(X)
>>> d = mcd.mahalanobis(X)

Methods

fit(X[, y])

Fit the robust location and covariance.

fit_transform(X[, y])

Fit to X, then return transform(X).

get_params([deep])

Return this estimator's constructor parameters.

get_precision()

Return the precision matrix (pseudo-inverse of covariance_).

mahalanobis(X)

Return squared Mahalanobis distances under the fitted model.

score(X[, y])

Return the Gaussian log-likelihood of X under the fitted model.

set_params(**params)

Set constructor parameters by keyword.

fit(X, y=None)#

Fit the robust location and covariance.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y (ignored)

Returns:

self

mahalanobis(X)#

Return squared Mahalanobis distances under the fitted model.

Parameters:

X (array-like of shape (n_samples, n_features))

Returns:

ndarray of shape (n_samples,), float32

Raises:

RuntimeError – If the model is not fitted.

get_precision()#

Return the precision matrix (pseudo-inverse of covariance_).

Available even when store_precision=False.

Returns:

ndarray of shape (n_features, n_features), float32

score(X, y=None)#

Return the Gaussian log-likelihood of X under the fitted model.

As scikit-learn’s EmpiricalCovariance.score: uses X’s empirical covariance around location_ and the fitted precision.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y (ignored)

Returns:

float

fit_transform(X, y=None)#

Fit to X, then return transform(X).

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (ignored)

Returns:

ndarray of shape (n_samples, n_components), float32

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self