-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmm_classifier.py
72 lines (57 loc) · 1.71 KB
/
hmm_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
import operator
from copy import copy
from scipy.special import softmax
class HMM_classifier():
def __init__(self, base_hmm_model):
self.models = {}
self.hmm_model = base_hmm_model
def fit(self, X, Y):
"""
X: input sequence [[[x1,x2,.., xn]...]]
Y: output classes [1, 2, 1, ...]
"""
print("Detect classes:", set(Y))
print("Prepare datasets...")
X_Y = {}
X_lens = {}
for c in set(Y):
X_Y[c] = []
X_lens[c] = []
for x, y in zip(X, Y):
X_Y[y].extend(x)
X_lens[y].append(len(x))
for c in set(Y):
print("Fit HMM for", c, " class")
hmm_model = copy(self.hmm_model)
hmm_model.fit(X_Y[c], X_lens[c])
self.models[c] = hmm_model
def _predict_scores(self, X):
"""
X: input sample [[x1,x2,.., xn]]
Y: dict with log likehood per class
"""
X_seq = []
X_lens = []
for x in X:
X_seq.extend(x)
X_lens.append(len(x))
scores = {}
for k, v in self.models.items():
scores[k] = v.score(X)
return scores
def predict_proba(self, X):
"""
X: input sample [[x1,x2,.., xn]]
Y: dict with probabilities per class
"""
pred = self._predict_scores(X)
keys = list(pred.keys())
scores = softmax(list(pred.values()))
return dict(zip(keys, scores))
def predict(self, X):
"""
X: input sample [[x1,x2,.., xn]]
Y: predicted class label
"""
pred = self.predict_proba(X)
return max(pred.items(), key=operator.itemgetter(1))[0]