帮我改写代码,使四种故障类型能够分别计算准确率并显示在四张图中:% 计算准确率 accTrain = sum(trainYreal_out==trainYreal)/length(trainYreal); figure plot(trainYreal,'o','linewidth',1.5,'markersize',7) hold on plot(trainYreal_out,'+','linewidth',1.5) title(['DBN训练集诊断准确率:' num2str(accTrain*100) '%'],'fontsize',12) xlabel('样本','fontsize',12) ylabel('类型','fontsize',12) legend('实际类型','诊断类型') grid on set(gca,'ytick',1:6,'yticklabel',names) set(gca,'linewidth',1.5) % 对测试数据进行测试 [testErr,testY_out] = nntest(nn, testXn, testY);
时间: 2023-07-16 14:13:04 浏览: 102
以下是修改后的代码,可以分别计算四种故障类型的准确率并在四张图中显示:
```
% 计算四种故障类型的准确率
accTrain1 = sum(trainYreal_out==trainYreal & trainYreal==1)/sum(trainYreal==1);
accTrain2 = sum(trainYreal_out==trainYreal & trainYreal==2)/sum(trainYreal==2);
accTrain3 = sum(trainYreal_out==trainYreal & trainYreal==3)/sum(trainYreal==3);
accTrain4 = sum(trainYreal_out==trainYreal & trainYreal==4)/sum(trainYreal==4);
% 绘制第一张图
figure
subplot(2,2,1)
plot(find(trainYreal==1),trainYreal(trainYreal==1),'o','linewidth',1.5,'markersize',7)
hold on
plot(find(trainYreal_out==1),trainYreal_out(trainYreal_out==1),'+','linewidth',1.5)
title(['故障类型1在训练集中的诊断准确率:' num2str(accTrain1*100) '%'],'fontsize',12)
xlabel('样本','fontsize',12)
ylabel('类型','fontsize',12)
legend('实际类型','诊断类型')
grid on
set(gca,'ytick',1:4,'yticklabel',{'1','2','3','4'})
set(gca,'linewidth',1.5)
% 绘制第二张图
subplot(2,2,2)
plot(find(trainYreal==2),trainYreal(trainYreal==2),'o','linewidth',1.5,'markersize',7)
hold on
plot(find(trainYreal_out==2),trainYreal_out(trainYreal_out==2),'+','linewidth',1.5)
title(['故障类型2在训练集中的诊断准确率:' num2str(accTrain2*100) '%'],'fontsize',12)
xlabel('样本','fontsize',12)
ylabel('类型','fontsize',12)
legend('实际类型','诊断类型')
grid on
set(gca,'ytick',1:4,'yticklabel',{'1','2','3','4'})
set(gca,'linewidth',1.5)
% 绘制第三张图
subplot(2,2,3)
plot(find(trainYreal==3),trainYreal(trainYreal==3),'o','linewidth',1.5,'markersize',7)
hold on
plot(find(trainYreal_out==3),trainYreal_out(trainYreal_out==3),'+','linewidth',1.5)
title(['故障类型3在训练集中的诊断准确率:' num2str(accTrain3*100) '%'],'fontsize',12)
xlabel('样本','fontsize',12)
ylabel('类型','fontsize',12)
legend('实际类型','诊断类型')
grid on
set(gca,'ytick',1:4,'yticklabel',{'1','2','3','4'})
set(gca,'linewidth',1.5)
% 绘制第四张图
subplot(2,2,4)
plot(find(trainYreal==4),trainYreal(trainYreal==4),'o','linewidth',1.5,'markersize',7)
hold on
plot(find(trainYreal_out==4),trainYreal_out(trainYreal_out==4),'+','linewidth',1.5)
title(['故障类型4在训练集中的诊断准确率:' num2str(accTrain4*100) '%'],'fontsize',12)
xlabel('样本','fontsize',12)
ylabel('类型','fontsize',12)
legend('实际类型','诊断类型')
grid on
set(gca,'ytick',1:4,'yticklabel',{'1','2','3','4'})
set(gca,'linewidth',1.5)
% 对测试数据进行测试
[testErr,testY_out] = nntest(nn, testXn, testY);
```
阅读全文