def learn(self, evidence): """Quantize data into 'max_coincidence_count' clusters with maximal size of 'max_distance'. Args: evidence: a numpy vector to be learned """ # input checking if not utils.is_list(evidence) and not evidence: raise ValueError("'evidence' has incorrect value") # don't learn empty patterns if self.ignore_background_pattern: is_empty = np.min(evidence) == np.max(evidence) == self.background_color if is_empty: return False if self.algorithm in ('product', 'sum'): # keep only the indices of the maximal activation for each child evidence = utils.get_max_indices(evidence) # if there is no memorized coincidence, store it if len(self.coincidences) == 0: self.coincidences[0] = evidence self.coincidences_stats[0] = 1 else: # store the evidence iff d(evicence, coincidences) > maxDistance # dist is a list of tuples [ (index, distance), ...] dist = self._compute_dist_to_coincidences(evidence) # if self.debug and min(dist, key=itemgetter(1))[1] > 0: # print "-> The distance to the closest coincidence is %.2f" % min(dist, key=itemgetter(1))[1] codebook_is_not_full = len(self.coincidences) < self.max_coincidence_count nearest_coincidence = min(dist, key=itemgetter(1)) # tuple (id, distance) if codebook_is_not_full and nearest_coincidence[1] > self.max_distance: self.coincidences[len(self.coincidences)] = evidence # new coincidence self.coincidences_stats[len(self.coincidences) - 1] = 1 # if self.debug: print "-> New coincidence added, count = %d" % len(self.coincidences) else: # Increase stats for the closest coincidence # nearest_coincidence[0] is the index of the coincidence self.coincidences_stats[nearest_coincidence[0]] += 1
def learn(self, evidence): """Quantize data into 'max_coincidence_count' clusters with maximal size of 'max_distance'. Args: evidence: a numpy vector to be learned """ # input checking if not utils.is_list(evidence) and not evidence: raise ValueError("'evidence' has incorrect value") # don't learn empty patterns if self.ignore_background_pattern: is_empty = np.min(evidence) == np.max(evidence) == self.background_color if is_empty: return False if self.algorithm in ("product", "sum"): # keep only the indices of the maximal activation for each child evidence = utils.get_max_indices(evidence) # if there is no memorized coincidence, store it if len(self.coincidences) == 0: self.coincidences[0] = evidence self.coincidences_stats[0] = 1 else: # store the evidence iff d(evicence, coincidences) > maxDistance # dist is a list of tuples [ (index, distance), ...] dist = self._compute_dist_to_coincidences(evidence) # if self.debug and min(dist, key=itemgetter(1))[1] > 0: # print "-> The distance to the closest coincidence is %.2f" % min(dist, key=itemgetter(1))[1] codebook_is_not_full = len(self.coincidences) < self.max_coincidence_count nearest_coincidence = min(dist, key=itemgetter(1)) # tuple (id, distance) if codebook_is_not_full and nearest_coincidence[1] > self.max_distance: self.coincidences[len(self.coincidences)] = evidence # new coincidence self.coincidences_stats[len(self.coincidences) - 1] = 1 # if self.debug: print "-> New coincidence added, count = %d" % len(self.coincidences) else: # Increase stats for the closest coincidence # nearest_coincidence[0] is the index of the coincidence self.coincidences_stats[nearest_coincidence[0]] += 1
def testMaxIndices(self): l = [[23, 1, 4], [2.4, 5, 11], [44, 22, 105, 18]] r = utils.get_max_indices(l) self.assertTrue(np.array_equal(r, np.array([0, 2, 2])))