%% 数据输入模块(保持不变)
historical_emission = [9682.74,9989.63,10773.99,11117.61,11137.42,11128.88,11495.02,11951.23,12049.60,12355.79,12925.21,13234.42];
historical_factors = [
59720000 27303 0.6046 0.6491
59780000 30697 0.5596 0.6192
59880000 34404 0.5211 0.5682
59970000 37580 0.5044 0.5334
60110000 39692 0.4985 0.5162
60330000 43682 0.4780 0.4813
60570000 49092 0.4385 0.4387
60760000 56063 0.4001 0.3909
60920000 60561 0.3876 0.3764
61047617 62411 0.3976 0.3862
61130000 69676 0.3745 0.3605
61270000 72888 0.3477 0.3526
];
future_factors = [
61496981 50549 0.3340 0.3112
61660973 55287 0.3189 0.2929
61825402 60469 0.3044 0.2756
61990270 66137 0.2906 0.2594
62155577 72336 0.2774 0.2441
62321325 79116 0.2648 0.2297
];
%% 数据预处理(关键修改)
% 分割数据为训练集(前9年)和测试集(后3年)
train_idx = 1:9; % 训练数据索引
test_idx = 10:12; % 测试数据索引
% 仅用训练集计算归一化参数
[input_norm_train, input_ps] = mapminmax(historical_factors(train_idx, :)‘, 0, 1);
[target_norm_train, target_ps] = mapminmax(historical_emission(train_idx)’, 0, 1);
% 测试集应用训练集的归一化参数
input_norm_test = mapminmax(‘apply’, historical_factors(test_idx, :)‘, input_ps);
target_norm_test = mapminmax(‘apply’, historical_emission(test_idx)’, target_ps);
%% BP神经网络构建(改进训练算法)
net = feedforwardnet(10, ‘trainbr’); % 贝叶斯正则化算法
net.trainParam.epochs = 1000;
net.trainParam.goal = 1e-5;
net.trainParam.max_fail = 20; % 早停:连续20次验证误差不下降则停止
%% 模型训练(使用训练集)
net = train(net, input_norm_train, target_norm_train);
%% 模型验证(使用独立测试集)
test_pred_norm = net(input_norm_test);
test_pred = mapminmax(‘reverse’, test_pred_norm, target_ps)';
%% 未来预测(保持不变)
future_norm = mapminmax(‘apply’, future_factors’, input_ps);
pred_norm = net(future_norm);
pred_emission = mapminmax(‘reverse’, pred_norm, target_ps)';
%% 增强可视化
years_hist = 2011:2022;
years_pred = 2023:2028;
% 主趋势图
figure;
plot(years_hist, historical_emission, ‘b-o’, ‘LineWidth’, 2, ‘MarkerSize’, 8);
hold on;
plot(years_pred, pred_emission, ‘r–*’, ‘LineWidth’, 2, ‘MarkerSize’, 10);
plot(years_hist(test_idx), test_pred, ‘g-s’, ‘LineWidth’, 2, ‘MarkerSize’, 8); % 测试集预测
xlabel(‘年份’); ylabel(‘碳排放量(万吨)’);
legend(‘历史数据’, ‘未来预测’, ‘测试集预测’, ‘Location’, ‘northwest’);
title(‘碳排放量预测结果’);
grid on;
% 添加数值标签
text(years_hist, historical_emission, num2str(historical_emission’, ‘%.1f’),…
‘VerticalAlignment’,‘bottom’, ‘HorizontalAlignment’,‘right’, ‘FontSize’,8);
text(years_pred, pred_emission, num2str(pred_emission’, ‘%.1f’),…
‘VerticalAlignment’,‘bottom’, ‘HorizontalAlignment’,‘left’, ‘FontSize’,8);
% 预测误差分析图
figure;
scatter(historical_emission(test_idx), test_pred, 100, ‘filled’);
hold on;
plot([min(historical_emission), max(historical_emission)],…
[min(historical_emission), max(historical_emission)], ‘k–’);
xlabel(‘实际值’); ylabel(‘预测值’);
title(‘测试集预测精度散点图’);
grid on;
%% 综合评估指标(新增R²和MAE)
% 计算误差
errors = test_pred - historical_emission(test_idx);
% MSE
mse = mean(errors.^2);
% MAE
mae = mean(abs(errors));
% RMSE
rmse = sqrt(mse);
% R²
SS_res = sum(errors.^2);
SS_tot = sum((historical_emission(test_idx) - mean(historical_emission(test_idx))).^2);
R2 = 1 - (SS_res / SS_tot);
% 输出结果
fprintf(‘----- 模型评估结果 -----\n’);
fprintf(‘均方误差 (MSE) : %.2f 万吨²\n’, mse);
fprintf(‘平均绝对误差 (MAE) : %.2f 万吨\n’, mae);
fprintf(‘均方根误差 (RMSE) : %.2f 万吨\n’, rmse);
fprintf(‘决定系数 (R²) : %.4f\n’, R2); 修改代码,使数据一开始就在代码中,不需要用户输入,并生成完整代码