%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
% 训练集——190个样本
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')';
N = size(P_test, 2); % 测试集样本数
M = size(P_train, 2); % 训练集样本数
%% 数据归一化
[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 = 20; %种群数量
Max_iter = 20; %最大迭代次数
lb = 0.001; %下边界
ub = 1; %上边界
dim = 1; %维度
fobj=@(X)fobj(X,p_train,t_train,p_test,t_test);
[ Best_score, Best_P,curve] = PSO(pop, Max_iter, lb, ub, dim, fobj);
%% 重新训练
disp(['最佳光滑因子值为',num2str(Best_P)])
%% 采用最佳方法建立GRNN网络
net=newgrnn(p_train,t_train,Best_P); %最优模型
t_sim1=sim(net,p_train); %训练集预测
t_sim2=sim(net,p_test); %测试集预测
%% 数据反归一化
T_sim1 = mapminmax('reverse', t_sim1, ps_output);
T_sim2 = mapminmax('reverse', t_sim2, ps_output);
%% 均方根误差
error1 = sqrt(sum((T_sim1 - T_train).^2) ./ M);
error2 = sqrt(sum((T_sim2 - T_test ).^2) ./ N);
%% 适应度曲线
figure
plot(1 : length(curve), curve,'r-', 'LineWidth', 1.5);
title('PSO-GRNN', 'FontSize', 13);
xlabel('迭代次数', 'FontSize', 10);
ylabel('适应度值', 'FontSize', 10);
grid off
%% 绘图
figure
plot(1: M, T_train, 'r-^', 'LineWidth', 1,'MarkerFaceColor', 'r','MarkerSize', 4);
hold on
plot(1: M, T_sim1, 'b-o', 'LineWidth', 1,'MarkerFaceColor', 'b','MarkerSize', 4);
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'PSO-GRNN训练集预测结果对比'; ['RMSE=' num2str(error1)]};
title(string)
xlim([1, M])
grid off
figure
plot(1: N, T_test, 'r-^', 'LineWidth', 1,'MarkerFaceColor', 'r','MarkerSize', 4);
hold on
plot(1: N, T_sim2, 'b-o', 'LineWidth', 1,'MarkerFaceColor', 'b','MarkerSize', 4);
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'PSO-GRNN测试集预测结果对比';['RMSE=' num2str(error2)]};
title(string)
xlim([1, N])
grid off
%% 误差图
figure
[AX,H1,H2]=plotyy(1:N,(T_sim2-T_test),1:N,(T_sim2-T_test)./T_test,'plot')%plot函数图像
set(AX(1),'XColor','k','YColor','b');%设置左边y轴
set(AX(2),'XColor','k','Ycolor','r');%设置右边y轴
xlabel('测试集样本编号'); %设置右边y轴
HH1=get(AX(1),'Ylabel');
set(HH1,'String','绝对误差');%设置左边y坐标轴标题
set(HH1,'color','b');%设置左边y坐标轴图样颜色为‘b’,蓝色
HH2=get(AX(2),'Ylabel');
set(HH2,'String','相对误差');%设置右边y坐标轴标题
set(HH2,'color','r');%设置右边y坐标轴图样颜色为‘r’红色
set(H1,'LineStyle','-','marker','o', 'LineWidth', 1,'MarkerFaceColor', 'b','MarkerSize', 4);%设置函数y1的线形状
set(H1,'color','b');%设置函数y1的线颜色
set(H2,'LineStyle',':','marker','^', 'LineWidth', 1,'MarkerFaceColor', 'r','MarkerSize', 4);%设置函数y2的线形状
set(H2,'color','r');%设置函数y2的线颜色
set(gca, 'FontSize', 10);
figure
[AX,H1,H2]=plotyy(1:M,(T_sim1-T_train),1:M,(T_sim1-T_train)./T_train,'plot');%plot函数图像
set(AX(1),'XColor','k','YColor','b');%设置左边y轴
set(AX(2),'XColor','k','Ycolor','r');%设置右边y轴
xlabel('训练集样本编号'); %设置右边y轴
HH1=get(AX(1),'Ylabel');
set(HH1,'String','绝对误差');%设置左边y坐标轴标题
set(HH1,'color','b');%设置左边y坐标轴图样颜色为‘b’,蓝色
HH2=get(AX(2),'Ylabel');
set(HH2,'String','相对误差');%设置右边y坐标轴标题
set(HH2,'color','r');%设置右边y坐标轴图样颜色为‘r’红色
set(H1,'LineStyle','-','marker','o', 'LineWidth', 1,'MarkerFaceColor', 'b','MarkerSize', 4);%设置函数y1的线形状
set(H1,'color','b');%设置函数y1的线颜色
set(H2,'LineStyle',':','marker','^', 'LineWidth', 1,'MarkerFaceColor', 'r','MarkerSize', 4);%设置函数y2的线形状
set(H2,'color','r');%设置函数y2的线颜色
set(gca, 'FontSize', 10);
%% 测试集结果
figure;
plotregression(T_test,T_sim2,['回归图']);
figure;
ploterrhist(T_test-T_sim2,['误差直方图']);
%% 相关指标计算
disp(['训练集数据误差:'])
[mae_train,mse_train,rmse_train,mape_train,error_train,errorPercent_train,R_train]=calc_error(T_train,T_sim1); %
disp(['测试集数据误差:'])
[mae_test,mse_test,rmse_test,mape_test,error_test,errorPercent_test,R_test]=calc_error(T_test,T_sim2); %
%% 训练集拟合效果图
R1 = 1 - norm(t_train - t_sim1)^2 / norm(t_train - mean(t_train))^2;
R2 = 1 - norm(t_test - t_sim2)^2 / norm(t_test - mean(t_test ))^2;
figure
plot(T_train,T_sim1,'ok');
xlabel('真实值')
ylabel('预测值')
string = {'训练集效果图';['R^2_c=' num2str(R1) ' RMSEC=' num2str(error1) ]};
title(string)
hold on ;h=lsline;
set(h,'LineWidth',1,'LineStyle','-','Color','r')
%% 预测集拟合效果图
figure
plot(T_test,T_sim2,'ob');
xlabel('真实值')
ylabel('预测值')
string1 = {'测试集效果图';['R^2_p=' num2str(R2) ' RMSEP=' num2str(error2) ]};
title(string1)
hold on ;h=lsline();
set(h,'LineWidth',1,'LineStyle','-','Color','r')
%% 全部数据集拟合效果图
figure
plot([T_train T_test],[T_sim1 T_sim2],'oc');
xlabel('真实值')
ylabel('预测值')
string1 = {'所有样本拟合预测图';['R^2_p=' num2str(mean([R2 R1])) ' RMSEP=' num2str(mean([R2 R1]))]};
title(string1)
hold on ;h=lsline();
set(h,'LineWidth',1,'LineStyle','-','Color','r')
%% 采用随机参数建立GRNN网络
net1=newgrnn(p_train,t_train); %未优化GRNN
t_sim1_rand=sim(net1,p_train); %训练集预测
t_sim2_rand=sim(net1,p_test); %测试集预测
%% 数据反归一化
T_sim1_rand = mapminmax('reverse', t_sim1_rand, ps_output);
T_sim2_rand = mapminmax('reverse', t_sim2_rand, ps_output);
%% 均方根误差
error1_rand = sqrt(sum((T_sim1_rand - T_train).^2) ./ M);
error2_rand = sqrt(sum((T_sim2_rand - T_test ).^2) ./ N);
%% 绘图
figure
plot(1: M, T_train, 'r-^', 'LineWidth', 1,'MarkerFaceColor', 'r','MarkerSize', 4);
hold on
plot(1: M, T_sim1_rand, 'b-o', 'LineWidth', 1,'MarkerFaceColor', 'b','MarkerSize', 4);
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'原始GRNN训练集预测结果对比'; ['RMSE=' num2str(error1_rand)]};
title(string)
xlim([1, M])
grid off
figure
plot(1: N, T_test, 'r-^', 'LineWidth', 1,'MarkerFaceColor', 'r','MarkerSize', 4);
hold on
plot(1: N, T_sim2_rand, 'b-o', 'LineWidth', 1,'MarkerFaceColor', 'b','MarkerSize', 4);
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'原始GRNN测试集预测结果对比';['RMSE=' num2str(error2_rand)]};
title(string)
xlim([1, N])
grid off
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
PSO-GRNN.zip (6个子文件)
PSO-GRNN
calc_error.m 2KB
fobj.m 341B
initialization.m 567B
main.m 7KB
PSO.m 2KB
data.xlsx 36KB
共 6 条
- 1
资源评论
yyliuhong
- 粉丝: 3
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- ThinkPHP5多小区物业管理系统源码PHP物业管理系统.zip
- 基于flask,Navicat和爬虫的可视化岗位分析网站.zip
- 部署yolov10的ncnn模型到android源码+模型+项目说明.zip
- Go语言网络层脚手架代码资料项目间共用的网络层.zip
- 基于monaco-editor的sql编辑器,支持sql语法联想、高亮.zip
- 网络安全相关的RSS订阅列表资料.zip
- 用Vue.js开发微信app项目源代码资料.zip
- 基于YOLO-V4算法与Pytorch的视频多目标跟踪系统的开发.zip
- 雾霾分析的MATLAB GUI仿真程序.zip
- 基于react的618节日网页全屏祝福.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功