% 载入数据 data = xlsread('补偿.xlsx'); input = data(1:20, 7:12)'; % 输入数据 output = data(1:20, 2:4)'; % 输出数据 % 分割训练集和测试集 input_train = input(:, 1:15); output_train = output(:, 1:15); input_test = input(:, 5:20); output_test = output(:, 5:20); % 归一化数据 [input_train_norm, input_ps] = mapminmax(input_train, -1, 1); [output_train_norm, output_ps] = mapminmax(output_train, -1, 1); % 构建BP神经网络 input_num = size(input_train_norm, 1); hidden_num = 10; output_num = size(output_train_norm, 1); net = newff(input_train_norm, output_train_norm, hidden_num, {'tansig', 'purelin'}, 'trainlm'); % 训练BP神经网络 net.trainParam.epochs = 2000; net.trainParam.lr = 0.0001; net.trainParam.goal = 0.001; net = train(net, input_train_norm, output_train_norm); % 测试BP神经网络 input_test_norm = mapminmax('apply', input_test, input_ps); output_test_norm = mapminmax('apply', output_test, output_ps); output_pred_norm = sim(net, input_test_norm); output_pred = mapminmax('reverse', output_pred_norm, output_ps); % 可视化结果 figure; plot(output_test(1,:), 'bo-'); hold on; plot(output_pred(1,:), 'r*-'); legend('真实结果', '预测结果'); xlabel('样本编号'); ylabel('输出值'); title('预测结果和真实结果');样本编号帮我改为1到20
时间: 2023-12-22 15:05:21 浏览: 367
matlab导入excel数据教程 [number,txt,raw]=xlsread('noise xlsx')
5星 · 资源好评率100%
% 载入数据
data = xlsread('补偿.xlsx');
input = data(1:20, 7:12)'; % 输入数据
output = data(1:20, 2:4)'; % 输出数据
% 分割训练集和测试集
input_train = input(:, 1:15);
output_train = output(:, 1:15);
input_test = input(:, 5:20);
output_test = output(:, 5:20);
% 归一化数据
[input_train_norm, input_ps] = mapminmax(input_train, -1, 1);
[output_train_norm, output_ps] = mapminmax(output_train, -1, 1);
% 构建BP神经网络
input_num = size(input_train_norm, 1);
hidden_num = 10;
output_num = size(output_train_norm, 1);
net = newff(input_train_norm, output_train_norm, hidden_num, {'tansig', 'purelin'}, 'trainlm');
% 训练BP神经网络
net.trainParam.epochs = 2000;
net.trainParam.lr = 0.0001;
net.trainParam.goal = 0.001;
net = train(net, input_train_norm, output_train_norm);
% 测试BP神经网络
input_test_norm = mapminmax('apply', input_test, input_ps);
output_test_norm = mapminmax('apply', output_test, output_ps);
output_pred_norm = sim(net, input_test_norm);
output_pred = mapminmax('reverse', output_pred_norm, output_ps);
% 可视化结果
figure;
plot(1:20, output_test(1,:), 'bo-');
hold on;
plot(1:20, output_pred(1,:), 'r*-');
legend('真实结果', '预测结果');
xlabel('样本编号');
ylabel('输出值');
title('预测结果和真实结果');
阅读全文