-
Notifications
You must be signed in to change notification settings - Fork 239
/
mdm.py
123 lines (101 loc) · 3.79 KB
/
mdm.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""The MDM classifier on manifolds.
Lead authors: Daniel Brooks and Quentin Barthelemy.
"""
from scipy.special import softmax
from sklearn.base import BaseEstimator, ClassifierMixin
import geomstats.backend as gs
from geomstats.learning.frechet_mean import FrechetMean
class RiemannianMinimumDistanceToMean(ClassifierMixin, BaseEstimator):
"""Minimum Distance to Mean (MDM) classifier on manifolds.
Classification by nearest centroid. For each of the given classes, a
centroid is estimated according to the chosen metric. Then, for each new
point, the class is affected according to the nearest centroid [BBCJ2012]_.
Parameters
----------
riemannian_metric : RiemannianMetric
Riemannian metric to be used.
Attributes
----------
classes_ : array-like, shape=[n_classes,]
If fit, labels of training set.
mean_estimates_ : array-like, shape=[n_classes, *metric.shape]
If fit, centroids computed on training set.
References
----------
.. [BBCJ2012] A. Barachant, S. Bonnet, M. Congedo and C. Jutten, Multiclass
Brain-Computer Interface Classification by Riemannian Geometry. IEEE
Trans. Biomed. Eng., vol. 59, pp. 920-928, 2012.
"""
def __init__(self, riemannian_metric):
self.riemannian_metric = riemannian_metric
self.classes_ = None
self.mean_estimates_ = None
def fit(self, X, y, weights=None):
"""Compute Frechet mean of each class.
Parameters
----------
X : array-like, shape=[n_samples, *metric.shape]
Training input samples.
y : array-like, shape=[n_samples,]
Training labels.
weights : array-like, shape=[n_samples,]
Weights associated to the samples.
Optional, default: None, in which case it is equally weighted.
Returns
-------
self : object
Returns self.
"""
self.classes_ = gs.unique(y)
self.n_classes_ = len(self.classes_)
if weights is None:
weights = gs.ones(X.shape[0])
weights /= gs.sum(weights)
mean_estimator = FrechetMean(metric=self.riemannian_metric)
frechet_means = []
for c in self.classes_:
X_c = X[gs.where(y == c, True, False)]
weights_c = weights[gs.where(y == c, True, False)]
mean_c = mean_estimator.fit(X_c, None, weights_c).estimate_
frechet_means.append(mean_c)
self.mean_estimates_ = gs.array(frechet_means)
def predict(self, X):
"""Compute closest neighbor according to riemannian_metric.
Parameters
----------
X : array-like, shape=[n_samples, *metric.shape]
Test samples.
Returns
-------
y : array-like, shape=[n_samples,]
Predicted labels.
"""
indices = self.riemannian_metric.closest_neighbor_index(
X,
self.mean_estimates_,
)
if gs.ndim(indices) == 0:
indices = gs.expand_dims(indices, 0)
return gs.take(self.classes_, indices)
def predict_proba(self, X):
"""Compute probabilities.
Compute probabilities to belong to classes according to
riemannian_metric.
Parameters
----------
X : array-like, shape=[n_samples, *metric.shape]
Test samples.
Returns
-------
probas : array-like, shape=[n_samples, n_classes]
Probability of the sample for each class in the model.
"""
n_samples = X.shape[0]
probas = []
for i in range(n_samples):
dist2 = self.riemannian_metric.squared_dist(
X[i],
self.mean_estimates_,
)
probas.append(softmax(-dist2))
return gs.array(probas)