crossval函数是否会泄露数据 Whether the crossval function will leak data

3 visualizaciones (últimos 30 días)
鹏程
鹏程 el 25 de Abr. de 2025
Respondida: cdarling el 19 de Mayo de 2025
我先是在回归学习工具箱中训练好模型,然后将模型的代码导出,现在我使用原始数据集重新训练模型。partitionedModel = crossval(trainedModel.RegressionGP, 'KFold', 10); 在这个交叉验证的代码计算过程中,crossval函数是仅提取模型配置,如超参数的种类,还是会提取整个模型参数。如果是提取的整个参数,是不是意味着在每一轮的验证中,模型已经见过了所有的数据(包括验证集的数据),从而造成数据泄露,使得评估结果不能真实反映模型在未知数据上的泛化能力。
First, I trained the model in the regression learning toolbox, then exported the code of the model, and now I retrain the model using the original dataset. partitionedModel = crossval(trainedModel.RegressionGP, 'KFold', 10); In the code calculation process of this cross-validation, does the crossval function only extract the model configuration, such as the types of hyperparameters, or does it extract the entire model parameters? If it is the entire extracted parameter, does it mean that in each round of validation, the model has seen all the data (including the data in the validation set), thereby causing data leakage and making the evaluation results unable to truly reflect the model's generalization ability on unknown data?
predictorNames = {'AN1', 'VR1', 'ARE1', 'EF2', 'E12', 'ST_1', 'St_1', 'CRR_1', 'AT_1', 'At_1'};
predictors = p_train;
response = t_train;
% 训练回归模型
% 以下代码指定所有模型选项并训练模型。
regressionGP = fitrgp(...
predictors, ...
response, ...
'BasisFunction', 'constant', ...
'KernelFunction', 'exponential', ...
'Standardize', true);
% 使用 predict 函数创建结果结构体
predictorExtractionFcn = @(t) t;
gpPredictFcn = @(x) predict(regressionGP, x);
trainedModel.predictFcn = @(x) gpPredictFcn(predictorExtractionFcn(x));
% 向结果结构体中添加字段
trainedModel.RequiredVariables = {'AN1', 'VR1', 'ARE1', 'EF2', 'E12', 'ST_1', 'St_1', 'CRR_1', 'AT_1', 'At_1'};
trainedModel.RegressionGP = regressionGP;
trainedModel.About = '此结构体是从回归学习器 R2024b 导出的训练模型。';
trainedModel.HowToPredict = sprintf('要基于新表 T 进行预测,请使用: \n yfit = c.predictFcn(T) \n将 ''c'' 替换为此结构体的变量名,例如 ''trainedModel''。\n \n表 T 必须包含由以下属性返回的变量: \n c.RequiredVariables \n变量格式(例如矩阵/向量、数据类型)必须与原始训练数据匹配。\n忽略其他变量。\n \n有关详细信息,请参阅 <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appregression_exportmodeltoworkspace'')">How to predict using an exported model</a>。');
% 执行交叉验证
rng(1000);
partitionedModel = crossval(trainedModel.RegressionGP, 'KFold', 10); % crossval分区是使用 MATLAB 的全局随机数流,必须在前面额外增加一个随机数种子才能固定
% 计算验证预测
validationPredictions = kfoldPredict(partitionedModel);
% 计算验证 RMSE
validationRMSE = sqrt(kfoldLoss(partitionedModel, 'LossFun', 'mse'));

Respuestas (1)

cdarling
cdarling el 19 de Mayo de 2025
数据泄露指的是训练阶段用过的数据,是否在验证阶段再次使用
根据文档中crossval函数的示例,其中的函数是要包括训练过程的,就是说,对于10折交叉验证来说,这10次中的每一次,都要使用90%的数据进行训练,剩下10%的数据进行验证,并收集验证结果
因此,要保证不出现数据泄露,这里建议
1 函数要包括训练过程,如果函数中是训练好的模型,这里使用的90%的数据没有进行训练,那就没有起到验证的意义
2 这10%的数据与剩下90%的数据是否有重叠。如果在预处理阶段数据涉及相互之间的整合处理,可能这10%的数据中也会带有其他数据的信息,也会发生数据泄露

Productos


Versión

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!