繁体   English   中英

python中的tf-idf函数需要帮助才能满足我的输出

[英]tf-idf function in python need help to satisfy my output

我写了一个基本上计算逆文档频率的函数(对数基数为10(总文档数/包含特定单词的文档数))

我的代码:

def tfidf(docs,doc_freqs):
    res = []
    t = sum(isinstance(i, list) for i in docs)
    for key,val in doc_freqs.items():
        res.append(math.log10(t/val))
    pos = defaultdict(lambda:[])
    for docID, lists in enumerate(docs):
        for element in set(lists):
            pos[element].append([docID] + res)
    return pos

我的输出:

index = tfidf([['a', 'b', 'c'], ['a']], {'a': 2., 'b': 1., 'c': 1.})
index['a']
[[0, 0.0, 0.3010299956639812, 0.3010299956639812], [1, 0.0, 0.3010299956639812, 0.3010299956639812]]
index['b']
[[0, 0.0, 0.3010299956639812, 0.3010299956639812]]

所需的输出:

index = tfidf([['a', 'b', 'c'], ['a']], {'a': 2., 'b': 1., 'c': 1.})
index['a']
[[0, 0.0], [1, 0.0]]
index['b']
[[0, 0.3010299956639812]]

所以基本上我只想显示该词出现的docid,然后仅显示其idf值。 (即)在上面的示例中,因为两个文档中都出现了术语'a',所以idf值为0。

谁能建议我需要在代码中进行哪些修改,才能根据运行时指定的术语仅打印相应的idf值?

请帮忙 !!! 提前致谢。

狼,

现在,您要将整个res附加到[docID] ,但是您只关心与该element关联的值。 我建议将res更改为类似以下代码的dict

import math

def tfidf(docs,doc_freqs):
    res = {}
    t = sum(isinstance(i, list) for i in docs)
    for key,val in doc_freqs.items():
        res[key] = math.log10(t/val)
    pos = defaultdict(lambda:[])
    for docID, lists in enumerate(docs):
        for element in set(lists):
            pos[element].append([docID, res[element]])
    return pos

docs = [['a', 'b', 'a'], ['a']]
doc_freqs = {'a': 2., 'b': 1., 'c': 1.}

index = tfidf(docs, doc_freqs)

这是您的输出:

index['a']
[[0, 0.0], [1, 0.0]]

index['b']
[[0, 0.3010299956639812]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM