嗨,前段时间我得到了帮助来实现这个功能,但我现在完全卡住了。
from scipy.stats import ttest_ind
def input_file_to_dict(f):
return dict((key, int(value)) for value, key in map(lambda line:line.split(), f))
with open("count-pos.txt") as f:
word_counts1 = input_file_to_dict(f)
with open("count-neg.txt") as f:
word_counts2 = input_file_to_dict(f)查找list1和list2中的所有单词
out = open('t-test_output.txt', 'w')
common_words = set.intersection(set(word_counts1.keys()), set(word_counts2.keys()))
for line in common_words:
t,p = ttest_ind([word_counts1[k] for k in common_words], [word_counts2[k] for k in common_words])
print >> out, (t,p)可以看出,我正在尝试比较包含单词频率的两个列表,但是有些单词不会出现在两个样本大小中。我希望对每个单词对进行t测试,以确定它们的方差。然而,这给了我一次又一次相同的t值和p值对。
有谁有什么想法吗?
示例文件如下所示: count-pos.txt
529 the
469 want
464 it
449 de发布于 2013-06-07 00:39:50
这一行每次都在循环中计算相同的值,因为您每次都会传入所有common_words的计数:
t,p = ttest_ind([word_counts1[k] for k in common_words], [word_counts2[k] for k in common_words])你需要遍历所有的common_words吗
https://stackoverflow.com/questions/16966854
复制相似问题