def similarity_to(self, other, metric = "cosine"): """ set-to-set similarity Param: docs: KeywordList Return: float """ assert type(other) in (scinet3.model.Keyword, KeywordList), "`other` should be either Keyword or KeywordList, but is %r" %other if isinstance(other, scinet3.model.Keyword): other = KeywordList([other]) if metric == "cosine": return cosine_similarity(self.centroid, other.centroid) else: raise NotImplementedError("Only cosine similarity metric is implemented for now")
def similarity_to(self, other, metric="cosine"): """ set-to-set similarity Param: docs: KeywordList Return: float """ assert type(other) in ( scinet3.model.Keyword, KeywordList ), "`other` should be either Keyword or KeywordList, but is %r" % other if isinstance(other, scinet3.model.Keyword): other = KeywordList([other]) if metric == "cosine": return cosine_similarity(self.centroid, other.centroid) else: raise NotImplementedError( "Only cosine similarity metric is implemented for now")
def test_sparse_matrix(self): v1 = csr_matrix([self.row1]) v2 = csr_matrix([self.row2]) self.assertAlmostEqual(self.expected, cosine_similarity(v1, v2))
def test_array(self): v1 = np.array(self.row1) v2 = np.array(self.row2) self.assertAlmostEqual(self.expected, cosine_similarity(v1, v2))
def test_list(self): v1 = self.row1 v2 = self.row2 self.assertAlmostEqual(self.expected, cosine_similarity(v1, v2))