python-3.x - 如何使用 TF-IDF 向量选择前 1000 个单词?

标签 python-3.x scikit-learn tf-idf sklearn-pandas tfidfvectorizer

我有一个包含 5000 条评论的文档。我在该文件上应用了 tf-idf。这里样本数据 包含 5000 条评论。我正在使用 对 sample_data 应用 tf-idf 矢量化器一克范围 .现在我想获得前 1000 个单词
来自具有 的 sample_data最高 tf-idf 值 .谁能告诉我如何获得热门词?

from sklearn.feature_extraction.text import TfidfVectorizer
tf_idf_vect = TfidfVectorizer(ngram_range=(1,1))
tf_idf_vect.fit(sample_data)
final_tf_idf = tf_idf_vect.transform(sample_data)

最佳答案

TF-IDF 值取决于单个文档。您可以使用 max_features parameter of TfidfVectorizer 根据计数 (Tf) 获得前 1000 个术语:

max_features : int or None, default=None

If not None, build a vocabulary that only consider the top
max_features ordered by term frequency across the corpus.

做就是了:
tf_idf_vect = TfidfVectorizer(ngram_range=(1,1), max_features=1000)
您甚至可以获得 'idf' (全局术语权重)来自 tf_idf_vect使用 idf_ 拟合(学习)文档后属性:

idf_ : array, shape = [n_features], or None

  The learned idf vector (global term weights) when use_idf is set to True,  

调用 tf_idf_vect.fit(sample_data) 后执行此操作:
idf = tf_idf_vect.idf_
然后从中选择前 1000 个,并根据这些选定的特征重新拟合数据。
但是你不能通过“ tf-idf ”获得前 1000 名,因为 tf-idf 是 tf 的乘积idf 的单个文档中的术语(全局)词汇。因此,对于在单个文档中出现 2 次的同一个词,其 tf-idf 将是在另一个文档中只出现一次的同一个词的两倍。您如何比较同一术语的不同值。希望这可以说清楚。

关于python-3.x - 如何使用 TF-IDF 向量选择前 1000 个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655598/

相关文章:

python - TF-IDF 如何仅获取单词列表

python - 保留使用 TFIDF 制作的模型,以便使用 Scikit for Python 预测新内容

python - 获取嵌套字典的所有键

python - 我在运行 train.py 时遇到问题,我很困惑

python - sklearn OMP : Error #15 ("Initializing libiomp5md.dll, but found mk2iomp5md.dll already initialized.") when fitting models

numpy - y_test、sklearn 多标签分类上的 MultiLabelBinarizer 形状不一致错误

python - 是否可以在列表和元素中使用相同的切片符号来获得相同的结果?

python - UHF RFID 阅读器和 Python

python-3.x - 轴错误 : axis 1 is out of bounds for array of dimension 1 when calculating AUC

machine-learning - sklearn oneclass svm KeyError