Python 单词和短语的共现矩阵

标签 python python-2.7 numpy pandas matrix

我正在处理两个文本文件。一个包含 58 个单词的列表 (L1),另一个包含 1173 个短语 (L2)。我要查 for i in range(len(L1))for j in range(len(L1)) L2中的同现.

例如:

L1 = ['b', 'c', 'd', 'e', 't', 'w', 'x', 'y', 'z']
L2 = ['the onion', 'be your self', 'great zoo', 'x men', 'corn day']

for i in range(len(L1)):
    for j in range(len(L1)):
        for s in range(len(L2)):
            if L1[i] in L2[s] and L1[j] in L2[s]:
                output = L1[i], L1[j], L2[s]
                print output

输出(例如 'be your self' 来自 L2 ):

('b', 'b', 'be your self')
('b', 'e', 'be your self')
('b', 'y', 'be your self')
('e', 'b', 'be your self')
('e', 'e', 'be your self')
('e', 'y', 'be your self')
('y', 'b', 'be your self')
('y', 'e', 'be your self')
('y', 'y', 'be your self')

输出显示了我想要的内容,但为了可视化数据,我还需要返回时间 L1[j]同意L1[i] .

例如:

  b e y
b 1 1 1
e 1 2 1
y 1 1 1

我应该使用 pandasnumpy为了返回这个结果?

我发现了这个关于共现矩阵的问题,但我没有找到具体的答案。 efficient algorithm for finding co occurrence matrix of phrases

谢谢!

最佳答案

这是一个使用itertools.product的解决方案。这应该比已接受的解决方案时间要好得多(如果这是一个问题)。

from itertools import product
from operator import mul

L1 = ['b', 'c', 'd', 'e', 't', 'w', 'x', 'y', 'z']
L2 = ['the onion', 'be your self', 'great zoo', 'x men', 'corn day']

phrase_map = {}

for phrase in L2:
    word_count = {word: phrase.count(word) for word in L1 if word in phrase}

    occurrence_map = {}
    for perm in product(word_count, repeat=2):
        occurrence_map[perm] = reduce(mul, (word_count[key] for key in perm), 1)

    phrase_map[phrase] = occurrence_map

根据我的计时,Python 3 的速度快了 2-4 倍(Python 2 的改进可能较小)。另外,在Python 3中,您需要从functools导入reduce

编辑:请注意,虽然此实现相对简单,但效率明显较低。例如,我们知道相应的输出将是对称的,并且该解决方案没有利用这一点。使用 combinations_with_replacements 而不是 product 将仅生成输出矩阵上三角部分中的条目。因此,我们可以通过执行以下操作来改进上述解决方案:

from itertools import combinations_with_replacement

L1 = ['b', 'c', 'd', 'e', 't', 'w', 'x', 'y', 'z']
L2 = ['the onion', 'be your self', 'great zoo', 'x men', 'corn day']

phrase_map = {}

for phrase in L2:
    word_count = {word: phrase.count(word) for word in L1 if word in phrase}

    occurrence_map = {}
    for x, y in combinations_with_replacement(word_count, 2):
        occurrence_map[(x,y)] = occurrence_map[(y,x)] = \
            word_count[x] * word_count[y]

    phrase_map[phrase] = occurrence_map

return phrase_map

正如预期的那样,这个版本的时间是之前版本的一半。请注意,此版本依赖于将自己限制为两个元素对,而以前的版本则不然。

请注意,如果该行可以减少大约 15-20% 的运行时间

 occurrence_map[(x,y)] = occurrence_map[(y,x)] = ...

改为

occurrence_map[(x,y)] = ...

但这可能不太理想,具体取决于您将来如何使用此映射。

关于Python 单词和短语的共现矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001884/

相关文章:

python - 函数引用在循环外丢失

python - 以 10 为基数的 int() 的文字无效 : '' error

python - 创建简单的一次性 Python 对象的简单方法是什么?

python - mp3 音频从交互式 python 播放,但不是从 bash 播放

python - 如何获取字符串中重复出现的字符的位置?

python - np.arange与C++iota对比,iota更慢

python - 如何从html文件中解析文本

python - xterm 无法在 mininet 中工作

python - Python 中的命名字符串格式参数

python - 如何平衡 numpy 数组中的类?