def setUp(self): super(TestPrototypeHMM,self).setUp() self.p = PrototypeHMM(self.label, num_states=0.5, self_transprob=0.8, next_transprob=0.2, skip_transprob=1e-6) self.p.train(self.ink_data, max_N=15)
def train(self, clustered_ink_data, verbose=False): """Train the classifier from clustered_data Parameters ---------- clustered_ink_data : dictionary clustered_ink_data[label] = [ [(ink,weight) from each cluster] ] """ self._trained_prototypes = [] for label in clustered_ink_data: for ink_list in clustered_ink_data[label]: if len(ink_list) > self.min_cluster_size: ink_data, weights = zip(*ink_list) proto = PrototypeHMM(label) loglike = proto.train(ink_data, obs_weights=weights) self._trained_prototypes.append(proto) if verbose: print ("Prototype for " "%s (%d instances, avg_ll = %0.1f)"%( label, len(ink_list), loglike/len(ink_list))) self._compute_log_priors()
class TestPrototypeHMM(_BaseTest): def setUp(self): super(TestPrototypeHMM,self).setUp() self.p = PrototypeHMM(self.label, num_states=0.5, self_transprob=0.8, next_transprob=0.2, skip_transprob=1e-6) self.p.train(self.ink_data, max_N=15) def test_serialization(self): p_data = self.p.toJSON() q = PrototypeHMM(None) q.fromJSON(p_data) self.assertEqual(p_data, q.toJSON()) def test_score(self): score0 = self.p.score(self.ink_data[0]) self.assertAlmostEqual(score0, 47.772, delta=1e-3) score1 = self.p.score(self.ink_data[1]) self.assertAlmostEqual(score1, 53.725, delta=1e-3)
def _train(algorithm, label, obs, obs_weights): if algorithm == 'hmm': prot = PrototypeHMM(label) prot.train(obs, obs_weights=obs_weights) else: prot = PrototypeDTW(label) prot.train(obs, obs_weights=obs_weights, center_type='centroid') return prot
def test_serialization(self): p_data = self.p.toJSON() q = PrototypeHMM(None) q.fromJSON(p_data) self.assertEqual(p_data, q.toJSON())