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

Function fit_coin

hmm_class/hmmd.py:181–202  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

179 return states
180
181def fit_coin():
182 X = []
183 for line in open('coin_data.txt'):
184 # 1 for H, 0 for T
185 x = [1 if e == 'H' else 0 for e in line.rstrip()]
186 X.append(x)
187
188 hmm = HMM(2)
189 hmm.fit(X)
190 L = hmm.log_likelihood_multi(X).sum()
191 print("LL with fitted params:", L)
192
193 # try true values
194 hmm.pi = np.array([0.5, 0.5])
195 hmm.A = np.array([[0.1, 0.9], [0.8, 0.2]])
196 hmm.B = np.array([[0.6, 0.4], [0.3, 0.7]])
197 L = hmm.log_likelihood_multi(X).sum()
198 print("LL with true params:", L)
199
200 # try viterbi
201 print("Best state sequence for:", X[0])
202 print(hmm.get_state_sequence(X[0]))
203
204
205if __name__ == '__main__':

Callers 1

hmmd.pyFile · 0.70

Calls 4

fitMethod · 0.95
log_likelihood_multiMethod · 0.95
get_state_sequenceMethod · 0.95
HMMClass · 0.70

Tested by

no test coverage detected