关于tf和idf的定义这里就不再赘述了,网上关于二者的讲解博客非常多,这里只讲一下在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(norm=None)
x = vectorizer.fit_transform(corpus)

print(vectorizer.get_feature_names())
print(x.toarray())
def __init__(self, input='content', encoding='utf-8',
                 decode_error='strict', strip_accents=None, lowercase=True,
                 preprocessor=None, tokenizer=None, analyzer='word',
                 stop_words=None, token_pattern=r"(?u)\b\w\w+\b",
                 ngram_range=(1, 1), max_df=1.0, min_df=1,
                 max_features=None, vocabulary=None, binary=False,
                 dtype=np.float64, norm='l2', use_idf=True, smooth_idf=True,
                 sublinear_tf=False):

TfidfVectorizer中常用的参数有norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False,这四个参数的取值都是默认情况,取值不同计算过程也不同。
smooth_idf=False:
在这里插入图片描述
官网写的是log,一开始不知道是ln,看了好多人写的博客都没提到,通过反复将程序运算结果和公式对比才发现是ln。
smooth_idf=True:
在这里插入图片描述
而且最终算的tf-idf是某个词在其文档中出现的频数乘以idf的值,而不是频率乘以idf,这里要注意。
norm='l2':L2范数标准化处理。拿上面的栗子来说,上面程序得到的tf-idf如下所示:

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

[[0.         1.22314355 1.51082562 1.         0.         0.
  1.         0.         1.        ]
 [0.         2.4462871  0.         1.         0.         1.91629073
  1.         0.         1.        ]
 [1.91629073 0.         0.         1.         1.91629073 0.
  1.         1.91629073 1.        ]
 [0.         1.22314355 1.51082562 1.         0.         0.
  1.         0.         1.        ]]

我们拿第一行的数来说,经过L2范数标准化处理的过程如下:
在这里插入图片描述
最终得到的结果为:

[0.         0.46979139 0.58028582 0.38408524 0.         0.
  0.38408524 0.         0.38408524]
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