Python 尝试复制TFIDF示例时,乘法返回错误的数字

Python 尝试复制TFIDF示例时,乘法返回错误的数字,python,python-3.x,tf-idf,Python,Python 3.x,Tf Idf,我正在尝试复制此视频中的TFIDF示例: 据我所知,代码与示例中的代码相同,只是我使用了.items(python 3)而不是.iteritems(python 2): 生成的表应该是这样的,其中常用词(on、my、sat、The)的分数都是0: bed cat dog face my on sat the 0 0.000000 0.115525 0.000000 0.11552

我正在尝试复制此视频中的TFIDF示例:

据我所知,代码与示例中的代码相同,只是我使用了.items(python 3)而不是.iteritems(python 2):

生成的表应该是这样的,其中常用词(on、my、sat、The)的分数都是0:

         bed       cat       dog      face        my        on       sat       the   
0  0.000000  0.115525  0.000000  0.115525  0.000000  0.000000  0.000000  0.000000   
1  0.115525  0.000000  0.115525  0.000000  0.000000  0.000000  0.000000  0.000000 
但是,我的结果数据框看起来是这样的,所有的单词都有相同的分数,除了那些仅仅出现在文档中的单词(bed\dog,cat\face):

如果我打印(IDF),我会

在这里,两个文档中包含的单词的值为0,然后将使用该值来衡量它们的重要性,因为它们对所有文档都是通用的。在使用ComputeFidf函数之前,数据如下所示:

{'my': 0.1666, 'sat': 0.1666, 'dog': 0.0, 'cat': 0.1666, 'on': 0.1666, 'the': 0.1666, 'face': 0.1666, 'bed': 0.0}

由于函数将两个数字相乘,“my”(idfs为0)应为0,“dog”(idfs为0.6931)应为(06931*01666=0,11),如示例所示。取而代之的是,除了文档中不存在的单词外,我得到的数字是0.02083。除了python 2和3之间的iter\iteritems语法之外,还有什么东西会弄乱我的代码吗?

在转换为
df
之前的最后第二部分中,更改这两行-

tfidfBowA = computeTF(tfBowA, idfs)
tfidfBowB = computeTF(tfBowB, idfs)
至-

要计算
Tfidf
,必须调用函数
computeTFIDF()
,而不是
computeTF()

输出

tfidfBowA
{'bed': 0.0,
 'cat': 0.11552453009332421,
 'dog': 0.0,
 'face': 0.11552453009332421,
 'my': 0.0,
 'on': 0.0,
 'sat': 0.0,
 'the': 0.0}

tfidfBowB
{'bed': 0.11552453009332421,
 'cat': 0.0,
 'dog': 0.11552453009332421,
 'face': 0.0,
 'my': 0.0,
 'on': 0.0,
 'sat': 0.0,
 'the': 0.0}

希望有帮助

我真不敢相信我错过了。太明显了。谢谢你,我是瞎读的,没有发现任何差异!
{'my': 0.1666, 'sat': 0.1666, 'dog': 0.0, 'cat': 0.1666, 'on': 0.1666, 'the': 0.1666, 'face': 0.1666, 'bed': 0.0}
tfidfBowA = computeTF(tfBowA, idfs)
tfidfBowB = computeTF(tfBowB, idfs)
tfidfBowA = computeTFIDF(tfBowA, idfs)
tfidfBowB = computeTFIDF(tfBowB, idfs)
tfidfBowA
{'bed': 0.0,
 'cat': 0.11552453009332421,
 'dog': 0.0,
 'face': 0.11552453009332421,
 'my': 0.0,
 'on': 0.0,
 'sat': 0.0,
 'the': 0.0}

tfidfBowB
{'bed': 0.11552453009332421,
 'cat': 0.0,
 'dog': 0.11552453009332421,
 'face': 0.0,
 'my': 0.0,
 'on': 0.0,
 'sat': 0.0,
 'the': 0.0}