Python仅在对上执行t检验

时间:2013-06-06 16:02:07

标签: python hypothesis-test

一段时间后,我得到了帮助来完成这个功能,但我现在已经陷入困境。

        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值对。

有人有想法吗?

示例文件如下所示: 计数pos.txt

529 the
469 want
464 it
449 de

1 个答案:

答案 0 :(得分:0)

此行每次在循环中计算相同的值,因为您每次都会传递所有common_words的计数:

t,p = ttest_ind([word_counts1[k] for k in common_words], [word_counts2[k] for k in common_words])

您是否需要遍历所有common_words