其他分享
首页 > 其他分享> > 基于粗糙集分类器的手写数字识别matlab实现

基于粗糙集分类器的手写数字识别matlab实现

作者:互联网

1.计算等价集和下近似集

load ('template.mat');
patternNum=75;%每个类别取75个样品
bottom=zeros(patternNum*2,1);
bottom(1:patternNum,1)=0;
bottom(patternNum+1:patternNum*2,1)=1;
x=[pattern(1).feature(1:patternNum,:);
  pattern(2).feature(1:patternNum,:)];%取75个第一类样品和75个第二类样品按列连接
x=[x bottom];%按行连接决策属性,构造出决策表
x=ceil(x);%特征二值化
ruleNum=patternNum*2;%决策表的行数,即样品总数
classX=zeros(ruleNum,ruleNum);%属性C的等价集矩阵(因为等价集最多为ruleNum个,且每个等价集最多有ruleNum个元素,定义一个ruleNum*ruleNum的全零矩阵用于存放等价集)
classNum=zeros(ruleNum,1);%定义一个ruleNum维的全零行向量用于存放各等价集中元素个数

%去重规则和不一致规则
for i=1:ruleNum-1
     if(i>ruleNum-1)
         break;
     end
     for j=i+1:ruleNum
         if(j>ruleNum)
            break;
         end
         while (x(i,1:25)==x(j,1:25))
              x(j,:)=[];
              ruleNum=ruleNum-1; %统计去重后的样品数量
              if(j>ruleNum)
                 break;
              end
         end
     end
end
x1=x;

%计算所有条件属性C的等价集(各条件属性值都相同的样品组成一个等价集,不考虑决策属性)
%%%%%%%%%%%%%%%%%%%% Begin %%%%%%%%%%%%%%%%%%%%
%去重后的决策表为x,行代表样品,列代表25维条件属性+1维决策属性

function[classX,m,classNum] = CalTiaoJian(num,classX,x,ruleNum,classNum);
    if(num ~= 0)
        x(1:ruleNum,num) = 0;
    end
    xNum = size(x,2);
    x(:,xNum) = 0;
    m=0;
    for i = 1:ruleNum
        if(x(i,xNum)~= 0)
            continue;
        else
            m = m+1;
            x(i,xNum) = m;
            n = 1;
            classX(m,n) = i;
        end
        for j = i+1:ruleNum
            if(x(j,xNum)~= 0)
                continue;
            end
            if(x(i,1:xNum-1)==x(j,1:xNum-1))
                x(j,xNum) = m;
                n = n+1;
                classX(m,n) = j;
            end
        end
        classNum(m) = n;
    end
end
m=0;
n=1;
num=0;
[classX,m,classNum]=CalTiaoJian(num,classX,x,ruleNum,classNum);
fprintf('%s\n',num2str(m)) %输出等价集数量m(需在循环过程中将等价集的数量存入变量m)

%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%

%计算决策属性D的等价集
x=x1;
x=x(1:ruleNum,:);
ruleNumY1=0;
for m=1:ruleNum
    if (x(m,26)==0)
       ruleNumY1=ruleNumY1+1; %找到决策属性为0的样品的最大编号
    end
end
ruleNumY2=ruleNum-ruleNumY1;
classY1=zeros(1,ruleNumY1);
classY2=zeros(1,ruleNumY2);
classY1=1:ruleNumY1; %决策属性等价集Y1,前ruleNumY1个样品
classY2=ruleNumY1+1:ruleNum; %决策属性等价集Y2,后ruleNum-ruleNumY1个样品

%计算决策属性等价集Y1、Y2关于所有条件属性C的下近似集C_Y1、C_Y2
%%%%%%%%%%%%%%%%%%%% Begin %%%%%%%%%%%%%%%%%%%%
C_Y1=[]; 
C_Y2=[];

%通过循环寻找属于U/C(U关于C的商集,也是C所有等价集的集合)的同时包含于classY1/2的样品集合,即为C_Y1/2
function[C_Y1,C_Y2] = CalXiaJinSi(classX,m,classNum,ruleNumY1);
    C_Y1 = [];
    C_Y2 = [];
    for i = 1:m
        Y1 = true;
        Y2 = true;
        for j = 1:classNum(i)
            if(classX(i,j) > ruleNumY1)
                Y1 = false;
            else
                Y2 = false;
            end
            if(~Y1&&~Y2)
                break;
            end
        end
        if(Y1)
            C_Y1 = [C_Y1 classX(i,1:classNum(i))];
        elseif(Y2)
            C_Y2 = [C_Y2 classX(i,1:classNum(i))];
        end
    end
