使用均方根误差评估回归模型性能
使用均方根误差评估回归模型性能
在机器学习中,评估模型的性能至关重要。对于回归模型,均方根误差 (RMSE) 是一种常用的评估指标,用于衡量模型预测值与实际值之间的差异。
什么是均方根误差 (RMSE)?
RMSE 是预测值与实际值之间平方误差的平方根的平均值。它表示模型预测值与实际值之间的平均偏差。RMSE 值越低,模型的预测性能越好。
计算 RMSE
RMSE 的计算公式如下:
RMSE = sqrt(mean((y_true - y_pred)**2))
其中:
y_true是实际值*y_pred是预测值
Matlab 代码示例
以下 Matlab 代码演示了如何计算回归模型的 RMSE:matlabres = xlsread('位移角性能点预测数据库.xlsx');
cv = cvpartition(size(res, 1), 'HoldOut', 0.2);trainingData = res(cv.training,:);trainFeatures = trainingData(:, 1:11);trainOutputs = trainingData(:, end-6:end-3);testData = res(cv.test,:);testFeatures = testData(:, 1:11);testOutputs = testData(:, end-6:end);trainIdx = training(cv);testIdx = test(cv);
cv2 = cvpartition(sum(trainIdx), 'KFold', 5);
trainData = res(trainIdx, :);testData = res(testIdx, :);
trainFeatures = trainData(:, 1:11);trainOutputs = trainData(:, end-6:end-3);testFeatures = testData(:, 1:11);testOutputs = testData(:, end-6:end);
predictFeatures = double(res(:, 1:11));
NumTrees = 400;nLeaf=5;TreeMdl1 = TreeBagger(NumTrees, trainFeatures, trainOutputs(:,1), 'oobprediction', 'on', 'method', 'regression','minleaf', nLeaf); TreeMdl2 = TreeBagger(NumTrees, trainFeatures, trainOutputs(:,2), 'oobprediction', 'on', 'method', 'regression','minleaf', nLeaf); TreeMdl3 = TreeBagger(NumTrees, trainFeatures, trainOutputs(:,3), 'oobprediction', 'on', 'method', 'regression','minleaf', nLeaf); TreeMdl4 = TreeBagger(NumTrees, trainFeatures, trainOutputs(:,4), 'oobprediction', 'on', 'method', 'regression','minleaf', nLeaf);
SeitaY = trainOutputs(:, end-3);SeitaP = trainOutputs(:, end-3) + trainOutputs(:, end-2);SeitaU = trainOutputs(:, end-3) + trainOutputs(:, end-2) + trainOutputs(:, end-1);SeitaR = trainOutputs(:, end-3) + trainOutputs(:, end-2) + trainOutputs(:, end-1) + trainOutputs(:, end);outputMatrix = cat(2, SeitaY, SeitaP, SeitaU, SeitaR);
predictions1 = predict(TreeMdl1, testFeatures); predictions2 = predict(TreeMdl2, testFeatures); predictions3 = predict(TreeMdl3, testFeatures); predictions4 = predict(TreeMdl4, testFeatures); predictions=[predictions1,predictions2,predictions3,predictions4];predictionsY = predictions(:, end-3);predictionsP = predictions(:, end-3) + predictions(:, end-2);predictionsU = predictions(:, end-3) + predictions(:, end-2) + predictions(:, end-1);predictionsR = predictions(:, end-3) + predictions(:, end-2) + predictions(:, end-1) + predictions(:, end);
confusionMatrixY = confusionmat(testOutputs(:, end-3), predictionsY);confusionMatrixP = confusionmat(testOutputs(:, end-3) + testOutputs(:, end-2), predictionsP);confusionMatrixU = confusionmat(testOutputs(:, end-3) + testOutputs(:, end-2) + testOutputs(:, end-1), predictionsU);confusionMatrixR = confusionmat(testOutputs(:, end-3) + testOutputs(:, end-2) + testOutputs(:, end-1) + testOutputs(:, end), predictionsR);
% 计算均方根误差 (RMSE)rmseY = sqrt(mean((testOutputs(:, end-3) - predictionsY).^2));rmseP = sqrt(mean((testOutputs(:, end-3) - predictionsP).^2));rmseU = sqrt(mean((testOutputs(:, end-3) - predictionsU).^2));rmseR = sqrt(mean((testOutputs(:, end-3) - predictionsR).^2));
disp(['RMSE for Y: ', num2str(rmseY)]);disp(['RMSE for P: ', num2str(rmseP)]);disp(['RMSE for U: ', num2str(rmseU)]);disp(['RMSE for R: ', num2str(rmseR)]);
figure;subplot(2,2,1);heatmap(confusionMatrixY, 'Colormap', 'gray', 'ColorbarVisible', 'off', 'FontSize', 12);title('Confusion Matrix Y');xlabel('Predicted');ylabel('True');
subplot(2,2,2);heatmap(confusionMatrixP, 'Colormap', 'gray', 'ColorbarVisible', 'off', 'FontSize', 12);title('Confusion Matrix P');xlabel('Predicted');ylabel('True');
subplot(2,2,3);heatmap(confusionMatrixU, 'Colormap', 'gray', 'ColorbarVisible', 'off', 'FontSize', 12);title('Confusion Matrix U');xlabel('Predicted');ylabel('True');
subplot(2,2,4);heatmap(confusionMatrixR, 'Colormap', 'gray', 'ColorbarVisible', 'off', 'FontSize', 12);title('Confusion Matrix R');xlabel('Predicted');ylabel('True');
该代码片段展示了如何加载数据、训练回归模型、进行预测并计算 RMSE。通过分析 RMSE 值,可以评估模型的预测性能,并与其他模型进行比较。
原文地址: https://www.cveoy.top/t/topic/kWs 著作权归作者所有。请勿转载和采集!