-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgmm_classifier.py
201 lines (182 loc) · 7.23 KB
/
gmm_classifier.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from numbers import Number
import numpy as np
from sklearn.mixture import GaussianMixture
from sklearn.utils.extmath import softmax
from odin.utils import as_tuple
from odin.ml.base import BaseEstimator, ClassifierMixin, Evaluable
from odin.ml.scoring import VectorNormalizer
class GMMclassifier(BaseEstimator, ClassifierMixin, Evaluable):
""" GMMclassifier
Parameters
----------
strategy : str
'ova' - one-vs-all, for each class represented as a GMM
'all' - use a single GMM for all classes
covariance_type : {'full', 'tied', 'diag', 'spherical'},
defaults to 'full'.
String describing the type of covariance parameters to use.
Must be one of::
'full' (each component has its own general covariance matrix),
'tied' (all components share the same general covariance matrix),
'diag' (each component has its own diagonal covariance matrix),
'spherical' (each component has its own single variance).
max_iter : int, defaults to 100.
The number of EM iterations to perform.
n_init : int, defaults to 1.
The number of initializations to perform. The best results are kept.
init_params : {'kmeans', 'random'}, defaults to 'kmeans'.
The method used to initialize the weights, the means and the
precisions.
Must be one of::
'kmeans' : responsibilities are initialized using kmeans.
'random' : responsibilities are initialized randomly.
n_components : {int, list of int}
only used in case `strategy='ova'`, number of Gaussian components
for each class
"""
def __init__(self, strategy="ova", covariance_type='full',
max_iter=100, n_init=1,
init_params='kmeans', n_components=1,
centering=False, wccn=False, unit_length=False,
lda=False, concat=False, labels=None, random_state=1):
super(GMMclassifier, self).__init__()
self._strategy = str(strategy)
self._n_components = int(n_components)
self._covariance_type = str(covariance_type)
self._max_iter = int(max_iter)
self._n_init = int(n_init)
self._init_params = str(init_params)
self._random_state = (
1 if not isinstance(random_state, Number) else int(random_state))
# ====== default attribute ====== #
self._labels = labels
self._feat_dim = None
self._gmm = None
self._normalizer = VectorNormalizer(
centering=centering, wccn=wccn, unit_length=unit_length,
lda=lda, concat=concat)
# ==================== Pickling ==================== #
def __getstate__(self):
if not self.is_fitted:
raise RuntimeError("The GMMclassifier have not been fitted, "
"nothing to pickle!")
return (self._strategy, self._n_components, self._covariance_type,
self._max_iter, self._n_init, self._init_params,
self._labels, self._feat_dim, self._gmm,
self._normalizer)
def __setstate__(self, states):
(self._strategy, self._n_components, self._covariance_type,
self._max_iter, self._n_init, self._init_params,
self._labels, self._feat_dim, self._gmm,
self._normalizer) = states
# ==================== Properties ==================== #
@property
def is_fitted(self):
return self._gmm is not None
@property
def feat_dim(self):
return self._feat_dim
@property
def labels(self):
return self._labels
@property
def nb_classes(self):
return len(self.labels)
# ==================== Helpers ==================== #
def initialize(self, X, y=None):
if isinstance(X, (tuple, list)):
X = np.array(X)
elif not isinstance(X, np.ndarray):
X = X[:]
if isinstance(y, (tuple, list)):
y = np.array(y)
elif y is not None and not isinstance(y, np.ndarray):
y = y[:]
# ====== check dimensions ====== #
feat_dim = X.shape[1]
if self._feat_dim is None:
self._feat_dim = feat_dim
if y is not None:
classes = np.unique(y)
if self._labels is None:
self._labels = classes
else:
classes = self.labels
# ====== exception ====== #
if self.feat_dim != feat_dim:
raise ValueError("Initialized with `feat_dim`=%d, given data with %d "
"dimensions" % (self.feat_dim, feat_dim))
if self.nb_classes != len(classes):
raise ValueError("Initialized with `nb_classes`=%d, given data with %d "
"classes" % (self.nb_classes, len(classes)))
# ====== normalizing ====== #
if not self._normalizer.is_fitted:
self._normalizer.fit(X, y)
X = self._normalizer.transform(X)
# ====== initialize GMMs ====== #
if self._gmm is None:
if self._strategy == 'ova':
self._gmm = []
rand = np.random.RandomState(seed=self._random_state)
for n_components in as_tuple(self._n_components, t=int,
N=self.nb_classes):
gmm = GaussianMixture(n_components=n_components,
covariance_type=self._covariance_type,
max_iter=self._max_iter,
n_init=self._n_init,
init_params=self._init_params,
random_state=rand.randint(0, 10e8))
self._gmm.append(gmm)
elif self._strategy == 'all':
gmm = GaussianMixture(n_components=self.nb_classes,
covariance_type=self._covariance_type,
max_iter=self._max_iter,
n_init=self._n_init,
init_params=self._init_params,
means_init=np.array(
[X[y == clz].mean(axis=0) for clz in
np.unique(y)]),
random_state=self._random_state)
self._gmm = gmm
else:
raise ValueError("No support for `strategy`=%s" % self._strategy)
# ====== return ====== #
if y is None:
return X
return X, y
# ==================== Sklearn ==================== #
def fit(self, X, y):
X, y = self.initialize(X, y)
classes = np.unique(y)
if self._strategy == 'ova':
for i, (clz, gmm) in enumerate(zip(classes, self._gmm)):
X_cls = X[y == clz]
gmm.fit(X_cls)
elif self._strategy == 'all':
self._gmm.fit(X)
def score_samples(self, X):
X = self.initialize(X)
if self._strategy == 'ova':
scores = np.concatenate([gmm.score_samples(X)[:, None]
for k, gmm in enumerate(self._gmm)],
axis=-1)
elif self._strategy == 'all':
scores = self._gmm.predict_proba(X)
return scores
def predict(self, X):
return np.argmax(self.score_samples(X), axis=-1)
def predict_proba(self, X):
if self._strategy == 'ova':
scores = self.score_samples(X)
smin = np.min(scores, axis=-1, keepdims=True)
smax = np.max(scores, axis=-1, keepdims=True)
scores = (scores - smin) / (smax - smin)
proba = softmax(scores)
elif self._strategy == 'all':
proba = self.score_samples(X)
return proba
def predict_log_proba(self, X):
if self._strategy == 'ova':
return self.score_samples(X)
elif self._strategy == 'all':
return np.log(self.predict_proba(X))