python - 在整个数据集上计算 TF-IDF 还是仅在训练数据上计算 TF-IDF?

标签 python machine-learning scikit-learn nlp tf-idf

在本书《TensorFlow Machine Learning Cookbook》的第七章中,作者在预处理数据时使用了scikit-learn的fit_transform函数来获取tfidf特征用于训练的文本。作者将所有文本数据提供给函数,然后将其分为训练和测试。这是一个真实的操作,还是我们必须先分离数据,然后在训练中执行 fit_transform 并在测试中执行 transform

最佳答案

根据 scikit-learn 的文档,使用 fit() 是为了

Learn vocabulary and idf from training set.

另一方面,使用 fit_transform() 是为了

Learn vocabulary and idf, return term-document matrix.

同时变换()

Transforms documents to document-term matrix.

在训练集上,您需要应用 fit()transform() (或者只是 fit_transform() ,本质上将两者结合起来操作),但是,在测试集上,您只需要 transform() 测试实例(即文档)。

请记住,训练集用于学习目的(学习是通过 fit() 实现的),而测试集用于评估训练后的模型是否可以很好地推广到新的未见过的模型数据点。

<小时/>

更多详情可以引用文章fit() vs transform() vs fit_transform()

关于python - 在整个数据集上计算 TF-IDF 还是仅在训练数据上计算 TF-IDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47778403/

相关文章:

machine-learning - 停止机器学习训练,但使用 Vowpal Wabbit 保持模型的当前状态

python - 将 Pandas 'categorical' dtype 与 sklearn 一起使用

python - 如何在 django oscar 中设置 Paypal 重定向 url?

python - Python 中的嵌套循环

python - 我想将数据帧分成带有范围的训练集和测试集

machine-learning - keras.models.Model.fit 中的 "epoch"是什么?

python - 错误 : All estimators should implement fit and transform, 或者可以是 'drop' 或 'passthrough' 说明符。 StandardScaler() 没有

python - 将 scikit-learn 向量化器和词汇表与 gensim 一起使用

python - 推特/社交数据挖掘——Ruby 还是 Django?

python - 是否有一种基于索引将列表分成两部分的首选方法