如何创建频率矩阵?
How to create a frequency matrix?
我刚开始使用 Python 并且遇到了以下问题:
假设我有以下列表:
list = [["Word1","Word2","Word2","Word4566"],["Word2", "Word3", "Word4"], ...]
我想要得到的结果(矩阵)应该是这样的:
显示的列和行都是出现过的词(无论哪个列表)
我想要的是一个计算每个列表中单词出现次数的程序(按列表)。
图片是第一个列表后的结果
有没有一种简单的方法可以实现这样或类似的东西?
编辑:
基本上我想要一个 List/Matrix 来告诉我当单词 1 也在列表中时单词 2-4566 出现了多少次,等等。
所以我会得到每个单词的列表,显示与该单词相关的所有其他 4555 个单词的绝对频率。
所以我需要一种算法来遍历所有这些单词列表并构建结果列表
我发现很难理解您的真正要求,但我会尝试做一些假设:
- (1) 你有一个列表 (A),包含多个单词 (w).
- (2) 对于 A-list 中的每个 b-list
- (3) 对于 b 中的每个 w:
- (3.1) 统计w在所有b-lists
中出现的总次数
- (3.2) 计算有多少个 b-列表,其中 w 只出现一次
如果这些假设正确,则 table 与您提供的列表不符。如果我的假设是错误的,那么我仍然相信我的解决方案可能会给你一些灵感或一些关于如何正确解决它的想法。最后,我并不声称我的解决方案在速度或类似方面是最佳的。
天啊!!我使用 python 的内置词典,如果你打算用数千个单词填充它们,它可能会变得非常慢!!看看:https://docs.python.org/2/tutorial/datastructures.html#dictionaries
frq_dict = {} # num of appearances / frequency
uqe_dict = {} # unique
for list_b in list_A:
temp_dict = {}
for word in list_b:
if( word in temp_dict ):
temp_dict[word]+=1
else:
temp_dict[word]=1
# frq is the number of appearances
for word, frq in temp_dict.iteritems():
if( frq > 1 ):
if( word in frq_dict )
frq_dict[word] += frq
else
frq_dict[word] = frq
else:
if( word in uqe_dict )
uqe_dict[word] += 1
else
uqe_dict[word] = 1
据我了解,您想创建一个矩阵来显示每对单词中两个单词位于一起的列表的数量。
首先我们应该固定唯一词的集合:
lst = [["Word1","Word2","Word2","Word4566"],["Word2", "Word3", "Word4"], ...] # list is a reserved word in python, don't use it as a name of variables
words = set()
for sublst in lst:
words |= set(sublst)
words = list(words)
其次我们应该定义一个带零的矩阵:
result = [[0] * len(words)] * len(words) # zeros matrix N x N
最后我们通过给定列表填充矩阵:
for sublst in lst:
sublst = list(set(sublst)) # selecting unique words only
for i in xrange(len(sublst)):
for j in xrange(i + 1, len(sublst)):
index1 = words.index(sublst[i])
index2 = words.index(sublst[j])
result[index1][index2] += 1
result[index2][index1] += 1
print result
我设法找到了我自己问题的正确答案:
list = [["Word1","Word2","Word2"],["Word2", "Word3", "Word4"],["Word2","Word3"]]
#Names of all dicts
all_words = sorted(set([w for sublist in list for w in sublist]))
#Creating the dicts
dicts = []
for i in all_words:
dicts.append([i, dict.fromkeys([w for w in all_words if w != i],0)])
#Updating the dicts
for l in list:
for word in sorted(set(l)):
tmpL = [w for w in l if w != word]
ind = ([w[0] for w in dicts].index(word))
for w in dicts[ind][1]:
dicts[ind][1][w] += l.count(w)
print dicts
得到结果:
['Word1', {'Word4': 0, 'Word3': 0, 'Word2': 2}], ['Word2', {'Word4': 1, 'Word1': 1, 'Word3': 2}], ['Word3', {'Word4': 1, 'Word1': 0, 'Word2': 2}], ['Word4', {'Word1': 0, 'Word3': 1, 'Word2': 1}]]
我刚开始使用 Python 并且遇到了以下问题:
假设我有以下列表:
list = [["Word1","Word2","Word2","Word4566"],["Word2", "Word3", "Word4"], ...]
我想要得到的结果(矩阵)应该是这样的:
显示的列和行都是出现过的词(无论哪个列表)
我想要的是一个计算每个列表中单词出现次数的程序(按列表)。
图片是第一个列表后的结果
有没有一种简单的方法可以实现这样或类似的东西?
编辑:
基本上我想要一个 List/Matrix 来告诉我当单词 1 也在列表中时单词 2-4566 出现了多少次,等等。
所以我会得到每个单词的列表,显示与该单词相关的所有其他 4555 个单词的绝对频率。
所以我需要一种算法来遍历所有这些单词列表并构建结果列表
我发现很难理解您的真正要求,但我会尝试做一些假设:
- (1) 你有一个列表 (A),包含多个单词 (w).
- (2) 对于 A-list 中的每个 b-list
- (3) 对于 b 中的每个 w:
- (3.1) 统计w在所有b-lists 中出现的总次数
- (3.2) 计算有多少个 b-列表,其中 w 只出现一次
- (3) 对于 b 中的每个 w:
如果这些假设正确,则 table 与您提供的列表不符。如果我的假设是错误的,那么我仍然相信我的解决方案可能会给你一些灵感或一些关于如何正确解决它的想法。最后,我并不声称我的解决方案在速度或类似方面是最佳的。
天啊!!我使用 python 的内置词典,如果你打算用数千个单词填充它们,它可能会变得非常慢!!看看:https://docs.python.org/2/tutorial/datastructures.html#dictionaries
frq_dict = {} # num of appearances / frequency
uqe_dict = {} # unique
for list_b in list_A:
temp_dict = {}
for word in list_b:
if( word in temp_dict ):
temp_dict[word]+=1
else:
temp_dict[word]=1
# frq is the number of appearances
for word, frq in temp_dict.iteritems():
if( frq > 1 ):
if( word in frq_dict )
frq_dict[word] += frq
else
frq_dict[word] = frq
else:
if( word in uqe_dict )
uqe_dict[word] += 1
else
uqe_dict[word] = 1
据我了解,您想创建一个矩阵来显示每对单词中两个单词位于一起的列表的数量。
首先我们应该固定唯一词的集合:
lst = [["Word1","Word2","Word2","Word4566"],["Word2", "Word3", "Word4"], ...] # list is a reserved word in python, don't use it as a name of variables
words = set()
for sublst in lst:
words |= set(sublst)
words = list(words)
其次我们应该定义一个带零的矩阵:
result = [[0] * len(words)] * len(words) # zeros matrix N x N
最后我们通过给定列表填充矩阵:
for sublst in lst:
sublst = list(set(sublst)) # selecting unique words only
for i in xrange(len(sublst)):
for j in xrange(i + 1, len(sublst)):
index1 = words.index(sublst[i])
index2 = words.index(sublst[j])
result[index1][index2] += 1
result[index2][index1] += 1
print result
我设法找到了我自己问题的正确答案:
list = [["Word1","Word2","Word2"],["Word2", "Word3", "Word4"],["Word2","Word3"]]
#Names of all dicts
all_words = sorted(set([w for sublist in list for w in sublist]))
#Creating the dicts
dicts = []
for i in all_words:
dicts.append([i, dict.fromkeys([w for w in all_words if w != i],0)])
#Updating the dicts
for l in list:
for word in sorted(set(l)):
tmpL = [w for w in l if w != word]
ind = ([w[0] for w in dicts].index(word))
for w in dicts[ind][1]:
dicts[ind][1][w] += l.count(w)
print dicts
得到结果:
['Word1', {'Word4': 0, 'Word3': 0, 'Word2': 2}], ['Word2', {'Word4': 1, 'Word1': 1, 'Word3': 2}], ['Word3', {'Word4': 1, 'Word1': 0, 'Word2': 2}], ['Word4', {'Word1': 0, 'Word3': 1, 'Word2': 1}]]