Пример #1
0
 def components_to_target(self, vec):
     try:
         return divisi2.dot(vec, self.target_matrix[:, :vec.shape[-1]])
     except ValueError:
         raise ValueError(
             "Couldn't calculate dot product of %r (shape=%r, k=%d)" %
             (vec, vec.shape, self.k))
Пример #2
0
 def selectEvent(self, index):
     vec = self.luminoso.array[index]
     sim = divisi2.dot(self.luminoso.array,
                       vec) / np.linalg.norm(vec) / np.sqrt(
                           np.sum(self.luminoso.array**2, axis=1))
     sim_indices = np.clip(np.int32(sim * 600 + 300), 0, 599)
     self.luminoso.colors = simcolors[sim_indices]
Пример #3
0
 def move_towards(self, vec, target):
     orig = self.components_to_target(vec)
     delta = (target - orig) / divisi2.dot(vec, vec)
     self.target_matrix += delta[np.newaxis,:] * vec[:,np.newaxis]
     magnitude = np.sum(delta**2)
     power = np.tanh(magnitude)/10
     self.orthogonalize(power=power)
     self.rotated.emit()
Пример #4
0
def get_related_sponsors(email, n=10):
    if not ('sponsor', email) in tag_matrix.row_labels:
	return []
    vec = tag_matrix.row_named(('sponsor', email))
    got = divisi2.dot(tag_matrix, vec)
    results = []
    for tag, weight in got.top_items(len(got)):
        key, value = tag
        if key == 'sponsor':
            results.append((value, weight))
        if len(results) >= n:
            break
    return results
Пример #5
0
    def orthogonalize(self, power=1.0):
        """
        Use normalization and Gram-Schmidt orthogonalization to ensure that
        this projection represents a valid rotation of the component space,
        not a dilation or skew.

        Actually, now it only does that when power=1. When power is less, the
        space will not enforce strict rotations, it will only make the space
        wobble back toward orthogonality.
        """
        prev = self.target_matrix.copy()
        self.target_matrix[:,0] /= np.linalg.norm(self.target_matrix[:,0])
        self.target_matrix[:,1] -= self.target_matrix[:,0] * divisi2.dot(self.target_matrix[:,0], self.matrix[:,1])
        self.target_matrix[:,1] /= np.linalg.norm(self.target_matrix[:,1])
        self.target_matrix[:,:] = self.target_matrix*(power) + prev*(1-power)
        if np.any(np.isnan(self.target_matrix)):
            # better recovery
            self.luminoso.reset_view()
Пример #6
0
 def components_to_target(self, vec):
     try:
         return divisi2.dot(vec, self.target_matrix[:, :vec.shape[-1]])
     except ValueError:
         raise ValueError("Couldn't calculate dot product of %r (shape=%r, k=%d)" % (vec, vec.shape, self.k))
Пример #7
0
    def selectEvent(self, index):
        vec = self.luminoso.array[index]
        sim = divisi2.dot(self.luminoso.array, vec) / np.linalg.norm(vec) / np.sqrt(np.sum(self.luminoso.array ** 2, axis=1))

        sim_indices = np.clip(np.int32(sim*600 + 300), 0, 599)
        self.luminoso.colors = simcolors[sim_indices]
Пример #8
0
 def get_most_similar(self, index, n):
     vec = self.luminoso.array[index]
     sim = divisi2.dot(self.luminoso.array, vec) / np.sqrt(np.sum(self.luminoso.array ** 2, axis=1)) * self.concept_filter
     most_similar = np.argsort(sim)[-n:]
     how_similar = sim[most_similar]
     return zip(most_similar, how_similar)
Пример #9
0
 def component(self, vec, reference):
     "Return the component of a vector in the direction of another vector."
     reference_norm = reference / np.linalg.norm(reference)
     return reference_norm * divisi2.dot(vec, reference_norm)