如何获得单词的TF-IDF分数



我有一个很大的语料库(大约有400k个独特的句子(。我只想得到每个单词的TF-IDF分数。我试着通过扫描每个单词并计算频率来计算每个单词的分数,但时间太长了。

我用过:

X= tfidfVectorizer(corpus)

来自sklearn,但它直接返回了句子的向量表示。有什么方法可以让我得到语料库中每个单词的TF-IDF分数吗?

要使用sklearn.feature_extraction.text.TfidfVectorizer(取自文档(:

>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> corpus = [
...     'This is the first document.',
...     'This document is the second document.',
...     'And this is the third one.',
...     'Is this the first document?',
... ]
>>> vectorizer = TfidfVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.shape)
(4, 9)

现在,如果我打印X.toarray():

[[0.         0.46979139 0.58028582 0.38408524 0.         0.
0.38408524 0.         0.38408524]
[0.         0.6876236  0.         0.28108867 0.         0.53864762
0.28108867 0.         0.28108867]
[0.51184851 0.         0.         0.26710379 0.51184851 0.
0.26710379 0.51184851 0.26710379]
[0.         0.46979139 0.58028582 0.38408524 0.         0.
0.38408524 0.         0.38408524]]

该2D数组中的每一行都引用一个文档,该行中的每个元素都引用相应单词的TF-IDF分数。要了解每个元素代表的单词,请查看.get_feature_names()函数。它将打印出单词列表。例如,在这种情况下,查看第一个文档的行:

[0., 0.46979139, 0.58028582, 0.38408524, 0., 0., 0.38408524, 0., 0.38408524]

在本例中,.get_feature_names()返回以下内容:

['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']

因此,你可以将分数映射到这样的单词:

dict(zip(vectorizer.get_feature_names(), X.toarray()[0]))
{'and': 0.0, 'document': 0.46979139, 'first': 0.58028582, 'is': 0.38408524, 'one': 0.0, 'second': 0.0, 'the': 0.38408524, 'third': 0.0, 'this': 0.38408524}
正如评论者所指出的,给出的答案是错误的。下面的方法获取每个令牌的稀疏数组的和。
# initialise vectoriser
tfidf = TfidfVectorizer()
# apply to corpus of documents
X = tfidf.fit_transform(docs)  
# map feature names to sum of vector array
tfidf_dict = dict(zip(tfidf.get_feature_names_out(), X.toarray().sum(axis=0)))
# sort in descending order
tfidf_dict = dict(sorted(tfidf_dict.items(), key=lambda x: x[1], reverse=True))

然后,您可以选择将其显示为pandas数据帧。。。

# initialise dataframe
tfidf_df = pd.DataFrame.from_dict(tfidf_dict, orient='index', columns=['tfidf'])
# name the index
tfidf_df.index = tfidf_df.index.rename('token')
# display first 5 rows
tfidf_df.head()

最新更新