类型错误:“TfidfModel”对象不可调用
为什么初始化后无法计算每个文档的 TFIDF 矩阵?
我从 999 个文档开始:999 个段落,每个段落大约有 5-15 个句子。 在 spaCy 对所有内容进行标记后,我创建了字典(约 16k 个唯一标记)和语料库(元组列表的列表)
现在我准备为一些 ML 创建 tfidf 矩阵(以及后来的 LDA 和 w2V 矩阵);但是,在使用我的语料库初始化 tfidf 模型后(用于计算“IDF”)
tfidf = models.TfidfModel(corpus)
当尝试查看每个文档的 tfidf tfidf(corpus[5])
时,我收到以下错误消息
类型错误:“TfidfModel”对象不可调用
我可以使用不同的语料库创建这个模型,其中我有四个文档,每个文档仅包含一个句子。 在那里我可以确认预期的语料库格式是元组列表的列表: [doc1[(word1, count),(word2, count),...], doc2[(word3, count),(word4,count),...]...]
from gensim import corpora, models, similarities
texts = [['teenager', 'martha', 'moxley'...], ['ok','like','kris','usual',...]...]
dictionary = corpora.Dictionary(texts)
>>> Dictionary(15937 unique tokens: ['teenager', 'martha', 'moxley']...)
corpus = [dictionary.doc2bow(text) for text in texts]
>>> [[(0, 2),(1, 2),(2, 1)...],[(3, 1),(4, 1)...]...]
tfidf = models.TfidfModel(corpus)
>>> TfidfModel(num_docs=999, num_nnz=86642)
tfidf(corpus[0])
>>> TypeError: 'TfidfModel' object is not callable
corpus[0]
>>> [(0, 2),(1, 2),(2, 1)...]
print(type(corpus),type(corpus[1]),type(corpus[1][3]))
>>> <class 'list'> <class 'list'> <class 'tuple'>
最佳答案
而不是:tfidf(corpus[0])
尝试:tfidf[语料库[0]]
关于python - Gensim 中的 TFIDIF 模型创建类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368720/