改写以下代码,使其能够实现“准确率 = (真阳性数 + 真阴性数)/ 总样本数”的功能,并分别计算四种故障类型诊断准确率,同时对应生成四张图:% 对测试数据进行测试 [testErr,testY_out] = nntest(nn, testXn, testY); % 转为实际类别 [~,testYreal_out] = max(testY_out'); testYreal_out = testYreal_out'; % 计算准确率 accTest = sum(testYreal_out==testYreal)/length(testYreal); figure plot(testYreal,'o','linewidth',1.5,'markersize',7) hold on plot(testYreal_out,'+','linewidth',1.5) title(['DBN测试集诊断准确率:' num2str(accTest*100) '%'],'fontsize',12) xlabel('样本','fontsize',12) ylabel('类型','fontsize',12) legend('实际类型','诊断类型') grid on set(gca,'ytick',1:6,'yticklabel',names) set(gca,'linewidth',1.5)
时间: 2023-08-06 07:27:29 浏览: 251
以下是改写后的代码:
```
% 对测试数据进行测试
[testErr,testY_out] = nntest(nn, testXn, testY);
% 转为实际类别
[~,testYreal_out] = max(testY_out');
testYreal_out = testYreal_out';
% 计算准确率
true_positive = sum(testYreal_out == 1 & testYreal == 1);
true_negative = sum(testYreal_out == 0 & testYreal == 0);
accuracy = (true_positive + true_negative) / length(testYreal_out);
% 计算四种故障类型诊断准确率
fault1_accuracy = sum(testYreal_out == 1 & testYreal == 1) / sum(testYreal == 1);
fault2_accuracy = sum(testYreal_out == 0 & testYreal == 0) / sum(testYreal == 0);
fault3_accuracy = sum(testYreal_out == 1 & testYreal == 0) / sum(testYreal == 0);
fault4_accuracy = sum(testYreal_out == 0 & testYreal == 1) / sum(testYreal == 1);
% 绘制图像
figure;
plot(testYreal,'o','linewidth',1.5,'markersize',7);
hold on;
plot(testYreal_out,'+','linewidth',1.5);
title(['DBN测试集诊断准确率:' num2str(accuracy*100) '%'],'fontsize',12);
xlabel('样本','fontsize',12);
ylabel('类型','fontsize',12);
legend('实际类型','诊断类型');
grid on;
set(gca,'ytick',1:6,'yticklabel',names);
set(gca,'linewidth',1.5);
% 绘制四种故障类型的诊断准确率图像
figure;
bar([fault1_accuracy fault2_accuracy fault3_accuracy fault4_accuracy]);
title('四种故障类型的诊断准确率','fontsize',12);
xlabel('故障类型','fontsize',12);
ylabel('准确率','fontsize',12);
set(gca,'xticklabel',{'故障1','故障2','故障3','故障4'});
set(gca,'ylim',[0 1]);
set(gca,'linewidth',1.5);
grid on;
```
其中,true_positive 表示真阳性数,true_negative 表示真阴性数,accuracy 表示准确率。fault1_accuracy、fault2_accuracy、fault3_accuracy 和 fault4_accuracy 分别表示四种故障类型的诊断准确率。第一个 figure 绘制测试数据实际类型和诊断类型的图像,第二个 figure 绘制四种故障类型的诊断准确率图像。
阅读全文