1

这是我长期以来一直在绞尽脑汁的一个问题,所以任何帮助都会很棒。我有一个文件,其中包含以下格式的几行(单词,单词出现的时间,以及在给定实例中包含给定单词的文档的频率)。下面是输入文件的示例。

#inputfile
<word, time, frequency>
apple, 1, 3
banana, 1, 2
apple, 2, 1
banana, 2, 4
orange, 3, 1

我有下面的 Python 类,我用来创建二维字典来存储上述文件,使用作为键,频率作为值:

class Ddict(dict):
    '''
    2D dictionary class
    '''
    def __init__(self, default=None):
            self.default = default

    def __getitem__(self, key):
            if not self.has_key(key):
                self[key] = self.default()
            return dict.__getitem__(self, key)


wordtime=Ddict(dict) # Store each inputfile entry with a <word,time> key
timeword=Ddict(dict) # Store each inputfile entry with a <time,word> key

# Loop over every line of the inputfile
for line in open('inputfile'):
    word,time,count=line.split(',')

    # If <word,time> already a key, increment count
    try:
        wordtime[word][time]+=count
    # Otherwise, create the key
    except KeyError:
        wordtime[word][time]=count

    # If <time,word> already a key, increment count     
    try:
        timeword[time][word]+=count
    # Otherwise, create the key
    except KeyError:
        timeword[time][word]=count

我的问题与在迭代此 2D 字典中的条目时计算某些事物有关。对于每个时间 't' 的每个单词 'w',计算:

  1. 在时间 't'包含 单词 'w'的文档数。(一个)
  2. 在时间 't'没有 单词 'w'的文档数。(二)
  3. 在时间 't'之外带有 单词 'w'的文档数。(C)
  4. 在时间 't'之外没有 单词 'w'的文档数。(d)

上面的每个项目代表每个单词和时间的卡方列联表的一个单元格。所有这些都可以在一个循环中计算,还是需要一次完成一个?

理想情况下,我希望输出如下所示,其中 a、b、c、d 是上面计算的所有项目:

print "%s, %s, %s, %s" %(a,b,c,d)

在上述输入文件的情况下,尝试在时间 '1' 查找单词 'apple' 的列联表的结果将是(3,2,1,6). 我将解释如何计算每个单元格:

  • '3' 文档在时间'1' 内包含'apple'。
  • 在时间“1”内有“2”个文档不包含“apple”。
  • 在时间 '1' 之外有 '1' 个包含 'apple' 的文档。
  • 在时间 '1' 之外有 6 个文档不包含单词 'apple' (1+4+1)。
4

1 回答 1

2

您的 apple/1 的 4 个数字加起来为 12,超过了观察总数 (11)!在时间 '1' 之外只有 5 个文档不包含单词 'apple'。

您需要将观察结果划分为 4 个不相交的子集:
a: apple and 1 => 3
b: not-apple and 1 => 2
c: apple and not-1 => 1
d: not-apple and not-1 => 5

这是一些代码,显示了一种方法:

from collections import defaultdict

class Crosstab(object):

    def __init__(self):
        self.count = defaultdict(lambda: defaultdict(int))
        self.row_tot = defaultdict(int)
        self.col_tot = defaultdict(int)
        self.grand_tot = 0

    def add(self, r, c, n):
        self.count[r][c] += n
        self.row_tot[r] += n
        self.col_tot[c] += n
        self.grand_tot += n

def load_data(line_iterator, conv_funcs):
    ct = Crosstab()
    for line in line_iterator:
        r, c, n = [func(s) for func, s in zip(conv_funcs, line.split(','))]
        ct.add(r, c, n)
    return ct

def display_all_2x2_tables(crosstab):
    for rx in crosstab.row_tot:
        for cx in crosstab.col_tot:
            a = crosstab.count[rx][cx]
            b = crosstab.col_tot[cx] - a
            c = crosstab.row_tot[rx] - a
            d = crosstab.grand_tot - a - b - c
            assert all(x >= 0 for x in (a, b, c, d))
            print ",".join(str(x) for x in (rx, cx, a, b, c, d))

if __name__ == "__main__":

    # inputfile
    # <word, time, frequency>
    lines = """\
    apple, 1, 3
    banana, 1, 2
    apple, 2, 1
    banana, 2, 4
    orange, 3, 1""".splitlines()

    ct = load_data(lines, (str.strip, int, int))
    display_all_2x2_tables(ct)

这是输出:

orange,1,0,5,1,5
orange,2,0,5,1,5
orange,3,1,0,0,10
apple,1,3,2,1,5
apple,2,1,4,3,3
apple,3,0,1,4,6
banana,1,2,3,4,2
banana,2,4,1,2,4
banana,3,0,1,6,4
于 2010-06-13T00:49:12.970 回答