kpcak.asv
资源名称:KPCA.rar [点击查看]
上传用户:nxf103
上传日期:2017-12-04
资源大小:2690k
文件大小:2k
源码类别:
能源行业(电力石油煤炭)
开发平台:
Matlab
- function [ctni,CSPE]=kpcak(test,train,aa,ss,ks)
- %输入的训练数据和测试数据的每一行代表一个样本
- %得出训练数据和测试数据所包含的样本数
- %我们假设径向基核函数的度为1,即分母为2
- n1=size(train,1);
- n2=size(test,1);
- n3=size(train,2);
- %计算A的大小
- A=0;
- for i=1:n1
- for j=1:n1
- A=A+exp(-norm(train(i,:)-train(j,:))^2/ks);
- end
- end
- A=(1/n1^2)*A;
- %计算ap的大小
- ap=zeros(1,n1);
- for i=1:n1
- for j=1:n1
- ap(i)=ap(i)+exp(-norm(train(i,:)-train(j,:))^2/ks);
- end
- ap(i)=(1/n1)*ap(i);
- end
- %计算对原始输入变量的第k采样做偏导
- ctni=zeros(n2,n3);
- CSPE=zeros(n2,n3);
- add_ctni=zeros(n2,1);
- add_CSPE=zeros(n2,1);
- for new=1:n2
- kv=zeros(n3,1);
- for i=1:n3
- %第i个偏导的矩阵记为KKPQ
- KKPQ=zeros(n1);
- for p=1:n1
- for q=1:n1
- KKPQ(p,q)=-1/ks*...
- (((train(p,i)-test(new,i))^2+(train(q,i)-test(new,i))^2)*onekpca(train(p,:),test(new,:))*onekpca(train(q,:),test(new,:))...
- +(A-ap(q))*(train(p,i)-test(new,i))^2*onekpca(train(p,:),test(new,:))...
- +(A-ap(p))*(train(q,i)-test(new,i))^2*onekpca(train(q,:),test(new,:))...
- -twokpca(test,train,i,p,new)-twokpca(test,train,i,q,new)...
- +(1/n1)*(ap(p)+ap(q)-2*A)*threekpca(test,train,new,i)...
- +fourkpca(test,train,i,new));
- end
- kv(i)=kv(i)+(train(p,i)-test(new,i))^2*exp(-norm(train(p,:)-test(new,:))^2/ks);
- end
- %计算第new个测试值的第i个观测变量的偏导T平方值ctni
- ctni(new,i)=abs(trace(aa'*KKPQ*aa*ss));
- CSPE(new,i)=abs(1/ks*(-2/(ks*n1)*kv(i)+trace(aa'*KKPQ*aa)));
- add_ctni(new)=add_ctni(new)+ctni(new,i);
- add_CSPE(new)=add_CSPE(new)+CSPE(new,i);
- end
- for i=1:n3
- ctni(new,i)=ctni(new,i)/add_ctni(new);
- CSPE(new,i)=CSPE(new,i)/add_CSPE(new);
- end
- end