赞
踩
- function [outputArg1,outputArg2] = GD_AE_OD(inputArg1,inputArg2)
- %SIMPLEBP_MORENEURONMOREHIDDEN 此处显示有关此函数的摘要
- % 此处显示详细说明
- %通过计算待检测数据集的重构误差来计算样本的离群值,越大越有可能是离群点。
- x=load('Normalization_wbc.txt')';%每一列是一个样本
- y=load('Normalization_wbc.txt')';
- Label=load('Label_wbc.txt');
- ADLabels=load('Label_wbc.txt');
- [m,n]=size(x);%m表示有多少个特征,n表示有多少个样本
-
- %初始化
- Layer2_hiddensize=5;%第一个隐藏层的神经元个数
- Layer3_hiddensize=m;%第二个隐藏层的神经元个数
- Layer2_w=rand(Layer2_hiddensize,m);%rand(m,n)表示随机生成m行n列的0-1之间的矩阵,如果rand(m)表示生成m*m的方阵的随机矩阵
- Layer2_b=rand(Layer2_hiddensize,1);
- Layer3_w=rand(Layer3_hiddensize,Layer2_hiddensize);
- Layer3_b=rand(Layer3_hiddensize,1);
- %初始化隐藏层的输出
- Layer2_output=rand(Layer2_hiddensize,1);
- Layer3_output=rand(Layer3_hiddensize,1);
- %初始化隐藏层的梯度
- Layer2_e=rand(Layer2_hiddensize,1);
- Layer3_e=rand(Layer3_hiddensize,1);
-
- iteration=1000;%迭代次数
- LearningRate=0.1;
- Abnormal_number=20;%异常点个数
- for t=1:iteration
- %%%%%%%%%%%标准正向传播%%%%%%%%
- for i=1:n
- %%%%%正传%%%%%%%%%%%%
- Layer2_output = Sigmoid( Layer2_w * x(:,i) - Layer2_b);
- total_Layer2_output(:,i)=Layer2_output;
- Layer3_output = Sigmoid( Layer3_w * Layer2_output - Layer3_b);
- x(:,i)=Layer3_output;
- %%%%%%%反传%%%%%%%%%%%
- Layer3_e=Layer3_output .* (1-Layer3_output) .* (y(:,i)-Layer3_output);
- Layer2_e=Layer2_output .* (1-Layer2_output) .* (Layer3_w' * Layer3_e);
- Layer3_w = Layer3_w + LearningRate * Layer3_e * Layer2_output';
- Layer2_w = Layer2_w + LearningRate * Layer2_e * x(:,i)';
- Layer3_b = Layer3_b - LearningRate * Layer3_e;
- Layer2_b = Layer2_b - LearningRate * Layer2_e;
- end
- Loss=(y-x).*(y-x);%loss中每一列表示对应列的样本的损失值
- EverySample_Loss=sum(Loss,1);
- TotalLoss(t,:)=sum(EverySample_Loss)/n;
- end
- mse=sum(Loss,1)';
- auc = Measure_AUC(mse, ADLabels);
- disp(auc)
- [OF_value,index_number]=sort(mse);
- ODA_AbnormalObject_Number=index_number(n-Abnormal_number+1:end,:);%outlier detection algorithm 算法认定的异常对象的编号
- ODA_NormalObject_Number=index_number(1:n-Abnormal_number,:);%outlier detection algorithm算法认定的正常对象的编号
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%算法实际的检测率/准确率/误报率等评价指标的计算%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%Real_NormalObject_Number表示数据集中真正的正常对象的编号,Real_AbnormalObject_Number表示数据集中真正异常对象的编号
- [Real_NormalObject_Number,Real_Normal]=find(Label==0);
- [Real_AbnormalObject_Number,Real_Abnormal]=find(Label==1);
-
- %正例是异常对象,反例是正常对象
- TP=length(intersect(Real_AbnormalObject_Number,ODA_AbnormalObject_Number));
- FP=length(Real_AbnormalObject_Number)-TP;
- TN=length(intersect(Real_NormalObject_Number,ODA_NormalObject_Number));
- FN=length(Real_NormalObject_Number)-TN;
-
- %准确率
- ACC=(TP+TN)/(TP+TN+FP+FN);
- fprintf('准确率ACC= %8.5f\n',ACC*100)
- %检测率==查全率=R
- DR=TP/(TP+FN);
- fprintf('检测率DR= %8.5f\n',DR*100)
- %查准率P
- P=TP/(TP+FP);
- fprintf('查准率P= %8.5f\n',P*100)
- %误报率
- FAR=FP/(TN+FP);
- fprintf('误报率FAR= %8.5f\n',FAR*100)
-
- %绘制混淆矩阵
- Confusion_matrix=[TP,FN;FP,TN];
- Figure_Confusion_matrix=heatmap(Confusion_matrix);
-
-
- figure(2)
- for j=0:iteration-1
- j=j+1;
- axis_x(j,:)=j;
- end
- plot(axis_x,TotalLoss,'LineWidth',2);
-
- end
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。