-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGMMcl.py
More file actions
65 lines (54 loc) · 2.04 KB
/
GMMcl.py
File metadata and controls
65 lines (54 loc) · 2.04 KB
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
# -*- coding: utf-8 -*-
"""GMM based classifier
(c) 2019 Joachim Thiemann
This work is licensed under the Creative Commons Attribution-ShareAlike
4.0 International License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to
Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
"""
import numpy as np
import sklearn.base
from sklearn.mixture import GMM
# A classifier where each class is modeled by a GMM
class GMMcl(sklearn.base.ClassifierMixin):
'''The GMMcl is a sckits-learn style classifier where each class
is represented by a GMM. It is in effect an array of GMMs.
Parameters
----------
model : GMM-like class
Default is sklearn.mixture.GMM (default), but other possibilities
are sklearn.mixture.DPGMM or sklearn.mixture.VBGMM.
minll : float
Minimum value log-likelihood can be. Allows for labels that are
never seen in the training data.
kwargs : additional keyword arguments
Parameters to be passed through to `model`
'''
nClasses = None
mixtures = None
model = None
minll = None
def __init__(self, model=GMM, minll=-100, **kwargs):
self.model = model
self.minll = minll
self.kwargs = kwargs
def fit(self, X, y):
self.nClasses = int(np.max(y)+1)
self.mixtures = [self.model(**self.kwargs) for k in range(self.nClasses)]
for k, mix in enumerate(self.mixtures):
if any(y==k):
mix.fit(X[y==k])
return self
def _scoremix(self, m, X):
minvals = self.minll*np.ones(X.shape[0])
# check the mixture was trained
if 'means_' in m.__dict__:
return np.fmax(m.score(X), minvals)
else:
return minvals[:]
def predict(self, X):
ll = [ self._scoremix(mix, X) for mix in self.mixtures ]
return np.vstack(ll).argmax(axis=0)
def loglike_per_class(self, X, y=None):
ll = [ self._scoremix(mix, X) for mix in self.mixtures ]
return np.vstack(ll)