%% 加载数据与数据集划分
clc;clear;close all
load data
nwhole=length(data); %计算数据长度
train_ratio=0.75;
ntrain=round(nwhole*train_ratio);
ntest =nwhole-ntrain+209;
nntest=nwhole-ntrain;
%%
%准备训练数据
traindata= data(1:ntrain);
utrain=ntrain-209;
input_train=zeros(209,utrain);
for i=1:utrain
input_train(:,i)=traindata(i:i+208)';
end
output_train=zeros(1,utrain);
for i=1:utrain
output_train(:,i)=traindata(i+209:i+209)';
end
%准备测试数据
testdata=data(end-ntest+1:end);
utest=ntest-209;
input_test=zeros(209,utest);
for i=1:utest
input_test(:,i)=testdata(i:i+208)';
end
output_test=zeros(1,utest);
for i=1:utest
output_test(:,i)=testdata(i+209:i+209)';
end
%% 归一化(全部特征 均归一化)
[inputn_train,inputps]=mapminmax(input_train);
[outputn_train,outputps]=mapminmax(output_train);
inputn_test=mapminmax('apply',input_test,inputps);
outputn_test=mapminmax('apply',output_test,outputps);
inputn =[inputn_train inputn_test];
outputn=[outputn_train outputn_test];
%% 数据的一个转换,转换成MATLAB的CNN的输入数据形式,是4-D形式的,最后一维就是样本数
%训练集输入
trainD=reshape(inputn_train, [209,1,1,size(inputn_train,2)]);
%测试集输入
testD =reshape(inputn_test, [209,1,1,size(inputn_test,2)]);
%% CNN模型建立
layers = [
imageInputLayer([209 1 1]) %输入层参数设置
convolution2dLayer(3,16,'Padding','same')%卷积层的核大小、数量,填充方式
reluLayer %relu激活函数
fullyConnectedLayer(384) % 384 全连接层神经元
fullyConnectedLayer(384) % 384 全连接层神经元
fullyConnectedLayer(1) % 输出层神经元
regressionLayer]; % 添加回归层,用于计算损失值
%% 模型训练与测试
options = trainingOptions('adam', ...
'MaxEpochs',20, ...
'MiniBatchSize',16, ...
'InitialLearnRate',0.005, ...
'GradientThreshold',1, ...
'Verbose',false,...
'Plots','training-progress',...
'ValidationData',{testD,outputn_test'});
% 训练
CNNnet = trainNetwork(trainD,outputn_train',layers,options);
%% CNN测试数据
%%
%网络测试输出
CNNoutputr_test = predict(CNNnet,testD);
CNNoutputr_test = double(CNNoutputr_test');
%网络输出反归一化
CNNoutput_test= mapminmax('reverse',CNNoutputr_test,outputps);
CNNoutput_test=double(CNNoutput_test);
otest1=CNNoutput_test(1,1:end-1);
otest2=CNNoutput_test(:,end)';
CNNoutputnew_test=[otest1 otest2];
output_test=testdata(end-nntest+1:end)';
%% 误差评价
%--------------------------------------------------------------------------
CNNerror_test=CNNoutput_test'-output_test';
CNNpererror_test=CNNerror_test./output_test';
CNNpererror_test=filloutliers(CNNpererror_test,'spline');
CNNpererror_test(abs(CNNpererror_test)>1)=0;
% R
Rtest21=sum((output_test -mean(output_test)).*( CNNoutput_test - mean(CNNoutput_test)));
Rtest22=sqrt(sumsqr(output_test -mean(output_test)).*sumsqr(CNNoutput_test - mean(CNNoutput_test)));
Rtest = Rtest21/Rtest22;
% RMSE
RMSEtest = sqrt(sumsqr(CNNerror_test)/length(output_test));
% MAPE
MAPEtest = mean(abs(CNNpererror_test));
disp("——————CNN网络模型测试数据——————————")
disp(" 预测值 真实值 误差 ")
disp([CNNoutput_test' output_test' CNNerror_test])
%--------------------------------------------------------------------------
disp('CNN测试相关系数R2');
disp(Rtest)
disp('CNN测试平均绝对误差百分比MAPE');
disp(MAPEtest)
disp('CNN测试均方根误差RMSE')
disp(RMSEtest)
%--------------------------------------------------------------------------
%% 数据可视化
%-------------------------------------------------------------------------------------
figure()
plot(CNNoutput_test,'r:.')
hold on
plot(output_test,'k-')
legend( '测试数据','实际数据','Location','NorthWest','FontName','华文宋体');
title('CNN模型测试结果及真实值','fontsize',12,'FontName','华文宋体')
xlabel('时间','fontsize',12,'FontName','华文宋体');
ylabel('数值','fontsize',12,'FontName','华文宋体');
axis tight
%-------------------------------------------------------------------------------------
figure()
stem(CNNerror_test,'filled','Color',[128 0 0]./255,'linewidth',1,'Markersize',3)
legend('CNN模型测试误差','Location','NorthEast','FontName','华文宋体')
title('CNN模型测试误差','fontsize',12,'FontName','华文宋体')
ylabel('误差','fontsize',12,'FontName','华文宋体')
xlabel('样本','fontsize',12,'FontName','华文宋体')
axis tight
%-------------------------------------------------------------------------------------
figure()
stairs(CNNpererror_test,'-','Color',[128 0 255]./255,'linewidth',1)
legend('CNN模型测试相对误差','Location','NorthEast','FontName','华文宋体')
title('CNN模型测试相对误差','fontsize',12,'FontName','华文宋体')
ylabel('误差','fontsize',12,'FontName','华文宋体')
xlabel('样本','fontsize',12,'FontName','华文宋体')
axis tight
%-------------------------------------------------------------------------------------
- 1
- 2
- 3
- 4
- 5
- 6
前往页