class TestPrototypeDTW(_BaseTest):
    def setUp(self):
        super(TestPrototypeDTW,self).setUp()
        self.p = PrototypeDTW(self.label, alpha=0.5)
        self.p.train(self.ink_data)
        
    def test_serialization(self):
        p_data = self.p.toJSON()
        q = PrototypeDTW(None)
        q.fromJSON(p_data)
        self.assertEqual(p_data, q.toJSON())

    def test_score(self):
        score0 = self.p.score(self.ink_data[0])
        if VERBOSE: print score0
        self.assertAlmostEqual(score0, -0.088, delta=1e-3)
        score1 = self.p.score(self.ink_data[1])
        if VERBOSE: print score1
        self.assertAlmostEqual(score1, -0.049, delta=1e-3)
        
    def test_centroid(self):
        q = PrototypeDTW('u', alpha=0.5)
        q.train(self.ink_data, center_type="centroid")
        score0 = q.score(self.ink_data[0])
        if VERBOSE: print score0
        self.assertAlmostEqual(score0, -0.088, delta=1e-3)

    def test_state_reduction(self):
        q = PrototypeDTW('u', alpha=0.5)
        q.train(self.ink_data, center_type="centroid", state_reduction=True)
        score0 = q.score(self.ink_data[0])
        if VERBOSE: print score0
        self.assertAlmostEqual(score0, -0.089, delta=1e-3)
    def train(self, clustered_ink_data, center_type="centroid", verbose=False, state_reduction=False):
        """Trains the classifier.

        Parameters
        ----------
        clustered_ink_data : dictionary
           Training data with the following structure.
           clustered_ink_data[label][cluster_id] = [ink1, ink2, ... inkN]

        center_type : {'medoid', 'centroid'}
           Type of the DTW center.
           
        state_reduction : bool
           If state_reduction=True, state-reduction procedure is performed
           after each prototype is trained.
       
        """
        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 = PrototypeDTW(label, alpha=self.alpha)
                    avg_score = proto.train(
                        ink_data, obs_weights=weights, center_type=center_type, state_reduction=state_reduction
                    )
                    self._trained_prototypes.append(proto)
                    if verbose:
                        print(
                            "Prototype for " "%s (%d instances, avg.score = %0.2f)" % (label, len(ink_list), avg_score)
                        )

        self._compute_log_priors()
 def fromJSON(self, json_obj):
     prototypes = json_obj["prototypes"]
     self._trained_prototypes = []
     self.log_priors = np.zeros(len(prototypes))
     for i, prototype in enumerate(prototypes):
         p = PrototypeDTW(prototype["label"], prototype["alpha"])
         p.fromJSON(prototype)
         self._trained_prototypes.append(p)
         self.log_priors[i] = np.log(prototype["prior"])
Beispiel #4
0
def _train_prototypes(weighted_ink, partition, label, 
                      min_cluster_size=5, center_type='centroid'):
    obs, obs_weights = zip(*weighted_ink)
    obs_weights = np.asarray(obs_weights)
    trained_prototypes = []
    for k in range(np.max(partition)):
        clustered_obs = [obs[j] 
                         for j in range(len(obs))
                         if partition[j] == (k+1)]
        clustered_weights = obs_weights[partition == (k+1)]
        if len(clustered_obs) > min_cluster_size:
            p_dtw = PrototypeDTW(label)
            p_dtw.train(clustered_obs, 
                        obs_weights=clustered_weights, 
                        center_type=center_type)
            trained_prototypes.append(p_dtw)
    return trained_prototypes
 def test_state_reduction(self):
     q = PrototypeDTW('u', alpha=0.5)
     q.train(self.ink_data, center_type="centroid", state_reduction=True)
     score0 = q.score(self.ink_data[0])
     if VERBOSE: print score0
     self.assertAlmostEqual(score0, -0.089, delta=1e-3)
 def test_centroid(self):
     q = PrototypeDTW('u', alpha=0.5)
     q.train(self.ink_data, center_type="centroid")
     score0 = q.score(self.ink_data[0])
     if VERBOSE: print score0
     self.assertAlmostEqual(score0, -0.088, delta=1e-3)
 def test_serialization(self):
     p_data = self.p.toJSON()
     q = PrototypeDTW(None)
     q.fromJSON(p_data)
     self.assertEqual(p_data, q.toJSON())
 def setUp(self):
     super(TestPrototypeDTW,self).setUp()
     self.p = PrototypeDTW(self.label, alpha=0.5)
     self.p.train(self.ink_data)