Python 2.7 TFIDF值的转换方式

Python 2.7 TFIDF值的转换方式,python-2.7,tfidfvectorizer,Python 2.7,Tfidfvectorizer,我是NLP新手,请澄清如何使用fit_transform转换TFIDF值 以下计算IDF的公式运行良好, 日志(文件总数+1/术语出现次数+1)+1 例如:文档1中术语“This”(这是一个字符串)的IDF值为1.91629073 应用fit_变换后,所有术语的值都会更改,变换使用的公式\逻辑是什么 TFID=TF*IDF 例如:文档1中术语“This”(这是一个字符串)的TFIDF值为0.61366674 该值是如何得出的,0.61366674 from sklearn.feature_ext

我是NLP新手,请澄清如何使用fit_transform转换TFIDF值

以下计算IDF的公式运行良好, 日志(文件总数+1/术语出现次数+1)+1

例如:文档1中术语“This”(这是一个字符串)的IDF值为1.91629073

应用fit_变换后,所有术语的值都会更改,变换使用的公式\逻辑是什么

TFID=TF*IDF

例如:文档1中术语“This”(这是一个字符串)的TFIDF值为0.61366674

该值是如何得出的,0.61366674

from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd

d = pd.Series(['This is a string','This is another string',
               'TFIDF Computation Calculation','TFIDF is the product of TF and IDF'])


df = pd.DataFrame(d)

tfidf_vectorizer = TfidfVectorizer()

tfidf = tfidf_vectorizer.fit_transform(df[0])


print (tfidf_vectorizer.idf_)

#output
#[1.91629073 1.91629073 1.91629073 1.91629073 1.91629073 1.22314355 1.91629073 
#1.91629073 1.51082562 1.91629073 1.51082562 1.91629073 1.51082562]

##-------------------------------------------------

##how the above values are getting transformed here 

##-------------------------------------------------


print (tfidf.toarray())


#[[0.         0.         0.         0.         0.         0.49681612  0.         
#0.         0.61366674 0.         0.         0.     0.61366674]
# [0.         0.61422608 0.         0.         0.         0.39205255
#  0.         0.         0.4842629  0.         0.         0.  0.4842629 ]
# [0.         0.         0.61761437 0.61761437 0.         0.
#  0.         0.         0.         0.         0.48693426 0.  0.        ]
# [0.37718389 0.         0.         0.         0.37718389 0.24075159
#  0.37718389 0.37718389 0.         0.37718389 0.29737611 0.37718389  0.        ]]

