我有一个关于sklearn的TfidfVectorizer的问题,当它在每个文档中执行单词的频率时。
我看到的示例代码是:
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> corpus = [
>>> 'The dog ate a sandwich and I ate a sandwich',
>>> 'The wizard transfigured a sandwich'
>>> ]
>>> vectorizer = TfidfVectorizer(stop_words='english')
>>> print vectorizer.fit_transform(corpus).todense()
[[ 0.75458397 0.37729199 0.53689271 0. 0. ]
[ 0. 0. 0.44943642 0.6316672 0.6316672 ]]
我的问题是:如何解释矩阵中的数字?我知道 0 意味着单词即向导在第一个文档中出现 0 次,因此它是 0,但是我如何解释数字 0.75458397?是第一份文件中"吃"字出现的频率吗?还是整个语料库中出现"吃"字的频率?
TF-IDF(意思是"术语频率 - 反向文档频率")在表示中没有给出术语的频率。
TF-IDF只对极少数文档中出现的术语给予高分,对许多文档中出现的术语给予低分,因此粗略地说,它是衡量给定文档中术语的歧视性。查看此资源以找到TF-IDF的出色描述,并更好地了解它在做什么。
如果您只需要计数,则需要使用 CountVectorizer
.
我想你忘记了 TF-IDF 向量通常是归一化的,所以它们的幅度(长度或 2 范数)总是为 1。
因此,TFIDF 值0.75
是"ate"的频率乘以"ate"的反向文档频率,然后除以该 TF-IDF 矢量的大小。
以下是所有肮脏的细节(跳到tfidf0 =
的要点):
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ["The dog ate a sandwich and I ate a sandwich",
"The wizard transfigured a sandwich"]
vectorizer = TfidfVectorizer(stop_words='english')
tfidfs = vectorizer.fit_transform(corpus)
from collections import Counter
import pandas as pd
columns = [k for (v, k) in sorted((v, k)
for k, v in vectorizer.vocabulary_.items())]
tfidfs = pd.DataFrame(tfidfs.todense(),
columns=columns)
# ate dog sandwich transfigured wizard
#0 0.75 0.38 0.54 0.00 0.00
#1 0.00 0.00 0.45 0.63 0.63
df = (1 / pd.DataFrame([vectorizer.idf_], columns=columns))
# ate dog sandwich transfigured wizard
#0 0.71 0.71 1.0 0.71 0.71
corp = [txt.lower().split() for txt in corpus]
corp = [[w for w in d if w in vectorizer.vocabulary_] for d in corp]
tfs = pd.DataFrame([Counter(d) for d in corp]).fillna(0).astype(int)
# ate dog sandwich transfigured wizard
#0 2 1 2 0 0
#1 0 0 1 1 1
# The first document's TFIDF vector:
tfidf0 = tfs.iloc[0] * (1. / df)
tfidf0 = tfidf0 / pd.np.linalg.norm(tfidf0)
# ate dog sandwich transfigured wizard
#0 0.754584 0.377292 0.536893 0.0 0.0
tfidf1 = tfs.iloc[1] * (1. / df)
tfidf1 = tfidf1 / pd.np.linalg.norm(tfidf1)
# ate dog sandwich transfigured wizard
#0 0.0 0.0 0.449436 0.631667 0.631667
只需打印下面的代码,您将看到输出类似的东西
#(0, 1) 0.448320873199 Document 1, term = Dog
#(0, 3) 0.630099344518 Document 1, term = Sandwitch
print(vectorizer.fit_transform(corpus))
# if python 3 other wise remove () in print
注意:如果你只有单字母,请使用这个
SKLEARN的TFIDFvectorizer不会直接给你计数。若要获取计数,可以使用类方法TfidfVectorizer
inverse_transform
和build_tokenizer
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
'The dog ate a sandwich and I ate a sandwich',
'The wizard transfigured a sandwich'
]
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(corpus)
X_words = tfidf.inverse_transform(X) ## this will give you words instead of tfidf where tfidf > 0
tokenizer = vectorizer.build_tokenizer() ## return tokenizer function used in tfidfvectorizer
for idx,words in enumerate(X_words):
for word in words:
count = tokenizer(corpus[idx]).count(word)
print(idx,word,count)
输出
0 dog 1
0 ate 2
0 sandwich 2
1 sandwich 1
1 wizard 1
1 transfigured 1
#0 means first sentence in corpus
这是一个希望可以帮助某人的工作:)
它应该vectorizer
在行中 X_words = tfidf.inverse_transform(X)
而不是 TFIDF。