分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测

分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测

目录

    • [分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测](#分类预测 | Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测)

分类效果




基本介绍

1.Matlab实现GWO-LSSVM灰狼算法优化最小二乘支持向量机多特征分类预测(完整源码和数据),优化参数为,优化RBF核函数gam和sig,运行环境为Matlab2018及以上。

2.多特征输入单输出的二分类及多分类模型。程序内注释详细,直接替换excel数据就可以用;

3.程序语言为matlab,程序可出分类效果图,迭代优化图,混淆矩阵图。

4.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。

程序设计

clike 复制代码
%%  清空环境变量
warning off             % 关闭报警信息
close all               % 关闭开启的图窗
clear                   % 清空变量
clc                     % 清空命令行

%%  导入数据
res = xlsread('数据集.xlsx');
%% 划分训练集和测试集%
P_train = res(1: 250, 1: 12)';
T_train = res(1: 250, 13)';
M = size(P_train, 2);

P_test = res(251: end, 1: 12)';
T_test = res(251: end, 13)';
N = size(P_test, 2);
%% 数据归一化
[p_train, ps_input] = mapminmax(P_train,0,1);
p_test = mapminmax('apply',P_test,ps_input);
t_train = T_train;
t_test  = T_test;

%% LS参数设置
type        = 'c';             % 模型类型 分类
kernel_type = 'RBF_kernel';    % 线性核函数
codefct     = 'code_OneVsOne'; % 一对一编码分类
fun = @getObjValue;  % 目标函数
dim = 2;             % 优化参数个数
ub  = [300, 300];  % 优化参数目标上限
lb  = [1, 1];   % 优化参数目标下限

pop = 8;             % 数量
Max_iteration = 20; % 最大迭代次数   


c = Best_pos(1);  
g = Best_pos(2);

%% 编码
[t_train,codebook,old_codebook] = code(t_train,codefct);

%% 建立模型
model = initlssvm(p_train,t_train,type,c,g,kernel_type,codefct); %SSA

%% 训练模型
model = trainlssvm(model);

%% 测试模型
t_sim1 = simlssvm(model,p_train);
t_sim2 = simlssvm(model,p_test); 



T_sim1 = T_sim1(index_1);
T_sim2 = T_sim2(index_2);
%% 性能评价
error1 = sum((T_sim1' == T_train))/M * 100 ;
error2 = sum((T_sim2' == T_test))/N * 100 ;

%% 优化曲线
figure
plot(curve, 'linewidth',1.5);
title('-LSSVM')
xlabel('The number of iterations')
ylabel('Fitness')
grid on;
%%  绘图
figure
plot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)
legend('真实值', '-LSSVM预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['准确率=' num2str(error1) '%']};
title(string)
xlim([1, M])
grid

figure
plot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)
legend('真实值', '-LSSVM预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比'; ['准确率=' num2str(error2) '%']};
title(string)
xlim([1, N])
grid

%%  混淆矩阵
figure
cm = confusionchart(T_train, T_sim1);
cm.Title = 'Confusion Matrix for Train Data';
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';
    
figure
cm = confusionchart(T_test, T_sim2);
cm.Title = 'Confusion Matrix for Test Data';
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';

参考资料

1 http://t.csdn.cn/pCWSp

2 https://download.csdn.net/download/kjm13182345320/87568090?spm=1001.2014.3001.5501

3 https://blog.csdn.net/kjm13182345320/article/details/129433463?spm=1001.2014.3001.5501

相关推荐
毕竟是shy哥38 分钟前
计算YOLO数据集中每个类的目标数
算法·yolo·机器学习
M78佐菲42 分钟前
Linux学习笔记:TCP协议
linux·笔记·学习·tcp/ip·算法
晊晌_h2 小时前
嵌入式从0到精通——数据结构总结[特殊字符]
数据结构·算法·排序算法
我找到地球的支点啦3 小时前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信
罗西的思考3 小时前
【OpenClaw具身硬件】ZeroClaw 源码阅读笔记(3)--- RAG
人工智能·算法·机器学习
浪里镖客4 小时前
位姿转换矩阵写法-个人习惯(计算机理解其实是相反的)
线性代数·算法·矩阵
小白羊丨7 小时前
如何诊断 Prompt 模板导致的效果下降?
人工智能·算法·prompt
OPEN-F8 小时前
C++11/14新特性精讲:移动语义与智能指针实战
开发语言·c++·算法
lisin-lee-cooper8 小时前
【leetcode658】有序数组找出k个最接近x的数
java·数据结构·算法
sunburn-8 小时前
Java堆(Heap)详解与实战教学
java·开发语言·数据结构·ide·算法