【问题标题】:Python - calculate the co-occurrence matrixPython - 计算共现矩阵
【发布时间】:2017-05-30 10:19:53
【问题描述】:

我正在处理 NLP 任务,我需要计算文档的共现矩阵。基本公式如下:

这里我有一个形状为(n, length) 的矩阵,其中每一行代表一个由length 单词组成的句子。所以总共有n 的句子长度相同。然后使用定义的上下文大小,例如window_size = 5,我想计算共现矩阵D,其中cth 行和wth 列中的条目是#(w,c),这意味着数量上下文词 c 出现在 w 的上下文中的次数。

这里可以参考一个例子。 How to calculate the co-occurrence between two words in a window of text?

我知道它可以通过堆叠循环来计算,但我想知道是否存在简单的方法或简单的函数?我找到了一些答案,但它们无法在句子中滑动的窗口中使用。例如:word-word co-occurrence matrix

那么谁能告诉我 Python 中是否有任何函数可以简洁地处理这个问题?因为我认为这个任务在 NLP 中很常见。

【问题讨论】:

    标签: python matrix machine-learning nlp


    【解决方案1】:

    我认为这并不复杂。为什么不为自己制作一个函数? 先按照本教程得到共现矩阵Xhttp://scikit-learn.org/stable/modules/feature_extraction.html#common-vectorizer-usage 然后对于每个句子,计算共现并将它们添加到一个汇总变量中。

    m = np.zeros([length,length]) # n is the count of all words
    def cal_occ(sentence,m):
        for i,word in enumerate(sentence):
            for j in range(max(i-window,0),min(i+window,length)):
                 m[word,sentence[j]]+=1
    for sentence in X:
        cal_occ(sentence, m)
    

    【讨论】:

    • 非常感谢。我已经看到了 CountVectorizer 函数,但它计算的出现不是我想要的。在出现矩阵中,我的意思是,每个项目代表一个 central 单词w 的出现,它是上下文单词cCountVectorizer 中的函数只是计算句子中每对单词的出现次数。我只能通过使用堆栈循环来完成它,我想知道是否存在简单的 API。谢谢你的回复:)
    • @GEORGEGUO 很抱歉误用了 CountVectorizer。但是你可以直接使用我给的代码的转录句子。总之,它并不复杂,你可以自己制作这样的“API”。
    • 我知道。谢谢
    • @Holdwin 感谢您的编辑。这真的很有帮助。我很尴尬,我的答案有这么多错误。
    • @Ruzihm m[word,sentence[j]]+=1 是什么?我收到切片索引错误
    【解决方案2】:

    我计算了窗口大小 =2 的共现矩阵

    1. 首先编写一个函数,给出正确的邻域词(这里我使用了获取上下文)

    2. 创建矩阵,如果邻域中存在特定值,则只需加 1。

    这是python代码:

    import numpy as np
    CORPUS=["abc def ijk pqr", "pqr klm opq", "lmn pqr xyz abc def pqr abc"]
    
    
    top2000 = [ "abc","pqr","def"]#list(set((' '.join(ctxs)).split(' ')))
    a = np.zeros((3,3), np.int32)
    for  sentence in CORPUS:
        for index,word in enumerate(sentence.split(' ')):
           if word in top2000 : 
               print(word)
               context=GetContext(sentence,index)
               print(context)
               for word2 in context:
                 if word2 in top2000:
                     a[top2000.index(word)][top2000.index(word2)]+=1
    print(a)
    

    获取上下文函数

    def GetContext(sentence, index):
    words = sentence.split(' ')
    ret=[]
    for word in words:
    
            if index==0:
                ret.append(words[index+1])
                ret.append(words[index+2])
            elif index==1:
                ret.append(words[index-1])
                ret.append(words[index+1])
            if len(words)>3:
                    ret.append(words[index+2])
            elif index==(len(words)-1):
                ret.append(words[index-2])
                ret.append(words[index-1])
            elif index==(len(words)-2):
                ret.append(words[index-2])
                ret.append(words[index-1])
                ret.append(words[index+1])
            else:
                ret.append(words[index-2])
                ret.append(words[index-1])
                ret.append(words[index+1])
                ret.append(words[index+2])
            return ret     
    

    结果如下:

    array([[0, 3, 3],
       [3, 0, 2],
       [3, 2, 0]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2016-06-04
      • 2021-12-24
      • 1970-01-01
      • 2016-11-12
      • 2017-06-17
      相关资源
      最近更新 更多