嗨,不久前我得到了帮助来实现这个功能,但我现在被卡住了。
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