end
[C_Y1,C_Y2]=CalXiaJinSi(classX,m,classNum,ruleNumY1);

fprintf('%s\n',num2str(C_Y1(1))) %输出C_Y1的第一个元素
fprintf('%s\n',num2str(C_Y2(2))) %输出C_Y2的第一个元素
%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%
    

2.基于粗糙集分类器的手写数字识别

function Cucaoji()
%通过循环计算多个二分类器
for i=1:10
     for j=1:i-1
         ruleStruct(i,j).rule=CuCao2ClassTrain(i,j);  %粗糙集两分类训练,见下方CuCao2ClassTrain()函数
     end
end
save ruleStruct ruleStruct;%保存规则表
img=imread('Untitled.bmp');%读取测试图像
feature=Getfeature(img);
num=zeros(1,10);
for i=1:10
   for j=1:i-1
       %两类判别
       G=CuCao2Class(i,j,feature,ruleStruct(i,j).rule);
       if(G==0)
          num(i)=num(i)+1;
       elseif(G==1)
          num(j)=num(j)+1;
       end
    end
end
[max_val,max_pos]=max(num);%取票数最高类别为识别结果
result=max_pos-1;
str = num2str(result);
str = ['应用粗糙集分类器的识别结果:' str];
fprintf('%s\n',str);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%函数名称:CuCao2ClassTrain()
%参数:class1:类别1;class2:类别2
%返回值:newRule:规则
%函数功能:粗糙集两类分类训练
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function newRule = CuCao2ClassTrain(class1,class2);
    load ('template.mat');
    patternNum=70;
    bottom=zeros(patternNum*2,1);
    bottom(1:patternNum,1)=0;
    bottom(patternNum+1:patternNum*2,1)=1;
    x=[pattern(class1).feature(1:patternNum,:);
        pattern(class2).feature(1:patternNum,:)];
    x=[x bottom];
    x=ceil(x);
    %一致性检测
    ruleNum=patternNum*2;
    %去重规则和不一致规则
    for m=1:ruleNum-1
        if(m>ruleNum-1)
            break;
        end
        for n=m+1:ruleNum
            if(n>ruleNum)
                break;
            end
            
            while (x(m,1:25)==x(n,1:25))
                x(n,:)=[];
                ruleNum=ruleNum-1;
                if(n>ruleNum)
                    break;
                end
            end
        end
    end
    
    x=x(1:ruleNum,:);
    ruleNumY1=0;
    for m=1:ruleNum
        if (x(m,26)==0)
            ruleNumY1=ruleNumY1+1;
        end
    end
    ruleNumY2=ruleNum-ruleNumY1;
    classX=zeros(ruleNum,ruleNum);%X属性等价集矩阵
    classNum=zeros(ruleNum,1);%等价集中元素个数
    m=0;%等价集个数
    n=1;
    %计算条件X等价集
    num=0;
    [classX,m,classNum]=CalTiaoJian(num,classX,x,ruleNum,classNum); %利用CalTiaoJian()函数计算条件属性C的等价集,该函数与第一关计算等价集的代码一致
    %决策D的等价集
    classY1=zeros(1,ruleNumY1);
    classY2=zeros(1,ruleNumY2);
    classY1=1:ruleNumY1;
    classY2=ruleNumY1+1:ruleNum;
    %决策D的下近似集
    C_Y1=zeros(1,ruleNumY1);
    C_Y2=zeros(1,ruleNumY2);
    [C_Y1,C_Y2]=CalXiaJinSi(classX,m,classNum,ruleNumY1);%利用CalXiaJinSi()函数计算决策D的下近似集,该函数与第一关计算下近似集的代码一致
    %%%%%%%%%%%%%%%%%%%% Begin %%%%%%%%%%%%%%%%%%%%
    %计算条件属性全集C的正域Pos(C,D)(C_Y1∪C_Y2)
 
    PosXD=[C_Y1,C_Y2];

    %依赖度r(C,D)(Pos(C,D)元素个数/论域中所有元素个数)

    rXD=size(PosXD,2)/(ruleNum);
    import=zeros(1,25);

    %%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%
    xReserve=[]; %约简后的条件属性
    %计算25个条件属性的重要度
    for i=1:25;
        %计算条件C-i的等价集
        classX(:,:)=0;
        [classX,m,classNum]=CalTiaoJian(i,classX,x,ruleNum,classNum);
        %计算决策D的下近似集
        [C_Y1,C_Y2]=CalXiaJinSi(classX,m,classNum,ruleNumY1);
        %%%%%%%%%%%%%%%%%%%% Begin %%%%%%%%%%%%%%%%%%%%
        %计算条件属性C-i的正域Pos(C-i,D)

        %依赖度r(C-i,D)(Pos(C-i,D)元素个数/论域中所有元素个数)

        PosXiD=[C_Y1,C_Y2];

        %计算条件属性i的重要度import(i)(r(C,D)-r(C-i,D))

        import(i)=size(PosXiD,2)/(ruleNum);

        %%%%%%%%%%%%%%%%%%%%% End %%%%%%%%%%%%%%%%%%%%%
        if(import(i)==0)
            if(Consistent(i,x,ruleNumY1,ruleNumY2)==1) %如果删除后一致
                x(1:ruleNum,i)=0;
            else
                xReserve=[xReserve i]; 
            end
        else
            xReserve=[xReserve i];
        end
    end
    %得到简化后的决策表
    xReserve=[xReserve 26];
    xNum=size(xReserve,2);
    xNew=zeros(ruleNum,xNum);
    for i=1:ruleNum
        for j=1:xNum
            xNew(i,j)=x(i,xReserve(j)); %构造属性约简后的决策表
        end
    end
    rule=[];
    %计算条件X等价集
    num=0;
    classX(:,:)=0;
    [classX,m,classNum]=CalTiaoJian(num,classX,xNew,ruleNum,classNum);
    %获取规则
    cf=[];
    for i=1:m
        temp=zeros(1,2);
        for j=1:classNum(i)
            if(size(find(classY1==classX(i,j)),2)~=0)
                temp(1,1)=1;
            end
            if(size(find(classY2==classX(i,j)),2)~=0)
                temp(1,2)=1;
            end
            if(temp(1,1)==1&&temp(1,2)==1)
                cf=[cf i];%记录cf不为1的等价集
                break;
            end
        end
    end
    temp=size(cf,2);
    if(temp~=0)%舍去cf不为1的规则
        for i=1:temp
            for j=1:classNum(cf(i))
                xNew(classX(cf(i),j),xNum)=2;
            end
        end
        for i=1:ruleNum
            while(xNew(i,xNum)==2)
                xNew(i,:)=[];
                ruleNum=ruleNum-1;
                if(i>ruleNum)
                    break;
                end
            end
            if(i>=ruleNum)
                break;
            end
        end
    end
    %简化规则表,去掉重复规则
    for m=1:ruleNum-1
        if(m>ruleNum-1)
            break;
        end
        for n=m+1:ruleNum
            if(n>ruleNum)
                break;
            end
            while(xNew(m,1:xNum)==xNew(n,1:xNum))
                xNew(n,:)=[];
                ruleNum=ruleNum-1;
                if(n>ruleNum)
                    break;
                end
            end
        end
    end
    
    ruleNumY1=0;
    for m=1:ruleNum
        if(xNew(m,xNum)==0)
            ruleNumY1=ruleNumY1+1;
        end
    end
    ruleNumY2=ruleNum-ruleNumY1;
    for m=1:ruleNum
        a=[];
        for j=1:xNum
            a=[a xNew(m,j)];
        end
        rule=[rule;a];
    end
    %规则化简
    oldRule=rule;
    newRule=[];
    ruleJ=[];%统计可化简的规则
    for i=1:size(xReserve,2)
        rule=oldRule;
        rule(:,i)=0;
        flag=0;
        for m=1:ruleNumY1-1
            for n=m+1:ruleNumY1
                if(rule(m,:)==rule(n,:))%可化简
                    rule(m,i)=inf;
                    newRule=[newRule;rule(m,:)];
                    flag=1;
                    ruleJ=[ruleJ m n];
                    break;
                end
            end
            if(flag)
                break;
            end
        end
        flag=0;
        for m=ruleNumY1+1:ruleNum-1
            for n=m+1:ruleNum
                if(rule(m,:)==rule(n,:))%可简化
                    rule(m,i)=inf;
                    newRule=[newRule;rule(m,:)];
                    flag=1;
                    ruleJ=[ruleJ m n];
                    break;
                end
            end
            if(flag)
                break;
            end
        end
    end
    
    for i=1:ruleNum
        b=size(find(ruleJ==i),2);
        if(b==0)%该规则不可约简
            newRule=[newRule;oldRule(i,:)];
        end
    end
    newRule=[newRule;xReserve];
        
    

标签:classNum,end,ruleNumY1,分类器,粗糙集,matlab,classX,Y1,ruleNum
来源: https://blog.csdn.net/m0_46384757/article/details/121265113