【问题标题】:Should tfidf be generated with train and test sets combined?tfidf 是否应该结合训练集和测试集生成?
【发布时间】:2020-05-13 22:38:40
【问题描述】:

https://medium.com/machine-learning-intuition/document-classification-part-3-detection-algorithm-support-vector-machines-gradient-descent-282316b0838e

在上面的例子中,tfidf 是为训练和测试语料分别生成的。它不应该一起生成吗,因为单独处理训练和测试与一起处理时 idf 会不一样?谢谢。

# Vectorize the training data
X_train = vectorizer.fit_transform(train_corpus)

# Vectorize the testing data
X_test = vectorizer.transform(test_corpus)

【问题讨论】:

    标签: scikit-learn classification tf-idf


    【解决方案1】:

    您不能使用测试来创建 TF-IDF 模型,因为您不能对测试集进行任何假设。

    尽管如此,您仍然需要一种方法将测试集中的单词表示为数字。这就是为什么您必须在训练集 (vectorizer.fit_transform(train_corpus)) 上进行训练,但只对测试集 (vectorizer.transform(test_corpus)) 进行转换(无需训练)。

    【讨论】:

    • 因此,如果目的是确定在这两个集合上训练以对看不见的数据进行预测的最佳参数是什么,那么这两个集合仍然应该像示例中那样分开使用吗?谢谢。
    • 另外,如果我有一组包含标记数据和未标记数据的数据怎么办。目的是使用标记数据对未标记数据进行预测。似乎使用标记数据和未标记数据创建 tfidf 应该没有问题。然后可以将 tfidf 拆分为训练和测试,以学习 SVM 分类的最佳参数。
    猜你喜欢
    • 2019-12-09
    • 2018-10-10
    • 2017-11-01
    • 2018-02-01
    • 2015-01-17
    • 2013-06-18
    • 2019-08-15
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多