clear
clc

%定义变量区
SamplePoint=5;%样本中每个传感器5000点里提取的点数
HCHO=[];%初始化甲醛样本集
NO=[];%初始化no2样本集
CO=[];%初始化co样本集
ress=[];%记录结果变量
aress=[];%记录结果变量
temp_label=[];%标签的暂存变量
count=0;
%%********************************************
%HCHO
%提取tgs2620的点
load HCHOalldata2620.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    HCHO=[HCHO;HCHOalldata2620(i,:)];
end
%提取tgs2602的点
load HCHOalldata2602.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    HCHO=[HCHO;HCHOalldata2602(i,:)];
end
%提取温湿度和浓度
HCHO=[HCHO;HCHOalldata2602(5001:5003,:)];


%NOX
%提取tgs2620的点
load NOX2620alldata.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    NO=[NO;NOX2620alldata(i,:)];
end
%提取tgs2602的点
load NOX2602alldata.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    NO=[NO;NOX2602alldata(i,:)];
end
%提取温湿度和浓度
NO=[NO;NOX2602alldata(5001:5003,:)];

%CO
%提取tgs2620的点
load co2620alldatamatrix.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    CO=[CO;co2620alldatamatrix(i,:)];
end
%提取tgs2602的点
load co2602alldatamatrix.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    CO=[CO;co2602alldatamatrix(i,:)];
end
%提取温湿度和浓度
CO=[CO;co2602alldatamatrix(5001:5003,:)];


%组合所有样本  5003*（100+113+96）
sa=[HCHO NO CO];

%标准化
[m n]=size(sa);
sa(1:m-3,:)=sa(1:m-3,:)/5000;
sa(m-2:m-1,:)=sa(m-2:m-1,:)/4095;
%由于是分类  替换成类别标签
a=[1 -1 -1]';%HCHO
b=[-1 1 -1]';%NO2
c=[-1 -1 1]';%CO
label=[a(:,ones(1,100)) b(:,ones(1,113)) c(:,ones(1,96))];
sa(m,:)=[];%浓度行置为空
sa=[sa;label];%将类别信息附上
[m n]=size(sa);
load('5折分组.mat')%导入随机分组好的5折
for tt=1:10   %交叉验证重复的次数
    for c=10   %参数C
        for l=3   %参数L
            L=10^l;
            C=10^c;
            Weight=rand(SamplePoint*2+2,L);%随机初始化ELM的W权值阵
            Bias1=rand(1,L);%随机初始化ELM的偏置
            ress=[];
            for cyclenum=1:5  %五折验证
                count=0;
                %截取样本******************************
                switch cyclenum
                    case 1
                        tr_label=[gl2,gl3,gl4,gl5];
                        te_label=gl1;
                        
                    case 2
                        tr_label=[gl1,gl3,gl4,gl5];
                        te_label=gl2;
                        
                    case 3
                        tr_label=[gl1,gl2,gl4,gl5];
                        te_label=gl3;
                        
                        
                    case 4
                        tr_label=[gl1,gl2,gl3,gl5];
                        te_label=gl4;
                        
                        
                    case 5
                        tr_label=[gl1,gl2,gl3,gl4];
                        te_label=gl5;
                                          
                end
                
                train_P=sa(1:m-3,tr_label);
                train_T=sa(m-2:m,tr_label);
                test_P=sa(1:m-3,te_label);
                test_T=sa(m-2:m,te_label);
                %ELM****************************************
                train_P=train_P';
                test_P=test_P';
                train_T=train_T';
                test_T=test_T';
                
                [N n]=size(train_P);
                %             Weight=rand(n,L);
                %             Bias1=rand(1,L);
                x=train_P*Weight+Bias1(ones(N*1,1),:);
                H=1./(1+exp(-x));
                % I=diag(ones(1,N));
                if L>=N
                    I=diag(ones(1,N));
                    Y=H'*inv(H*H'+I/C)*train_T;
                else
                    I=diag(ones(1,L));
                    Y=inv(H'*H+I/C)*H'*train_T;
                end
                ytrain=H*Y;
                [N1 n]=size(test_P);
                x=test_P*Weight+Bias1(ones(N1*1,1),:);
                H=1./(1+exp(-x));
                ytest=H*Y;
                
                
                 train_P=train_P';
                test_P=test_P';
                train_T=train_T';
                test_T=test_T';
                ytrain=ytrain';
                ytest=ytest';
                tr_l=[];
                ytr_l=[];
                te_l=[];
                yte_l=[];
                %计算出训练目标的标志
                for i=1:size(tr_label,2)
                    if train_T(1,i)==1
                        tr_l=[tr_l 1];
                    else
                        if train_T(2,i)==1
                            tr_l=[tr_l 2];
                        else
                            tr_l=[tr_l 3];
                        end
                    end
                end
                %计算出得出的训练结果的标志
                for i=1:size(tr_label,2)
                    if ytrain(1,i)>ytrain(2,i)
                        if ytrain(1,i)>ytrain(3,i)
                            ytr_l=[ytr_l 1];
                        else
                            ytr_l=[ytr_l 3];
                        end
                    else
                        if ytrain(2,i)>ytrain(3,i)
                            ytr_l=[ytr_l 2];
                        else
                            ytr_l=[ytr_l 3];
                        end
                    end
                end
                %计算出测试目标的标志
                for i=1:size(te_label,2)
                    if test_T(1,i)==1
                        te_l=[te_l 1];
                    else
                        if test_T(2,i)==1
                            te_l=[te_l 2];
                        else
                            te_l=[te_l 3];
                        end
                    end
                end
                %计算出得出的测试结果的标志
                for i=1:size(te_label,2)
                    if ytest(1,i)>ytest(2,i)
                        if ytest(1,i)>ytest(3,i)
                            yte_l=[yte_l 1];
                        else
                            yte_l=[yte_l 3];
                        end
                    else
                        if ytest(2,i)>ytest(3,i)
                            yte_l=[yte_l 2];
                        else
                            yte_l=[yte_l 3];
                        end
                    end
                end
                
                
            gas1=0;
            wr1=0;
            gas2=0;
            wr2=0;
            gas3=0;
            wr3=0;
            
            for i=1:size(te_label,2)
                switch te_l(i)
                    case 1
                         gas1=gas1+1;
                        if yte_l(i)~=1
                            wr1=wr1+1;
                        end
                    case 2
                         gas2=gas2+1;
                        if yte_l(i)~=2
                            wr2=wr2+1;
                        end
                        
                    case 3
                         gas3=gas3+1;
                        if yte_l(i)~=3
                            wr3=wr3+1;
                        end
                end
                    
            end
            ge1=(gas1-wr1)/gas1*100
            ge2=(gas2-wr2)/gas2*100
            ge3=(gas3-wr3)/gas3*100
                %保存某一折的误差（不断叠加）
                ress=[ress;cyclenum ge1 ge2 ge3];
            end
            aress=[aress;c l mean(ress(:,2)) mean(ress(:,3)) mean(ress(:,4))];
            
        end
    end
end
mean10=[mean(aress(:,3)) mean(aress(:,4)) mean(aress(:,5))]

