machine-learning - 使用 scikit-learn 对文本文档进行分类的交叉验证

标签 machine-learning scikit-learn cross-validation

在使用 scikit-learn 对文本文档进行分类时,您是否首先进行交叉验证,然后进行特征提取或其他方式?

这是我的管道:

union = FeatureUnion(
transformer_list = [
 ('tfidf', TfidfVectorizer()),
 ('featureEx', FeatureExtractor()),
 ('spell_chker', Spellingchecker()),
 ], n_jobs = -1)

我按照以下方式进行操作,但我想知道是否应该先提取特征并进行交叉验证。在此示例中,X 是文档列表,y 是标签。

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size= 0.2)

X_train = union.fit_transform(X_train)
X_test = union.transform(X_test)

ch2 = SelectKBest(f_classif, k = 7000)
X_train = ch2.fit_transform(X_train, y_train)
X_test  = ch2.transform(X_test)

clf = SVC(C=1, gamma=0.001, kernel = 'linear', probability=True).fit(
X_train , y_train)

print("classification report:")
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
print()

最佳答案

进行特征选择,然后对这些特征进行交叉验证有时在文本数据上很常见,但不太理想。这可能会导致过度拟合,并且交叉验证过程可能会高估您的真实准确性。

当您首先进行特征选择时,特征选择过程必须查看所有数据。交叉验证的目的是向其他人隐藏 1 倍。通过首先执行 FS,您会将一些数据知识泄漏到其他折叠中。

关于machine-learning - 使用 scikit-learn 对文本文档进行分类的交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722265/

相关文章:

numpy - 为什么SVM中支持向量的数量没有变化?

r - R-交叉验证错误处理- “dims product do not match the length of object”

python - 在 scikit-learn 中将 RandomizedSearchCV(或 GridSearcCV)与 LeaveOneGroupOut 交叉验证相结合

machine-learning - 神经网络模型架构每像素分类

machine-learning - 使用最少的图像数据设计分类器

python - 长时间运行的决策树模型

machine-learning - 为什么使用高斯过程回归的 GridCV 会出现错误?

python - 求解稀疏矩阵的线性回归方程

python - 使用 SelectKBest 按降序可视化特征选择

scikit-learn - sklearn 随机森林 : . oob_score_ 太低?