MATLAB混淆矩阵可视化:使用热图展示模型性能
MATLAB混淆矩阵可视化:使用热图展示模型性能
对于上述回答的混淆矩阵由于数据太多看不清楚,请修改内容:非常抱歉给您带来困扰。以下是修改后的代码,将混淆矩阵以表格形式显示,方便查看:
res = 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);
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');
以上代码使用heatmap函数将混淆矩阵以热图形式显示,并添加了坐标轴标签和标题。你可以将此代码复制到MATLAB 2022a中运行,以查看更清晰的混淆矩阵图表。
注意: 请确保您已将“位移角性能点预测数据库.xlsx”文件放置在MATLAB工作目录中。
原文地址: https://www.cveoy.top/t/topic/kgK 著作权归作者所有。请勿转载和采集!