%BP神经网络方法在用电量预测中的应用
clear all;
%训练样本存于二维数组中,x[9][4]
%x = [15002043,20346236,27629276,22575072;
% 20295598,21921608,31412233,23609863;
% 21065081,24240841,29866274,23328736;
% 21716443,24706176,29987753,23379667;
% 21129872,23622805,28921753,22793806;
%25560986,29038399,37734355,28395655;
% 25073397,28159125,37558426,28395655;
% 25254364,20792240,40910291,28945775;
% 27795417,31318638,44438585,29317211];
p = [15002043,20346236,27629276;
20295598,21921608,31412233;
21065081,24240841,29866274;
21716443,24706176,29987753;
21129872,23622805,28921753;
25560986,29038399,37734355;
25073397,28159125,37558426;
25254364,20792240,40910291;
27795417,31318638,44438585]'
t = [22575072,23609863,23328736,23379667,22793806,28395655,28395655,28945775,29317211];
year = 1995:1:2003;
%预测的根据(前3个季度的数据)存于数组data[3]中
data = [21715443,24703176,29987753]';
%由x层到隐层y的权重记录在二维数组wx2y[3][6]中,阈值记录在一维数组cy[6]中;
%由y层到v层的权重记录在数组wy2v[6]中,阈值为cv
wx2y = zeros(3,6);
cy = zeros(6);
wy2v = zeros(6);
cv = 0;
%数据归一化,(0,1)
ma1 = max(max(p));
ma2 = max(t);
ma = max(ma1,ma2);
mi1 = min(min(p));
mi2 = min(t);
mi = min(mi1,mi2);
for i=1:3
for j = 1:9
p(i,j) = (p(i,j)-mi)/(ma-mi);
end
end
for i = 1:9
t(i) = (t(i)-mi)/(ma-mi);
end
% NEWFF Create a feed-forward backpropagation network.
net = newff([0 1;0 1;0 1],[6 1],{'logsig' 'purelin'},'trainlm');
%画出训练前的拟合图形
Y1 = sim(net,p)
%plot(p,t,p,Y1,'--');
plot(year,t,year,Y1);
axis([1995,2003,0,1]);
title('训练前');
xlabel('年份');
%xlabel('前3个季度的用电量');
ylabel('第四季度的用电量');
%训练精度
net.trainparam.goal = 0.0001;
net.trainparam.epochs = 10000;%训练次数
%训练神经网络
[net,tr] = train(net,p,t);
%net = train(net,p,t);
%输出训练后的权值和阈值
Y = sim(net,p)
%plot(p,t,p,Y,'--');
plot(year,t,year,Y,'--');
axis([1995,2003,0,1]);
title('训练后');
%xlabel('前3个季度的用电量');
xlabel('年份');
ylabel('第四季度的用电量');
%输出权值矩阵和阈值
iw1 = net.IW{1}
b1 = net.b{1}
iw2 = net.LW{2}
b2 = net.b{2}
%Y = sim(net,data)
%存储训练好的神经网络
save netkohler net