示例#1
0
    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")
示例#2
0
    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")
示例#3
0
    def test_sparse_matrix(self):
        v1 = csr_matrix([self.row1])
        v2 = csr_matrix([self.row2])

        self.assertAlmostEqual(self.expected,
                               cosine_similarity(v1, v2))
示例#4
0
 def test_array(self):
     v1 = np.array(self.row1)
     v2 = np.array(self.row2)
     
     self.assertAlmostEqual(self.expected,
                            cosine_similarity(v1, v2))
示例#5
0
 def test_list(self):
     v1 = self.row1
     v2 = self.row2
     self.assertAlmostEqual(self.expected,
                            cosine_similarity(v1, v2))
示例#6
0
    def test_sparse_matrix(self):
        v1 = csr_matrix([self.row1])
        v2 = csr_matrix([self.row2])

        self.assertAlmostEqual(self.expected, cosine_similarity(v1, v2))
示例#7
0
    def test_array(self):
        v1 = np.array(self.row1)
        v2 = np.array(self.row2)

        self.assertAlmostEqual(self.expected, cosine_similarity(v1, v2))
示例#8
0
 def test_list(self):
     v1 = self.row1
     v2 = self.row2
     self.assertAlmostEqual(self.expected, cosine_similarity(v1, v2))