我想为LinearSVC估计器生成一个学习曲线,它使用countVectorizer来提取特征。countVectorizer还应用了一些特性选择步骤。
我可以做以下几点:
中的估计量
但我认为这会导致信息泄漏:基于所有数据的信息将被用于为学习曲线中使用的较小集合选择特征。
这是正确的吗?有没有一种方法可以在不泄露信息的情况下使用内置sklearn.model_selection.learning_curve()和countVectorizer?
谢谢!
发布于 2019-12-18 18:18:46
您需要与learning_curve
一起使用管道。管道在训练时调用变压器的fit_transform
,在测试时只调用transform
。learning_curve
还将应用可由参数cv
控制的交叉验证。
有了这条管道,就不会有信息泄露。这里是一个例子,使用一个集成的玩具库在科学工具包-学习。
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.svm import LinearSVC
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import learning_curve
categories = [
'alt.atheism',
'talk.religion.misc',
]
# Uncomment the following to do the analysis on all the categories
#categories = None
data = fetch_20newsgroups(subset='train', categories=categories)
pipeline = make_pipeline(
CountVectorizer(), TfidfTransformer(), LinearSVC()
)
learning_curve(pipeline, data.data, data.target, cv=5)
https://stackoverflow.com/questions/59397373
复制相似问题