0


机器学习之MATLAB代码--基于VMD与SSA优化lssvm的功率预测(多变量)(七)

机器学习之MATLAB代码--基于VMD与SSA优化lssvm的功率预测(多变量)(七)

代码

先对外层代码的揭露,包括:顺序而下
在这里插入图片描述

1、

  1. function s =Bounds( s, Lb, Ub)% Apply the lower bound vector
  2. temp = s;
  3. I = temp < Lb;temp(I)=Lb(I);% Apply the upper bound vector
  4. J = temp > Ub;temp(J)=Ub(J);% Update thisnewmove
  5. s = temp;

2、

  1. function [in,out]=data_process(data,num)% 采用1-num的各种值为输入 num+1的功率作为输出
  2. n=length(data)-num;for i=1:nx(i,:,:)=data(i:i+num,:);
  3. end
  4. in=x(1:end-1,:);out=x(2:end,end);%功率是最后一列

3、

  1. function y=fitness(x,train_x,train_y,test_x,test_y,typeID,kernelnamescell)
  2. gam=x(1);
  3. sig2=x(2);
  4. model=initlssvm(train_x,train_y,typeID,gam,sig2,kernelnamescell);
  5. model=trainlssvm(model);
  6. y_pre_test=simlssvm(model,test_x);
  7. y=mse(test_y-y_pre_test);

4、

  1. clc;clear;close all
  2. %%
  3. lssvm=load('lssvm.mat');
  4. ssa_lssvm=load('ssa_lssvm.mat');
  5. vmd_lssvm=load('vmd_lssvm.mat');
  6. vmd_ssa_lssvm=load('vmd_ssa_lssvm.mat');disp('结果分析-lssvm')
  7. result(lssvm.true_value,lssvm.predict_value)fprintf('\n')disp('结果分析-ssa-lssvm')
  8. result(ssa_lssvm.true_value,ssa_lssvm.predict_value)fprintf('\n')disp('结果分析-vmd-lssvm')
  9. result(vmd_lssvm.true_value,vmd_lssvm.predict_value)fprintf('\n')disp('结果分析-vmd-ssa-lssvm')result(vmd_ssa_lssvm.true_value,vmd_ssa_lssvm.predict_value)figureplot(lssvm.true_value)holdon;grid onplot(lssvm.predict_value)plot(ssa_lssvm.predict_value)plot(vmd_lssvm.predict_value)plot(vmd_ssa_lssvm.predict_value)legend('真实值','lssvm预测','ssa-lssvm预测','vmd-lssvm预测','vmd-ssa-lssvm预测')title('各算法预测效果对比')ylabel('功率值')xlabel('测试集样本')

