MATLAB code for machine learning - classic ELM (3)

MATLAB code for machine learning - classic ELM (3)

the code

clear
close all
clc
input=[1	13.3	7.7	10.5	69.00000001	20.07
1.5	13.05	7.7	10.4	70	20.43
2	12.8	7.7	10.3	71	17.92
2.5	12.75	7.8	10.3	72	17.48
3	12.7	7.9	10.3	73	17
3.5	12.7	8.100000001	10.4	74	16.81
4	12.7	8.300000001	10.5	75.00000001	16.43
4.5	12.6	8.4	10.5	76	16.19
5	12.5	8.5	10.5	77	16.68
5.5	12.4	8.149999999	10.3	75.5	17.2
6	12.3	7.8	10.1	74	17.68
6.5	12.25	7.9	10.1	74.5	22.87
7	12.2	7.999999998	10.1	74.99999999	25.38
7.5	12.4	7.6	10.05	72.5	26.45
8	12.6	7.2	10	70	31.75
8.5	13.15	7.850000002	10.55	70.5	35.56
9	13.7	8.5	11.1	71	27.24
9.5	14.6	8.800000001	11.65	68.49999999	26.23
10	15.5	9.100000001	12.2	65.99999999	25.26
10.5	16.05	9.2	12.5	64	24.76
11	16.6	9.3	12.8	62	23.39
11.5	16.65	9.900000002	13.1	64.50000001	23.06
12	16.7	10.5	13.4	67	22.57
12.5	17.1	9.899999998	13.3	62.99999999	22.62
13	17.5	9.299999999	13.2	58.99999999	22.33
13.5	17.6	9.05	13.15	57.5	22.28
14	17.7	8.8	13.1	56	22.05
14.5	17.75	8.549999999	13	55	21.19
15	17.8	8.3	12.9	54	19.87
15.5	17.2	9.050000002	12.95	59.00000001	19.49];  %载入输入数据
output=[8974.73
8819.75
8593.21
8165.02
7792.29
7451
7262.58
7192.3
7252.37
7544.07
8020.28
8986.43
9922.95
10337.4
10769.53
10808.89
10644.19
10541.34
10437.73
10324.85
10231.09
10045.83
9959.69
9865.52
9779.02
9705.81
9635.86
9636.27
9610.29
9562.34

];  

input_train = input(1:27,:)';
output_train =output(1:27,:)';
input_test = input(28:30,:)';
output_test =output(28:30,:)';

inputnum=2; 
hiddennum=5;
outputnum=1; 

[inputn,inputps]=mapminmax(input_train);%%  归一化
[outputn,outputps]=mapminmax(output_train);

net=newff(inputn,outputn,hiddennum,{
    
    'tansig','purelin'},'trainlm');% purelin传递函数,梯度下降法

W1= net. iw{
    
    1, 1};
B1 = net.b{
    
    1};
W2 = net.lw{
    
    2,1};
B2 = net. b{
    
    2};

net.trainParam.epochs=1000;         % 训练次数
net.trainParam.lr=0.01;             % 学习速率
net.trainParam.goal=0.00001;        % 训练目标最小误差
net.dividefcn='';%关闭过拟合解烨功能

net=train(net,inputn,outputn);
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test); %进行仿真
an0=sim(net,inputn);%对训练集仿真
train_simu=mapminmax('reverse',an0,outputps);
figure('units','normalized','position',[0.119 0.2 0.38 0.5])
plot(output_train,'bo-','markerfacecolor','b')
hold on
plot(train_simu,'rs-','markerfacecolor','r')
grid on
%hold on
%plot(error,'square','MarkerFaceColor','b')
legend('期望值','预测值','误差')
xlabel('数据组数')
ylabel('样本值')
title('极限学习机ELM测试集的预测值与实际值对比图')
 
test_simu=mapminmax('reverse',an,outputps);   
error=test_simu-output_test;    

figure('units','normalized','position',[0.5 0.2 0.38 0.5])
plot(output_test,'bo-','markerfacecolor','b')
hold on
plot(test_simu,'rs-','markerfacecolor','r')
grid on
%hold on
%plot(error,'square','MarkerFaceColor','b')
legend('期望值','预测值')
xlabel('数据组数')
ylabel('样本值')
title('极限学习机ELM测试集的预测值与实际值对比图')

[c,l]=size(output_test);
MAE1=sum(abs(error))/l;
MSE1=error*error'/l;
RMSE1=MSE1^(1/2);
MAPE1=sum(abs(error/output_test)/l);
R=corrcoef(output_test,test_simu);
R2=R(1,2)^2;%0.75以上就代表非常好了
disp(['-----------------------误差计算--------------------------'])
disp(['隐含层节点数为',num2str(hiddennum),'时的误差结果如下:'])
disp(['平均绝对误差MAE为:',num2str(MAE1)])
disp(['均方误差MSE为:       ',num2str(MSE1)])
disp(['均方根误差RMSE为:  ',num2str(RMSE1)])
disp(['相对平均误差MAPE为:  ',num2str(MAPE1)])
disp(['绝对系数R^2为:  ',num2str(R2)])
%导入用来预测的数据
A1=[16
16.6
9.799999996
13
63.99999998
19.66
];
%预测输出
Predict0=sim(net,A1)%根据输入数据得到预测数据
Predict=mapminmax('reverse',Predict0,outputps); 
disp(['预测下一天的数据为:  ',num2str(Predict)])

result

insert image description here
insert image description here
insert image description here
If you need code and data, please send an email in the comment area, usually a reply will be made within one day, please like + pay attention, thank you! !

Guess you like

Origin blog.csdn.net/weixin_44312889/article/details/128091472
Recommended