资源简介

以minstall数据库为例,介绍了详细应用例程。

资源截图

代码片段和文件信息

% 遗传算法 特征选择 分类测试
clear all
% 加载数据
tic
load mnistAll
train_images = mnist.train_images;
train_labels = mnist.train_labels;
test_images = mnist.test_images;
test_labels = mnist.test_labels;

MN = 28*28;

N = 1000;
TN = 1000;

Train_labels = train_labels(1:N);
Train_images = train_images(::1:N);
Train_images = double(reshape(Train_imagesMNN));

Test_images = double(reshape(test_images(::1:TN)MNTN));
Test_labels = test_labels(1:TN);

class_label = [0:9];

[mn] = size(Train_images);

Len = length(Train_labels);  %样本数目
% ga初始化
Iter = 200; %仿真代数
popnum = 80; % 群体规模
pm = 0.8;%变异概率
pc = 0.3; %交叉概率

pop = randi([0 1]mpopnum);%初始化粒子群,每列为一染色体

bestc = 0;
for ii = 1:Iter
    %1 计算适应度  采用离散度准则J4 = tr(Sb)/tr(Sw)
    pop1 = pop;
    for i=1:popnum
        temp_ind = find(pop(:i)~=0);
        train_temp = Train_images(temp_ind:);
        fiti(i) = zsl_J(train_tempTrain_labelsclass_label);
    end
    
    bestnow_ind = find(fiti==max(fiti));
    bestnow(ii) = fiti(bestnow_ind(1));
    if bestnow(ii) > bestc
        bestc = bestnow(ii);
        bestpop = pop(:bestnow_ind(1));
    end
    Bestev(ii) = bestc;
    
    
    %选择复制
    totalfiti = sum(fiti);
    fitvalue = fiti/totalfiti;
    fitvalue1 = cumsum(fitvalue);
    [pxpy] = size(pop);
    
    ms = sort(rand(py1));
%     for i=1:popnum
%         fitin = fi nd(fitvalue1>=ms(i));
%         newpop(:i) = pop(:fitin(1));
%     end
    
    for i=1:popnum
        tmp = find(rand<=fitvalue1);
        newpop(:i) = pop(:tmp(1));
    end
    
    pop = newpop;
    
    %交叉
    [pxpy] = size(pop);
    newpop = ones(size(pop));
    for i=1:2:py-1
        rand_temp = rand;
        if (rand_temp            cpoint = round(rand*px);
            if cpoint==0
                cpoint = 1;
            elseif cpoint == px
                cpoint = px-1;
            end
            newpop(:i) = [pop(1:cpointi);pop(cpoint+1:pxi+1)];
            newpop(:i+1) = [pop(1:cpointi+1);pop(cpoint+1:pxi)];
        else
            newpop(:i) = pop(:i);
            newpop(:i+1) = pop(:i+1);
        end
    end
    newpop(:N) = bestpop;
    pop = newpop;
    
    %单点变异
    [pxpy] = size(pop);
    newpop = ones(size(pop));
    for i=1:py
        if(rand            mpoint = round(rand*px);
            if mpoint <=0
                mpoint = 1;
            end
            newpop(:i) = pop(:i);
            if any(newpop(mpointi))==0
                newpop(mpointi)=1;
            else
                newpop(mpointi)=0;
            end
        else
            newpop(:i) = pop(:i);
        end
    end
    newpop(:N) = bestpop;
    pop = newpop;
   [ii]
    
end

%%

% 利用近邻法识别 SVM
% bestfeature_ind = find(pop(:10)~=0);
bestfeature_ind = find(bestpop==1);
train_images_select = Train_images(bestfeature_ind:);
test_images_select = Test_images(bestfeature_ind:);

acc1 = zsl_n

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3623  2012-05-04 21:54  ga1.m

----------- ---------  ---------- -----  ----

                 3623                    1


评论

共有 条评论