word2vec_as_MF.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:ro_sgns 作者: AlexGrinch 项目源码 文件源码
def nearest_words(self, word, top=20, display=False):
        """
        Find the nearest words to the word 
        according to the cosine similarity.
        """

        W = self.W / np.linalg.norm(self.W, axis=0)   
        if (type(word)==str):
            vec = self.word_vector(word, W)
        else:
            vec = word / np.linalg.norm(word)

        cosines = (vec.T).dot(W)
        args = np.argsort(cosines)[::-1]       

        nws = []
        for i in xrange(1, top+1):
            nws.append(self.inv_vocab[args[i]])
            if (display):
                print self.inv_vocab[args[i]], round(cosines[args[i]],3)

        return nws
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号