MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / HMMClassifier

Class HMMClassifier

hmm_class/hmm_classifier.py:21–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19from nltk import pos_tag, word_tokenize
20
21class HMMClassifier:
22 def __init__(self):
23 pass
24
25 def fit(self, X, Y, V):
26 K = len(set(Y)) # number of classes - assume 0..K-1
27 N = len(Y)
28 self.models = []
29 self.priors = []
30 for k in range(K):
31 # gather all the training data for this class
32 thisX = [x for x, y in zip(X, Y) if y == k]
33 C = len(thisX)
34 self.priors.append(np.log(C) - np.log(N))
35
36 hmm = HMM(5)
37 hmm.fit(thisX, V=V, print_period=1, learning_rate=1e-2, max_iter=80)
38 self.models.append(hmm)
39
40 def score(self, X, Y):
41 N = len(Y)
42 correct = 0
43 for x, y in zip(X, Y):
44 lls = [hmm.log_likelihood(x) + prior for hmm, prior in zip(self.models, self.priors)]
45 p = np.argmax(lls)
46 if p == y:
47 correct += 1
48 return float(correct) / N
49
50
51# def remove_punctuation(s):

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected