我正在处理两个文本文件。一个包含 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
我应该使用 pandas 吗?或 numpy为了返回这个结果?
我发现了这个关于共现矩阵的问题,但我没有找到具体的答案。 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)] = ...
但这可能不太理想,具体取决于您将来如何使用此映射。
