-
Notifications
You must be signed in to change notification settings - Fork 232
/
covariance.py
60 lines (46 loc) · 1.51 KB
/
covariance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Covariance metric (baseline method)
"""
import numpy as np
import scipy
from sklearn.base import TransformerMixin
from .base_metric import MahalanobisMixin
from ._util import components_from_metric
class Covariance(MahalanobisMixin, TransformerMixin):
"""Covariance metric (baseline method)
This method does not "learn" anything, rather it calculates
the covariance matrix of the input data.
This is a simple baseline method first introduced in
On the Generalized Distance in Statistics, P.C.Mahalanobis, 1936
Read more in the :ref:`User Guide <covariance>`.
Attributes
----------
components_ : `numpy.ndarray`, shape=(n_features, n_features)
The linear transformation ``L`` deduced from the learned Mahalanobis
metric (See function `components_from_metric`.)
Examples
--------
>>> from metric_learn import Covariance
>>> from sklearn.datasets import load_iris
>>> iris = load_iris()['data']
>>> cov = Covariance().fit(iris)
>>> x = cov.transform(iris)
"""
def __init__(self, preprocessor=None):
super(Covariance, self).__init__(preprocessor)
def fit(self, X, y=None):
"""
Calculates the covariance matrix of the input data.
Parameters
----------
X : data matrix, (n x d)
y : unused
"""
X = self._prepare_inputs(X, ensure_min_samples=2)
M = np.atleast_2d(np.cov(X, rowvar=False))
if M.size == 1:
M = 1. / M
else:
M = scipy.linalg.pinvh(M)
self.components_ = components_from_metric(np.atleast_2d(M))
return self