【SVM分类】基于布谷鸟算法优化支持向量机SVM实现数据分类附matlab代码

456 阅读2分钟

1 简介

针对经典支持向量机不适用于非平衡数据集分类的缺点,本研究结合径向基核函数,布谷鸟算法以及支持向量机,提出一种基于布谷鸟算法优化支持向量机的分类识别模型,用于分类诊断.在收集到的735例有效样本数据集上,采用matlab程序抽取平衡数据集.实验结果显示,基于平衡数据集,该模型的平均正确率为80.667%;基于非平衡数据集,其平均正确率为97.767%,相比经典支持向量机,粒子群算法-支持向量机,遗传算法-支持向量机均有不同程度的提高.因此,本研究模型对胸痛三联征的分类诊断具有一定的参考价值.

2 部分代码

function [fmin, bestnest, Curve] = CS(nest, N, Max_iter, Lb, Ub, dim, fobj)

%% CS参数

% 发现概率

pa = 0.25;

% 初始最优解

fitness = 10^10*ones(N, 1);

[fmin, bestnest, nest, fitness] = get_best_nest(nest, nest, fitness);

% N_iter = 0;

%% 迭代寻优

for iter = 1:Max_iter

    %% 通过莱维飞行得到新个体

    beta = 3/2;

    sigma = (gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);

    for i = 1:N

        s = nest(i, :);

        % 用蒙特卡洛方法模拟莱维飞行

        u = randn(size(s))*sigma;

        v = randn(size(s));

        step = u./abs(v).^(1/beta);

        stepsize = 0.01*step.*(s-bestnest);

        s=s+stepsize.*randn(size(s));

        % 边界处理

        new_nest(i, :) = simplebounds(s, Lb, Ub);

    end

    % 新个体和旧个体适应度值贪婪比较,选取最优个体

    [fnew, best, nest, fitness] = get_best_nest(nest, new_nest, fitness);

    %      % 更新计数器

    %      N_iter = N_iter+N;

    % 发现和随机化

    new_nest = empty_nests(nest, Lb, Ub, pa) ;

    

    % 新个体和旧个体适应度值贪婪比较,选取最优个体

    [fnew, best, nest, fitness] = get_best_nest(nest, new_nest, fitness);

    %      % 再次更新计数器

    %      N_iter = N_iter+N;

    % 更新全局最优解

    if fnew < fmin

        fmin = fnew;

        bestnest = best;

    end

    %% 记录每代最优解

    Curve(iter) = fmin;

    %% 显示每代优化结果

    display(['CS:At iteration ', num2str(iter), ' the best fitness is ', num2str(fmin)]);

end

%% Display all the nests

% disp(strcat('Total number of iterations=', num2str(N_iter)));

%% 最终结果显示

disp(['最终位置:' , num2str(bestnest)]);

disp(['最终函数值:', num2str(Curve(end))]);

% %% 绘图

% figure;

% plot(Curve, 'r', 'lineWidth', 2);          %  画出迭代图

% xlabel('迭代次数', 'fontsize', 12);

% ylabel('目标函数值', 'fontsize', 12);

%% ---------------下面列出了所有子函数------------------

%% 发现最优位置

function [fmin, best, nest, fitness] = get_best_nest(nest, newnest, fitness)

for j = 1:size(nest,1)

    fnew = fobj(newnest(j,:));

    if fnew <= fitness(j)

        fitness(j) = fnew;

        nest(j, :) = newnest(j, :);

    end

end

% Find the current best

[fmin, K] = min(fitness) ;

best = nest(K, :);

end

%% 通过构造新的个体来替代某些个体

function new_nest = empty_nests(nest, Lb, Ub, pa)

% 一小部分更糟的巢穴被发现的概率为pa

n = size(nest, 1);

% 发现与否——一个状态向量

K = rand(size(nest)) > pa;

% 有偏/选择随机游动的新解

stepsize = rand*(nest(randperm(n), :)-nest(randperm(n), :));

new_nest = nest+stepsize.*K;

for j = 1:size(new_nest, 1)

    s = new_nest(j, :);

    new_nest(j, :) = simplebounds(s, Lb, Ub);

end

end

%% 边界处理

function s = simplebounds(s,Lb,Ub)

% Apply the lower bound

ns_tmp = s;

I = ns_tmp<Lb;

ns_tmp(I) = Lb;

% Apply the upper bounds

J = ns_tmp>Ub;

ns_tmp(J) = Ub;

% Update this new move

s = ns_tmp;

end

end

3 仿真结果

4 参考文献

[1]胡智强等. "基于布谷鸟搜索优化支持向量机的短期负荷预测." 水电能源科学 34.12(2016):4.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。