5、

  1. %% 其他数据与功率数据一起作为输入,参看data_process.m
  2. clc;clear;close alladdpath(genpath('LSSVMlabv1_8'));%%
  3. data=xlsread('预测数据.xls','B2:K1000');[x,y]=data_process(data,24);%前24个时刻 预测下一个时刻
  4. %归一化
  5. [xs,mappingx]=mapminmax(x',0,1);x=xs';[ys,mappingy]=mapminmax(y',0,1);y=ys';%划分数据
  6. n=size(x,1);
  7. m=round(n*0.7);%前70%训练,对最后30%进行预测
  8. train_x=x(1:m,:);
  9. test_x=x(m+1:end,:);
  10. train_y=y(1:m,:);
  11. test_y=y(m+1:end,:);%% lssvm 参数
  12. gam=10;
  13. sig2=1000;
  14. typeID='function estimation';
  15. kernelnamescell='RBF_kernel';
  16. model=initlssvm(train_x,train_y,typeID,gam,sig2,kernelnamescell);
  17. model=trainlssvm(model);
  18. y_pre_test=simlssvm(model,test_x);% 反归一化
  19. predict_value=mapminmax('reverse',y_pre_test',mappingy);
  20. true_value=mapminmax('reverse',test_y',mappingy);
  21. save lssvm predict_value true_valuedisp('结果分析')
  22. rmse=sqrt(mean((true_value-predict_value).^2));disp(['根均方差(RMSE):',num2str(rmse)])
  23. mae=mean(abs(true_value-predict_value));disp(['平均绝对误差(MAE):',num2str(mae)])
  24. mape=mean(abs(true_value-predict_value)/true_value);disp(['平均相对百分误差(MAPE):',num2str(mape*100),'%'])fprintf('\n')figureplot(true_value,'-*','linewidth',3)
  25. hold onplot(predict_value,'-s','linewidth',3)legend('实际值','预测值')
  26. grid ontitle('LSSVM')

6、

  1. clc;clear;close all;format compactaddpath(genpath('LSSVMlabv1_8'));%%
  2. data=xlsread('预测数据.xls','B2:K1000');[x,y]=data_process(data,24);%前24个时刻 预测下一个时刻
  3. %归一化
  4. [xs,mappingx]=mapminmax(x',0,1);x=xs';[ys,mappingy]=mapminmax(y',0,1);y=ys';%划分数据
  5. n=size(x,1);
  6. m=round(n*0.7);%前70%训练,对最后30%进行预测
  7. train_x=x(1:m,:);
  8. test_x=x(m+1:end,:);
  9. train_y=y(1:m,:);
  10. test_y=y(m+1:end,:);%% ssa优化lssvm 参数
  11. typeID='function estimation';
  12. kernelnamescell='RBF_kernel';[x,trace]=ssa_lssvm(typeID,kernelnamescell,train_x,train_y,test_x,test_y);
  13. figure;plot(trace);title('适应度曲线/mse')%% 利用寻优得到的参数重新训练lssvmdisp('寻优得到的参数分别是:')
  14. gam=x(1)
  15. sig2=x(2)
  16. model=initlssvm(train_x,train_y,typeID,gam,sig2,kernelnamescell);
  17. model=trainlssvm(model);
  18. y_pre_test=simlssvm(model,test_x);% 反归一化
  19. predict_value=mapminmax('reverse',y_pre_test',mappingy);
  20. true_value=mapminmax('reverse',test_y',mappingy);
  21. save ssa_lssvm predict_value true_valuedisp('结果分析')
  22. rmse=sqrt(mean((true_value-predict_value).^2));disp(['根均方差(RMSE):',num2str(rmse)])
  23. mae=mean(abs(true_value-predict_value));disp(['平均绝对误差(MAE):',num2str(mae)])
  24. mape=mean(abs(true_value-predict_value)/true_value);disp(['平均相对百分误差(MAPE):',num2str(mape*100),'%'])fprintf('\n')figureplot(true_value,'-*','linewidth',3)
  25. hold onplot(predict_value,'-s','linewidth',3)legend('实际值','预测值')
  26. grid ontitle('SSA-LSSVM')

7、

  1. clc;clear;format compact;close all;ticrng('default')%% 数据预处理
  2. data=xlsread('预测数据.xls','B2:K1000');
  3. figure;plot(data(:,end),'-*','linewidth',3);title('原始功率曲线');gridon;ylabel('功率值')
  4. alpha =2000;% moderate bandwidth constraint
  5. tau =0;% noise-tolerance (no strict fidelity enforcement)
  6. K =5;%3modes
  7. DC =0;% no DC part imposed
  8. init =1;% initialize omegas uniformly
  9. tol =1e-7;[imf,u_hat,omega]=VMD(data(:,end), alpha, tau, K, DC, init, tol);
  10. figure
  11. for i =1:size(imf,1)
  12. subplot(size(imf,1),1,i)plot(imf(i,:))ylabel(['imf',num2str(i)])endylabel('残余')suptitle('VMD')
  13. c=size(imf,1);
  14. pre_result=[];
  15. true_result=[];%% 对每个分量建模
  16. for i=1:cdisp(['对第',num2str(i),'个分量建模'])[x,y]=data_process([data(:,1:end-1)imf(i,:)'],24);%每个序列和原始的几列数据合并 然后划分
  17. %归一化
  18. [xs,mappingx]=mapminmax(x',0,1);x=xs';[ys,mappingy]=mapminmax(y',0,1);y=ys';%划分数据
  19. n=size(x,1);
  20. m=round(n*0.7);%前70%训练,对最后30%进行预测
  21. train_x=x(1:m,:);
  22. test_x=x(m+1:end,:);
  23. train_y=y(1:m,:);
  24. test_y=y(m+1:end,:);%%
  25. gam=100;
  26. sig2=1000;
  27. typeID='function estimation';
  28. kernelnamescell='RBF_kernel';
  29. model=initlssvm(train_x,train_y,typeID,gam,sig2,kernelnamescell);
  30. model=trainlssvm(model);
  31. pred=simlssvm(model,test_x);
  32. pre_value=mapminmax('reverse',pred',mappingy);
  33. true_value=mapminmax('reverse',test_y',mappingy);
  34. pre_result=[pre_result;pre_value];
  35. true_result=[true_result;true_value];
  36. end
  37. %% 各分量预测的结果相加
  38. true_value=sum(true_result);
  39. predict_value=sum(pre_result);
  40. save vmd_lssvm predict_value true_value
  41. %%
  42. load vmd_lssvmdisp('结果分析')
  43. rmse=sqrt(mean((true_value-predict_value).^2));disp(['根均方差(RMSE):',num2str(rmse)])
  44. mae=mean(abs(true_value-predict_value));disp(['平均绝对误差(MAE):',num2str(mae)])
  45. mape=mean(abs(true_value-predict_value)/true_value);disp(['平均相对百分误差(MAPE):',num2str(mape*100),'%'])fprintf('\n')figureplot(true_value,'-*','linewidth',3)
  46. hold onplot(predict_value,'-s','linewidth',3)legend('实际值','预测值')
  47. grid ontitle('VMD-LSSVM')

8、

  1. clc;clear;format compact;close all;ticrng('default')%% 数据预处理
  2. data=xlsread('预测数据.xls','B2:K1000');
  3. figure;plot(data(:,end),'-*','linewidth',3);title('原始功率曲线');gridon;ylabel('功率值')
  4. alpha =2000;% moderate bandwidth constraint
  5. tau =0;% noise-tolerance (no strict fidelity enforcement)
  6. K =5;%3modes
  7. DC =0;% no DC part imposed
  8. init =1;% initialize omegas uniformly
  9. tol =1e-7;[imf,u_hat,omega]=VMD(data(:,end), alpha, tau, K, DC, init, tol);
  10. figure
  11. for i =1:size(imf,1)
  12. subplot(size(imf,1),1,i)plot(imf(i,:))ylabel(['imf',num2str(i)])endylabel('残余')suptitle('VMD')
  13. c=size(imf,1);
  14. pre_result=[];
  15. true_result=[];%% 对每个分量建模
  16. for i=1:cdisp(['对第',num2str(i),'个分量建模'])[x,y]=data_process([data(:,1:end-1)imf(i,:)'],24);%归一化
  17. [xs,mappingx]=mapminmax(x',0,1);x=xs';[ys,mappingy]=mapminmax(y',0,1);y=ys';%划分数据
  18. n=size(x,1);
  19. m=round(n*0.7);%前70%训练,对最后30%进行预测
  20. train_x=x(1:m,:);
  21. test_x=x(m+1:end,:);
  22. train_y=y(1:m,:);
  23. test_y=y(m+1:end,:);%%
  24. typeID='function estimation';
  25. kernelnamescell='RBF_kernel';[x,trace]=ssa_lssvm(typeID,kernelnamescell,train_x,train_y,test_x,test_y);
  26. gam=x(1);
  27. sig2=x(2);
  28. model=initlssvm(train_x,train_y,typeID,gam,sig2,kernelnamescell);
  29. model=trainlssvm(model);
  30. pred=simlssvm(model,test_x);
  31. pre_value=mapminmax('reverse',pred',mappingy);
  32. true_value=mapminmax('reverse',test_y',mappingy);
  33. pre_result=[pre_result;pre_value];
  34. true_result=[true_result;true_value];
  35. end
  36. %% 各分量预测的结果相加
  37. true_value=sum(true_result);
  38. predict_value=sum(pre_result);
  39. save vmd_ssa_lssvm predict_value true_value
  40. %%
  41. load vmd_ssa_lssvmdisp('结果分析')
  42. rmse=sqrt(mean((true_value-predict_value).^2));disp(['根均方差(RMSE):',num2str(rmse)])
  43. mae=mean(abs(true_value-predict_value));disp(['平均绝对误差(MAE):',num2str(mae)])
  44. mape=mean(abs(true_value-predict_value)/true_value);disp(['平均相对百分误差(MAPE):',num2str(mape*100),'%'])fprintf('\n')figureplot(true_value,'-*','linewidth',3)
  45. hold onplot(predict_value,'-s','linewidth',3)legend('实际值','预测值')
  46. grid ontitle('VMD-SSA-LSSVM')

9、

  1. clear;close all;
  2. clc;
  3. format compactaddpath('vmd-verify')
  4. data=xlsread('预测数据.xls','B2:K1000');
  5. f=data(:,end);% some sample parameters forVMD
  6. alpha =2000;% moderate bandwidth constraint
  7. tau =0;% noise-tolerance (no strict fidelity enforcement)
  8. K =5;%3modes
  9. DC =0;% no DC part imposed
  10. init =1;% initialize omegas uniformly
  11. tol =1e-7;%--------------- Run actual VMD code
  12. [u, u_hat, omega]=VMD(f, alpha, tau, K, DC, init, tol);figureplot(f)title('原始')
  13. figure
  14. for i=1:K
  15. subplot(K,1,i)plot(u(i,:))ylabel(['IMF_',num2str(i)])endylabel('res')

10、

  1. function result(true_value,predict_value)
  2. rmse=sqrt(mean((true_value-predict_value).^2));disp(['根均方差(RMSE):',num2str(rmse)])
  3. mae=mean(abs(true_value-predict_value));disp(['平均绝对误差(MAE):',num2str(mae)])
  4. mape=mean(abs(true_value-predict_value)/true_value);disp(['平均相对百分误差(MAPE):',num2str(mape*100),'%'])

11、

  1. function [bestX,Convergence_curve]=ssa_lssvm(typeID,Kernel_type,inputn_train,label_train,inputn_test,label_test)%% 麻雀优化
  2. pop=10;% 麻雀数
  3. M=10;% Maximum numbef of iterations
  4. c=1;
  5. d=10000;
  6. dim=2;
  7. P_percent =0.2;% The population size of producers accounts for"P_percent" percent of the total population size
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. pNum =round( pop * P_percent );% The population size of the producers
  10. lb= c.*ones(1,dim );% Lower limit/bounds/ a vector
  11. ub= d.*ones(1,dim );% Upper limit/bounds/ a vector
  12. %Initialization
  13. for i =1:popx( i,:)= lb +(ub - lb).*rand(1, dim );fit( i )=fitness(x(i,:),inputn_train,label_train,inputn_test,label_test,typeID,Kernel_type);end
  14. pFit = fit;
  15. pX = x;% The individual's best position corresponding to the pFit
  16. [ fMin, bestI ]=min( fit );% fMin denotes the global optimum fitness value
  17. bestX =x( bestI,:);% bestX denotes the global optimum position corresponding to fMin
  18. for t =1: M
  19. [ ans, sortIndex ]=sort( pFit );% Sort.[fmax,B]=max( pFit );
  20. worse=x(B,:);
  21. r2=rand(1);%%%%%%%%%%%%%5%%%%%%这一部位为发现者(探索者)的位置更新%%%%%%%%%%%%%%%%%%%%%%%%%if(r2<0.8)%预警值较小,说明没有捕食者出现
  22. for i =1: pNum %r2小于0.8的发现者的改变(1-20) % Equation (3)
  23. r1=rand(1);x(sortIndex( i ),:)=pX(sortIndex( i ),:)*exp(-(i)/(r1*M));%对自变量做一个随机变换
  24. x(sortIndex( i ),:)=Bounds(x(sortIndex( i ),:), lb, ub );%对超过边界的变量进行去除
  25. fit(sortIndex( i ))=fitness(x(sortIndex( i ),:),inputn_train,label_train,inputn_test,label_test,typeID,Kernel_type);
  26. end
  27. else%预警值较大,说明有捕食者出现威胁到了种群的安全,需要去其它地方觅食
  28. for i =1: pNum %r2大于0.8的发现者的改变
  29. x(sortIndex( i ),:)=pX(sortIndex( i ),:)+randn(1)*ones(1,dim);x(sortIndex( i ),:)=Bounds(x(sortIndex( i ),:), lb, ub );fit(sortIndex( i ))=fitness(x(sortIndex( i ),:),inputn_train,label_train,inputn_test,label_test,typeID,Kernel_type);
  30. end
  31. end
  32. [ fMMin, bestII ]=min( fit );
  33. bestXX =x( bestII,:);%%%%%%%%%%%%%5%%%%%%这一部位为加入者(追随者)的位置更新%%%%%%%%%%%%%%%%%%%%%%%%%for i =( pNum +1): pop %剩下20-100的个体的变换 % Equation (4)
  34. A=floor(rand(1,dim)*2)*2-1;if( i>(pop/2))%这个代表这部分麻雀处于十分饥饿的状态(因为它们的能量很低,也是是适应度值很差),需要到其它地方觅食
  35. x(sortIndex(i ),:)=randn(1)*exp((worse-pX(sortIndex( i ),:))/(i)^2);else%这一部分追随者是围绕最好的发现者周围进行觅食,其间也有可能发生食物的争夺,使其自己变成生产者
  36. x(sortIndex( i ),:)=bestXX+(abs((pX(sortIndex( i ),:)-bestXX)))*(A'*(A*A')^(-1))*ones(1,dim);endx(sortIndex( i ),:)=Bounds(x(sortIndex( i ),:), lb, ub );%判断边界是否超出
  37. fit(sortIndex( i ))=fitness(x(sortIndex( i ),:),inputn_train,label_train,inputn_test,label_test,typeID,Kernel_type);
  38. end
  39. %%%%%%%%%%%%%5%%%%%%这一部位为意识到危险(注意这里只是意识到了危险,不代表出现了真正的捕食者)的麻雀的位置更新%%%%%%%%%%%%%%%%%%%%%%%%%
  40. c=randperm(numel(sortIndex));%%%%%%%%%这个的作用是在种群中随机产生其位置(也就是这部分的麻雀位置一开始是随机的,意识到危险了要进行位置移动,
  41. %处于种群外围的麻雀向安全区域靠拢,处在种群中心的麻雀则随机行走以靠近别的麻雀)
  42. b=sortIndex(c(1:10));for j =1:length(b)% Equation (5)if(pFit(sortIndex(b(j)))>(fMin))%处于种群外围的麻雀的位置改变
  43. x(sortIndex(b(j)),:)=bestX+(randn(1,dim)).*(abs((pX(sortIndex(b(j)),:)-bestX)));else%处于种群中心的麻雀的位置改变
  44. x(sortIndex(b(j)),:)=pX(sortIndex(b(j)),:)+(2*rand(1)-1)*(abs(pX(sortIndex(b(j)),:)-worse))/(pFit(sortIndex(b(j)))-fmax+1e-50);endx(sortIndex(b(j)),:)=Bounds(x(sortIndex(b(j)),:), lb, ub );fit(sortIndex(b(j)))=fitness(x(sortIndex(b(j)),:),inputn_train,label_train,inputn_test,label_test,typeID,Kernel_type);
  45. end
  46. for i =1: pop
  47. if(fit( i )<pFit( i ))pFit( i )=fit( i );pX( i,:)=x( i,:);
  48. end
  49. if(pFit( i )< fMin )
  50. fMin=pFit( i );
  51. bestX =pX( i,:);
  52. end
  53. endConvergence_curve(t,:)=[fMinmean(pFit)];
  54. end

接下来是内嵌代码,就是下面两个文件夹的代码,实在是太多,想要的留言吧!
在这里插入图片描述
在这里插入图片描述

数据

数据是由时间、风速、风向等等因素组成的文件。
在这里插入图片描述

结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
结果图太多,就先给出这么多,如有需要代码和数据的同学请在评论区发邮箱,一般一天之内会回复,请点赞+关注谢谢!!

标签: matlab 人工智能

本文转载自: https://blog.csdn.net/weixin_44312889/article/details/128098263
版权归原作者 小陈IT 所有, 如有侵权,请联系我们删除。

“机器学习之MATLAB代码--基于VMD与SSA优化lssvm的功率预测(多变量)(七)”的评论:

还没有评论