改写以下代码,使其能够实现“准确率 = (真阳性数 + 真阴性数)/ 总样本数”的功能,并分别计算四种故障类型诊断准确率生成四张图:% 对测试数据进行测试 [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)
时间: 2024-01-08 13:02:51 浏览: 53
以下是改写后的代码,实现“准确率 = (真阳性数 + 真阴性数)/ 总样本数”的功能,并分别计算四种故障类型诊断准确率生成四张图:
% 对测试数据进行测试
[testErr, testY_out] = nntest(nn, testXn, testY);
% 转为实际类别
[~, testYreal_out] = max(testY_out');
testYreal_out = testYreal_out';
% 计算准确率
true_positive = sum((testYreal_out == 1) & (testYreal == 1));
false_positive = sum((testYreal_out == 1) & (testYreal == 0));
true_negative = sum((testYreal_out == 0) & (testYreal == 0));
false_negative = sum((testYreal_out == 0) & (testYreal == 1));
accuracy = (true_positive + true_negative) / length(testYreal);
% 计算四种故障类型的诊断准确率
accuracy1 = true_positive / sum(testYreal == 1);
accuracy2 = true_negative / sum(testYreal == 0);
accuracy3 = true_negative / sum(testYreal == 1);
accuracy4 = true_positive / sum(testYreal == 0);
% 绘制图形
figure
subplot(2,2,1)
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)
subplot(2,2,2)
bar([accuracy1 accuracy2])
ylim([0 1])
title('故障类型1和0的诊断准确率','fontsize',12)
set(gca,'xticklabel',{'故障类型1','故障类型0'})
ylabel('准确率','fontsize',12)
set(gca,'linewidth',1.5)
subplot(2,2,3)
bar([accuracy3 accuracy4])
ylim([0 1])
title('故障类型1和0的误诊率','fontsize',12)
set(gca,'xticklabel',{'故障类型1','故障类型0'})
ylabel('误诊率','fontsize',12)
set(gca,'linewidth',1.5)
subplot(2,2,4)
pie([true_positive true_negative false_positive false_negative])
title('四种故障类型的诊断结果','fontsize',12)
legend('真阳性','真阴性','假阳性','假阴性')
set(gca,'linewidth',1.5)
阅读全文