clear;
clc;
tic;
zhen=6;
fu=10;
load('Sag.mat')
Y(1,1:zhen)=1;
Y(1,zhen+1:zhen+fu)=0;
%初始化权重,按英文上的来的
W=zeros(1,zhen+fu);
for i=1:size(Y,2)
if (Y(i))
W(i)=1/(2*zhen);
else
W(i)=1/(2*fu);
end
end
%T轮训练
a=1;
while(a<=200)
%标准化权重
NW=sum(W);
W=W/NW;
num=0;
minerr=1;
for j=1:size(F,2)
%取出一列
f=F(:,j)';
[f_sort, I] = sort(f);%排序并记下标记
W_sort = W(I);%排序后权重,同时更新
Y_sort = Y(I);%排序后标签
Tz= Y_sort * W_sort'; %正样本权重和,T+
Sz= cumsum( Y_sort .* W_sort);%当前样本之前的正样本权重和
Tf= (1-Y_sort) * W_sort';%负样本权重和,T-
Sf= cumsum( (1-Y_sort) .* W_sort);%当前样本之前的负样本权重和
PN_cum1 = ( (Tf-Sf) + Sz); %公式e = min S+ + (T ? ? S?), S? + (T + ? S+)
PN_cum2 = ( (Tz-Sz) + Sf);
[min1,thresh_I1]= min(PN_cum1);
[min2,thresh_I2]= min(PN_cum2);
if (min1<min2)
thresh_I=thresh_I1;
PN_cum=PN_cum1;
else
thresh_I=thresh_I2;
PN_cum=PN_cum2;
end
thresh = ( f_sort(thresh_I));
p = 2 *( (Sz(thresh_I)>Sf(thresh_I)) -0.5);%判断方向
Result=(p*f<p*thresh);%直接f<thresh
error=sum(abs(Result-Y).*W);%每一列的错误率
if(error<minerr)
minerr=error;
thresht=thresh;
pt=p;
It=j;
Resultt=Result;
num=num+1;
end
end
beta=minerr/(1-minerr);
W(find(Y==Resultt))=W(find(Y==Resultt))*beta;%分类正确就更新权重
if(a==1)
P(1)=pt;%记录方向
Thresh(1)=thresht;%记录阈值
Err(1)=minerr;%记录错误率
alpha(1)=log(1/beta);%记录贝塔t
tz(1)=It;
a=a+1;
end
if(a>1 && num~=0)
if(tz(a-1)~=It)
P(a)=pt;%记录方向
Thresh(a)=thresht;%记录阈值
Err(a)=minerr;%记录错误率
alpha(a)=log(1/beta);%记录贝塔t
tz(a)=It;
a=a+1;
end
end
end
save('threshSag.mat','Thresh')
save('pSag.mat','P')
save('tzSag.mat','tz')
save('alphaSag.mat','alpha')
save('ErrSag.mat','Err')
toc
clc;
tic;
zhen=6;
fu=10;
load('Sag.mat')
Y(1,1:zhen)=1;
Y(1,zhen+1:zhen+fu)=0;
%初始化权重,按英文上的来的
W=zeros(1,zhen+fu);
for i=1:size(Y,2)
if (Y(i))
W(i)=1/(2*zhen);
else
W(i)=1/(2*fu);
end
end
%T轮训练
a=1;
while(a<=200)
%标准化权重
NW=sum(W);
W=W/NW;
num=0;
minerr=1;
for j=1:size(F,2)
%取出一列
f=F(:,j)';
[f_sort, I] = sort(f);%排序并记下标记
W_sort = W(I);%排序后权重,同时更新
Y_sort = Y(I);%排序后标签
Tz= Y_sort * W_sort'; %正样本权重和,T+
Sz= cumsum( Y_sort .* W_sort);%当前样本之前的正样本权重和
Tf= (1-Y_sort) * W_sort';%负样本权重和,T-
Sf= cumsum( (1-Y_sort) .* W_sort);%当前样本之前的负样本权重和
PN_cum1 = ( (Tf-Sf) + Sz); %公式e = min S+ + (T ? ? S?), S? + (T + ? S+)
PN_cum2 = ( (Tz-Sz) + Sf);
[min1,thresh_I1]= min(PN_cum1);
[min2,thresh_I2]= min(PN_cum2);
if (min1<min2)
thresh_I=thresh_I1;
PN_cum=PN_cum1;
else
thresh_I=thresh_I2;
PN_cum=PN_cum2;
end
thresh = ( f_sort(thresh_I));
p = 2 *( (Sz(thresh_I)>Sf(thresh_I)) -0.5);%判断方向
Result=(p*f<p*thresh);%直接f<thresh
error=sum(abs(Result-Y).*W);%每一列的错误率
if(error<minerr)
minerr=error;
thresht=thresh;
pt=p;
It=j;
Resultt=Result;
num=num+1;
end
end
beta=minerr/(1-minerr);
W(find(Y==Resultt))=W(find(Y==Resultt))*beta;%分类正确就更新权重
if(a==1)
P(1)=pt;%记录方向
Thresh(1)=thresht;%记录阈值
Err(1)=minerr;%记录错误率
alpha(1)=log(1/beta);%记录贝塔t
tz(1)=It;
a=a+1;
end
if(a>1 && num~=0)
if(tz(a-1)~=It)
P(a)=pt;%记录方向
Thresh(a)=thresht;%记录阈值
Err(a)=minerr;%记录错误率
alpha(a)=log(1/beta);%记录贝塔t
tz(a)=It;
a=a+1;
end
end
end
save('threshSag.mat','Thresh')
save('pSag.mat','P')
save('tzSag.mat','tz')
save('alphaSag.mat','alpha')
save('ErrSag.mat','Err')
toc