回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测
Posted 机器学习之心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测相关的知识,希望对你有一定的参考价值。
回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测
目录
效果一览
基本介绍
MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测
粒子群算法优化随机森林(PSO-RF)回归预测(Matlab完整程序和数据)
输入6个特征,输出1个,即多输入单输出;
运行环境Matlab2018及以上,运行主程序main即可,其余为函数文件无需运行,所有程序放在一个文件夹,data为数据集;
命令窗口输出RMSEP、MAE、R2、MAPE。
程序设计
- 完整程序和数据下载方式1(资源处直接下载):MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测
- 完整程序和数据下载方式2(订阅《RF随机森林》专栏,同时可阅读《RF随机森林》专栏收录的所有内容,数据订阅后私信我获取):MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测
- 完整程序和数据下载方式3(订阅《智能学习》专栏,同时获取《智能学习》专栏收录程序6份,数据订阅后私信我获取):MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测
%% 定义粒子群算法参数
% N 种群 T 迭代次数
%% 随机初始化种群
D=dim; %粒子维数
c1=1.5; %学习因子1
c2=1.5; %学习因子2
w=0.8; %惯性权重
Xmax=ub; %位置最大值
Xmin=lb; %位置最小值
Vmax=ub; %速度最大值
Vmin=lb; %速度最小值
%%
%%%%%%%%%%%%%%%%初始化种群个体(限定位置和速度)%%%%%%%%%%%%%%%%
x=rand(N,D).*(Xmax-Xmin)+Xmin;
v=rand(N,D).*(Vmax-Vmin)+Vmin;
%%%%%%%%%%%%%%%%%%初始化个体最优位置和最优值%%%%%%%%%%%%%%%%%%%
p=x;
pbest=ones(N,1);
for i=1:N
pbest(i)=fobj(x(i,:));
end
%%%%%%%%%%%%%%%%%%%初始化全局最优位置和最优值%%%%%%%%%%%%%%%%%%
g=ones(1,D);
gbest=inf;
for i=1:N
if(pbest(i)<gbest)
g=p(i,:);
gbest=pbest(i);
end
end
%%%%%%%%%%%按照公式依次迭代直到满足精度或者迭代次数%%%%%%%%%%%%%
for i=1:T
i
for j=1:N
%%%%%%%%%%%%%%更新个体最优位置和最优值%%%%%%%%%%%%%%%%%
if (fobj(x(j,:))) <pbest(j)
p(j,:)=x(j,:);
pbest(j)=fobj(x(j,:));
end
%%%%%%%%%%%%%%%%更新全局最优位置和最优值%%%%%%%%%%%%%%%
if(pbest(j)<gbest)
g=p(j,:);
gbest=pbest(j);
end
%%%%%%%%%%%%%%%%%跟新位置和速度值%%%%%%%%%%%%%%%%%%%%%
v(j,:)=w*v(j,:)+c1*rand*(p(j,:)-x(j,:))...
+c2*rand*(g-x(j,:));
x(j,:)=x(j,:)+v(j,:);
%%%%%%%%%%%%%%%%%%%%边界条件处理%%%%%%%%%%%%%%%%%%%%%%
if length(Vmax)==1
for ii=1:D
if (v(j,ii)>Vmax) | (v(j,ii)< Vmin)
v(j,ii)=rand * (Vmax-Vmin)+Vmin;
end
if (x(j,ii)>Xmax) | (x(j,ii)< Xmin)
x(j,ii)=rand * (Xmax-Xmin)+Xmin;
end
end
else
for ii=1:D
if (v(j,ii)>Vmax(ii)) | (v(j,ii)< Vmin(ii))
v(j,ii)=rand * (Vmax(ii)-Vmin(ii))+Vmin(ii);
end
if (x(j,ii)>Xmax(ii)) | (x(j,ii)< Xmin(ii))
x(j,ii)=rand * (Xmax(ii)-Xmin(ii))+Xmin(ii);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%记录历代全局最优值%%%%%%%%%%%%%%%%%%%%%
Convergence_curve(i)=gbest;%记录训练集的适应度值
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
tic
restoredefaultpath
%% 导入数据
P_train = xlsread('data','training set','B2:G191')';
T_train= xlsread('data','training set','H2:H191')';
% 测试集——44个样本
P_test=xlsread('data','test set','B2:G45')';
T_test=xlsread('data','test set','H2:H45')';
%% 划分训练集和测试集
M = size(P_train, 2);
N = size(P_test, 2);
f_ = size(P_train, 1);
%% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input);
[t_train, ps_output] = mapminmax(T_train, 0, 1);
t_test = mapminmax('apply', T_test, ps_output);
%% 参数初始化
pop=10; %种群数量
Max_iter=30; % 设定最大迭代次数
dim = 2;% 维度为2,即优化两个超参数
lb = [1,1];%下边界
ub = [20,20];%上边界
fobj = @(x) fun(x,p_train,t_train);
[Best_pos,Best_score,curve]=PSO(pop,Max_iter,lb,ub,dim,fobj); %开始优化
%% 提取最优参数
n_trees = Best_pos(1);
n_layer = Best_pos(2);
%% 转置以适应模型
p_train = p_train'; p_test = p_test';
t_train = t_train'; t_test = t_test';
%% 创建模型
model = regRF_train(p_train, t_train, n_trees, n_layer);
参考资料
[1] https://blog.csdn.net/kjm13182345320/article/details/129215161
[2] https://blog.csdn.net/kjm13182345320/article/details/128105718
回归预测-lssvm基于粒子群算法优化最小二乘支持向量机lssvm实现数据回归预测附matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab仿真内容点击👇
⛄ 内容介绍
准确预测光伏电站输出功率,是促进光伏并网发电,提高电网运行稳定性的主要途径之一.该文提出一种基于粒子群算法最小二乘支持向量机(particle swarm optimization and least squares support vector machine,PSO-LSSVM)的日前光伏功率预测方法,该方法首先利用粒子群算法的全局搜索能力来获取最小二乘支持向量机的惩罚因子和核函数宽度,有效解决了最小二乘支持向量机难以快速精准寻找最优参数的问题;然后利用数值天气预报和光伏功率的历史数据对PSO-LSSVM模型进行训练,利用训练好的PSO-LSSVM模型对日前光伏功率进行预测.
⛄ 部分代码
function omega = kernel_matrix(Xtrain,kernel_type, kernel_pars,Xt)
% Construct the positive (semi-) definite and symmetric kernel matrix
%
% >> Omega = kernel_matrix(X, kernel_fct, sig2)
%
% This matrix should be positive definite if the kernel function
% satisfies the Mercer condition. Construct the kernel values for
% all test data points in the rows of Xt, relative to the points of X.
%
% >> Omega_Xt = kernel_matrix(X, kernel_fct, sig2, Xt)
%
%
% Full syntax
%
% >> Omega = kernel_matrix(X, kernel_fct, sig2)
% >> Omega = kernel_matrix(X, kernel_fct, sig2, Xt)
%
% Outputs
% Omega : N x N (N x Nt) kernel matrix
% Inputs
% X : N x d matrix with the inputs of the training data
% kernel : Kernel type (by default 'RBF_kernel')
% sig2 : Kernel parameter (bandwidth in the case of the 'RBF_kernel')
% Xt(*) : Nt x d matrix with the inputs of the test data
%
% See also:
% RBF_kernel, lin_kernel, kpca, trainlssvm, kentropy
% Copyright (c) 2011, KULeuven-ESAT-SCD, License & help @ http://www.esat.kuleuven.be/sista/lssvmlab
[nb_data,d] = size(Xtrain);
if strcmp(kernel_type,'RBF_kernel'),
if nargin<4,
XXh = sum(Xtrain.^2,2)*ones(1,nb_data);
omega = XXh+XXh'-2*(Xtrain*Xtrain');
omega = exp(-omega./(2*kernel_pars(1)));
else
XXh1 = sum(Xtrain.^2,2)*ones(1,size(Xt,1));
XXh2 = sum(Xt.^2,2)*ones(1,nb_data);
omega = XXh1+XXh2' - 2*Xtrain*Xt';
omega = exp(-omega./(2*kernel_pars(1)));
end
elseif strcmp(kernel_type,'RBF4_kernel'),
if nargin<4,
XXh = sum(Xtrain.^2,2)*ones(1,nb_data);
omega = XXh+XXh'-2*(Xtrain*Xtrain');
omega = 0.5*(3-omega./kernel_pars).*exp(-omega./(2*kernel_pars(1)));
else
XXh1 = sum(Xtrain.^2,2)*ones(1,size(Xt,1));
XXh2 = sum(Xt.^2,2)*ones(1,nb_data);
omega = XXh1+XXh2' - 2*Xtrain*Xt';
omega = 0.5*(3-omega./kernel_pars).*exp(-omega./(2*kernel_pars(1)));
end
% elseif strcmp(kernel_type,'sinc_kernel'),
% if nargin<4,
% omega = sum(Xtrain,2)*ones(1,size(Xtrain,1));
% omega = omega - omega';
% omega = sinc(omega./kernel_pars(1));
% else
% XXh1 = sum(Xtrain,2)*ones(1,size(Xt,1));
% XXh2 = sum(Xt,2)*ones(1,nb_data);
% omega = XXh1-XXh2';
% omega = sinc(omega./kernel_pars(1));
% end
elseif strcmp(kernel_type,'lin_kernel')
if nargin<4,
omega = Xtrain*Xtrain';
else
omega = Xtrain*Xt';
end
elseif strcmp(kernel_type,'poly_kernel')
if nargin<4,
omega = (Xtrain*Xtrain'+kernel_pars(1)).^kernel_pars(2);
else
omega = (Xtrain*Xt'+kernel_pars(1)).^kernel_pars(2);
end
% elseif strcmp(kernel_type,'wav_kernel')
% if nargin<4,
% XXh = sum(Xtrain.^2,2)*ones(1,nb_data);
% omega = XXh+XXh'-2*(Xtrain*Xtrain');
%
% XXh1 = sum(Xtrain,2)*ones(1,nb_data);
% omega1 = XXh1-XXh1';
% omega = cos(kernel_pars(3)*omega1./kernel_pars(2)).*exp(-omega./kernel_pars(1));
%
% else
% XXh1 = sum(Xtrain.^2,2)*ones(1,size(Xt,1));
% XXh2 = sum(Xt.^2,2)*ones(1,nb_data);
% omega = XXh1+XXh2' - 2*(Xtrain*Xt');
%
% XXh11 = sum(Xtrain,2)*ones(1,size(Xt,1));
% XXh22 = sum(Xt,2)*ones(1,nb_data);
% omega1 = XXh11-XXh22';
%
% omega = cos(kernel_pars(3)*omega1./kernel_pars(2)).*exp(-omega./kernel_pars(1));
% end
end
⛄ 运行结果
⛄ 参考文献
[1]殷樾. 基于粒子群算法最小二乘支持向量机的日前光伏功率预测[J]. 分布式能源, 2021, 6(2):7.
❤️ 关注我领取海量matlab电子书和数学建模资料
❤️部分理论引用网络文献,若有侵权联系博主删除
以上是关于回归预测 | MATLAB实现PSO-RF粒子群算法优化随机森林多输入单输出回归预测的主要内容,如果未能解决你的问题,请参考以下文章
回归预测基于matlab粒子群算法优化混合核极限学习机KELM回归预测含Matlab源码 JQ002期
FNN回归预测基于matlab粒子群优化前馈神经网络婚姻和离婚数据回归预测含Matlab源码 2069期
FNN回归预测基于matlab粒子群优化前馈神经网络婚姻和离婚数据回归预测含Matlab源码 2069期
回归预测基于matlab粒子群算法优化混合核极限学习机KELM回归预测含Matlab源码 JQ002期
优化预测基于matlab粒子群算法优化SVM回归预测(多输入多输出)含Matlab源码 1421期
DELM回归预测基于matlab粒子群算法改进深度学习极限学习机PSO-DELM数据回归预测含Matlab源码 1884期