scikitlearn TfidfVectorizer忽略某些单词

2024-06-26 14:50:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试TfidfVectorizer从维基百科关于葡萄牙历史的一个句子。但是我注意到TfidfVec.fit_transform方法忽略了某些单词。我试过的句子是:

sentence = "The oldest human fossil is the skull discovered in the Cave of Aroeira in Almonda."

TfidfVec = TfidfVectorizer()
tfidf = TfidfVec.fit_transform([sentence])

cols = [words[idx] for idx in tfidf.indices]
matrix = tfidf.todense()
pd.DataFrame(matrix,columns = cols,index=["Tf-Idf"])

数据帧的输出:

enter image description here

本质上,它忽略了“Aroeira”和“Almonda”两个词。在

但我不想让它忽视这些话,那我该怎么办?我在文件上找不到他们谈论此事的地方。在

另一个问题是为什么要重复“the”这个词?算法是否应该只考虑一个“the”并计算其tf-idf?在


Tags: theintransform历史matrixsentence句子fit
2条回答

tfidf.indices只是TfidfVectorizer中功能名称的索引。 用这个索引从句子中找出单词是错误的。在

您应该得到df的列名为TfidfVec.get_feature_names()

enter image description here

输出是给二,因为句子中有两个。整个句子都被编码了,你可以得到每个索引的值。其他两个词没有出现的原因是因为它们是稀有词。通过降低阈值可以使它们出现。在

请参阅最小和最大功能:
http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html

相关问题 更多 >