Matlab 2022a 随机森林回归模型 R方评估
Matlab 2022a 随机森林回归模型 R方评估
在 Matlab 2022a 中,你可以使用 fitrensemble 函数来训练随机森林回归模型,并使用 perfcurve 函数来计算 R 方值。下面是修改后的代码示例:
% 步骤1:回归准备数据
res = xlsread('位移角性能点预测数据库1.1.xlsx');
% 步骤2:划分数据集
cv = cvpartition(size(res, 1), 'HoldOut', 0.2); % 80% 作为训练集和预测集,20% 作为测试集
trainingData = res(cv.training,:);
trainFeatures = trainingData(:, 1:11);
trainOutputs = trainingData(:, end-3:end);
testData = res(cv.test,:);
testFeatures = testData(:, 1:11);
testOutputs = testData(:, end-3:end);
trainIdx = training(cv);
testIdx = test(cv);
cv2 = cvpartition(sum(trainIdx), 'KFold', 5); % 交叉验证使用的折数
trainData = res(trainIdx, :);
testData = res(testIdx, :);
predictFeatures = double(res(:, 1:11));
% 步骤3:创建随机森林回归模型并进行交叉验证
NumTrees = 400;
nLeaf = 5;
TreeMdl1 = fitrensemble(trainFeatures, trainOutputs(:,1), 'Method', 'LSBoost', 'NumLearningCycles', NumTrees, 'Learners', 'tree', 'MinLeafSize', nLeaf);
TreeMdl2 = fitrensemble(trainFeatures, trainOutputs(:,2), 'Method', 'LSBoost', 'NumLearningCycles', NumTrees, 'Learners', 'tree', 'MinLeafSize', nLeaf);
TreeMdl3 = fitrensemble(trainFeatures, trainOutputs(:,3), 'Method', 'LSBoost', 'NumLearningCycles', NumTrees, 'Learners', 'tree', 'MinLeafSize', nLeaf);
TreeMdl4 = fitrensemble(trainFeatures, trainOutputs(:,4), 'Method', 'LSBoost', 'NumLearningCycles', NumTrees, 'Learners', 'tree', 'MinLeafSize', nLeaf);
% 步骤4:预测并评估模型
predictions1 = predict(TreeMdl1, testFeatures);
predictions2 = predict(TreeMdl2, testFeatures);
predictions3 = predict(TreeMdl3, testFeatures);
predictions4 = predict(TreeMdl4, testFeatures);
predictions = [predictions1, predictions2, predictions3, predictions4];
actual = testOutputs;
R2 = zeros(1, 4);
for i = 1:4
[~, ~, ~, R2(i)] = perfcurve(actual(:, i), predictions(:, i), 'ycrit', 'rsquared');
end
disp(R2);
在上述代码中,我们使用 fitrensemble 函数来训练随机森林回归模型。我们使用 LSBoost 方法,指定了树作为基学习器,并设置了树的最小叶节点大小为 nLeaf。然后,我们使用 predict 函数对测试集进行预测,并将预测结果存储在 predictions1 到 predictions4 中。
接下来,我们使用 perfcurve 函数计算 R 方值。在每个循环中,我们将实际输出值 actual 的对应列与预测值 predictions 的对应列传递给 perfcurve 函数,并设置 ycrit 参数为 'rsquared',以计算 R 方值。将计算得到的 R 方值存储在 R2 向量中。
最后,使用 disp 函数打印出计算得到的 R 方值。
请注意,这段代码假设 testOutputs 和 predictions 的维度都是 (N,4),其中 N 是测试样本的数量。如果实际情况不符,你可能需要根据数据的实际维度进行调整。
原文地址: https://www.cveoy.top/t/topic/QGG 著作权归作者所有。请勿转载和采集!