它是赋范的TF-IDF向量,因为默认情况下,
norm='l2'
根据.So在
tfidf.toarray()的输出中
数组第0级/行上的每个元素表示一个文档,第1级/列中的每个元素表示一个唯一的单词,每个文档的向量元素的平方和等于1,您可以通过打印
print([sum([word**2 for word in doc])检查(tfidf.toarray()中的文档的[sum([word**2]))

规范:“l1”、“l2”或无,可选(默认值为“l2”) 每个输出行都有单位范数:“'l2':向量元素的平方和为1。两行之间的余弦相似性 向量是应用l2范数时的点积。*“l1”: 向量元素的绝对值之和为1。请参阅 预处理.规范化

由于TF-IDF值为标准值,向量元素的平方和等于1。例如,对于索引0处的第一个文档,向量元素的平方和等于1:
sum([0.6136667440107333**2,0.4968161174826459**2,0.6136667440107333**2])

通过设置
norm=None
,可以关闭此转换

print(TfidfVectorizer(norm=None).fit_transform(df[0])) #the same values you find in TfidfVectorizer(norm=None).fit_transform(df[0]).toarray(), but more readable
output: ([index of document on array lvl 0 / row], [index of unique word on array lvl 1 / column]) TF-IDF value
(0, 12) 1.5108256237659907 #1st word in 1st sentence: 'This'
(0, 5)  1.2231435513142097 #'is'
(0, 8)  1.5108256237659907 #'string', see that word 'a' is missing
(1, 12) 1.5108256237659907 #'This'
(1, 5)  1.2231435513142097 #'is'
(1, 8)  1.5108256237659907 #'string'
(1, 1)  1.916290731874155  #'another'
(2, 10) 1.5108256237659907 #'TFIDF'
(2, 3)  1.916290731874155  #'Computation'
(2, 2)  1.916290731874155  #'Calculation'
(3, 5)  1.2231435513142097 #'is'
(3, 10) 1.5108256237659907 #'TFIDF'
(3, 11) 1.916290731874155  #'the'
(3, 7)  1.916290731874155  #'product'
(3, 6)  1.916290731874155  #'of'
(3, 9)  1.916290731874155  #'TF'
(3, 0)  1.916290731874155  #'and'
(3, 4)  1.916290731874155  #'IDF'
因为每个单词在每个文档中只出现一次,所以TF-IDF值是每个单词的IDF值乘以1:

tfidf_vectorizer = TfidfVectorizer(norm=None)
tfidf = tfidf_vectorizer.fit_transform(df[0])
print(tfidf_vectorizer.idf_)
output: Smoothed IDF-values
[1.91629073 1.91629073 1.91629073 1.91629073 1.91629073 1.22314355
 1.91629073 1.91629073 1.51082562 1.91629073 1.51082562 1.91629073
 1.51082562]
我希望,以上内容对您有所帮助

不幸的是,我无法复制转换,因为

当l2时,两个向量之间的余弦相似性是它们的点积 规范已被应用

这似乎是一个额外的步骤。因为当您使用默认设置
norm='l2'
时,TF-IDF值会因每个文档中的字数而产生偏差,所以我只需使用
norm=None
关闭此设置。我发现,您不能简单地使用以下方法进行转换:

tfidf_norm_calculated = [
    [(word/sum(doc))**0.5 for word in doc]
    for doc in TfidfVectorizer(norm=None).fit_transform(df[0]).toarray()]
print(tfidf_norm_calculated)
print('Sum of squares of vector elements is 1: ', [sum([word**2 for word in doc]) for doc in tfidf_norm_calculated])
print('Compare to:', TfidfVectorizer().fit_transform(df[0]).toarray())

它是赋范的TF-IDF向量,因为默认情况下,
norm='l2'
根据.So在
tfidf.toarray()的输出中
数组第0级/行上的每个元素表示一个文档,第1级/列中的每个元素表示一个唯一的单词,每个文档的向量元素的平方和等于1,您可以通过打印
print([sum([word**2 for word in doc])检查(tfidf.toarray()中的文档的[sum([word**2]))

规范:“l1”、“l2”或无,可选(默认值为“l2”) 每个输出行都有单位范数:“'l2':向量元素的平方和为1。两行之间的余弦相似性 向量是应用l2范数时的点积。*“l1”: 向量元素的绝对值之和为1。请参阅 预处理.规范化

由于TF-IDF值为标准值,向量元素的平方和等于1。例如,对于索引0处的第一个文档,向量元素的平方和等于1:
sum([0.6136667440107333**2,0.4968161174826459**2,0.6136667440107333**2])

通过设置
norm=None
,可以关闭此转换

print(TfidfVectorizer(norm=None).fit_transform(df[0])) #the same values you find in TfidfVectorizer(norm=None).fit_transform(df[0]).toarray(), but more readable
output: ([index of document on array lvl 0 / row], [index of unique word on array lvl 1 / column]) TF-IDF value
(0, 12) 1.5108256237659907 #1st word in 1st sentence: 'This'
(0, 5)  1.2231435513142097 #'is'
(0, 8)  1.5108256237659907 #'string', see that word 'a' is missing
(1, 12) 1.5108256237659907 #'This'
(1, 5)  1.2231435513142097 #'is'
(1, 8)  1.5108256237659907 #'string'
(1, 1)  1.916290731874155  #'another'
(2, 10) 1.5108256237659907 #'TFIDF'
(2, 3)  1.916290731874155  #'Computation'
(2, 2)  1.916290731874155  #'Calculation'
(3, 5)  1.2231435513142097 #'is'
(3, 10) 1.5108256237659907 #'TFIDF'
(3, 11) 1.916290731874155  #'the'
(3, 7)  1.916290731874155  #'product'
(3, 6)  1.916290731874155  #'of'
(3, 9)  1.916290731874155  #'TF'
(3, 0)  1.916290731874155  #'and'
(3, 4)  1.916290731874155  #'IDF'
因为每个单词在每个文档中只出现一次,所以TF-IDF值是每个单词的IDF值乘以1:

tfidf_vectorizer = TfidfVectorizer(norm=None)
tfidf = tfidf_vectorizer.fit_transform(df[0])
print(tfidf_vectorizer.idf_)
output: Smoothed IDF-values
[1.91629073 1.91629073 1.91629073 1.91629073 1.91629073 1.22314355
 1.91629073 1.91629073 1.51082562 1.91629073 1.51082562 1.91629073
 1.51082562]
我希望,以上内容对您有所帮助

不幸的是,我无法复制转换,因为

当l2时,两个向量之间的余弦相似性是它们的点积 规范已被应用

这似乎是一个额外的步骤。因为当您使用默认设置
norm='l2'
时,TF-IDF值会因每个文档中的字数而产生偏差,所以我只需使用
norm=None
关闭此设置。我发现,您不能简单地使用以下方法进行转换:

tfidf_norm_calculated = [
    [(word/sum(doc))**0.5 for word in doc]
    for doc in TfidfVectorizer(norm=None).fit_transform(df[0]).toarray()]
print(tfidf_norm_calculated)
print('Sum of squares of vector elements is 1: ', [sum([word**2 for word in doc]) for doc in tfidf_norm_calculated])
print('Compare to:', TfidfVectorizer().fit_transform(df[0]).toarray())