对于不同的单词获得相同的tfidf值

时间:2018-07-03 19:52:12

标签: python-3.x sentiment-analysis tf-idf tfidfvectorizer

所以我正在做一个有关推特情绪分析的项目,其中我碰巧需要在收集的推文上使用TFIDF。 因此,我将推文列表转换为单个字符串,并将其提供给对象,问题是我为大多数单词获得了相同的值,但具有一些不同的值,但它们也很频繁。为什么会这样呢?是因为我使用单个字符串作为输入吗?

这是代码https://trinket.io/python/9c2daed912

Here is the screenshot, as you can see many have same TFIDF values

1 个答案:

答案 0 :(得分:0)

根据出现的次数,不同单词的频率可以是相同的数字。我的gutenberg项目代码产生以下结果。

from sklearn.feature_extraction.text import TfidfVectorizer
tfvect = TfidfVectorizer(stop_words='english')
### The corpus is from Project Gutenberg after all the text cleanup.
karlmarx_freq = tfvect.fit_transform(gutenberg_KarlMarx_Corpus)

tftermFreq = pd.DataFrame(karlmarx_freq.toarray(),columns=tfvect.get_feature_names())
tfsumdf = tftermFreq.sum(axis=0)
pd.DataFrame({'Vocab': tfsumdf.index, 'Frequency': tfsumdf.values}).sort_values(by='Frequency', ascending=False)

结果是:

1412产量0.177513

345个条件0.174032

1151现代0.142706

1704社交0.128784

1000劳动0.128784

641存在0.111381

1705社会主义0.104419

1117表示0.100939

923行业0.100939

有关如何进行此计算的详细信息,请参阅scikit-learn文档。

Tf-Idf =术语频率*反文档频率