%% 清空环境变量warning off % 关闭报警信息close all % 关闭开启的图窗clear % 清空变量clc % 清空命令行%% 导入数据res = xlsread('数据集.xlsx');%% 划分训练集和测试集temp = randperm(103);P_train = res(temp(1: 80), 1: 7)';T_train = res(temp(1: 80), 8)';M = size(P_train, 2);P_test = res(temp(81: end), 1: 7)';T_test = res(temp(81: end), 8)';N = size(P_test, 2);%% 数据归一化[p_train, ps_input] = mapminmax(P_train, 0, 1);p_test = mapminmax('apply', P_test, ps_input);[t_train, ps_output] = mapminmax(T_train, 0, 1);t_test = mapminmax('apply', T_test, ps_output);%% 转置以适应模型p_train = p_train'; p_test = p_test';t_train = t_train'; t_test = t_test';%% 创建模型c = 4.0; % 惩罚因子g = 0.8; % 径向基函数参数cmd = [' -t 2',' -c ',num2str(c),' -g ',num2str(g),' -s 3 -p 0.01'];model = svmtrain(t_train, p_train, cmd);%% 仿真预测[t_sim1, error_1] = svmpredict(t_train, p_train, model);[t_sim2, error_2] = svmpredict(t_test , p_test , model);%% 数据反归一化T_sim1 = mapminmax('reverse', t_sim1, ps_output);T_sim2 = mapminmax('reverse', t_sim2, ps_output);%% 均方根误差error1 = sqrt(sum((T_sim1' - T_train).^2) ./ M);error2 = sqrt(sum((T_sim2' - T_test ).^2) ./ N);%% 绘图figureplot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)legend('真实值', '预测值')xlabel('预测样本')ylabel('预测结果')string = {'训练集预测结果对比'; ['RMSE=' num2str(error1)]};title(string)xlim([1, M])gridfigureplot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)legend('真实值', '预测值')xlabel('预测样本')ylabel('预测结果')string = {'测试集预测结果对比'; ['RMSE=' num2str(error2)]};title(string)xlim([1, N])grid%% 相关指标计算% R2R1 = 1 - norm(T_train - T_sim1')^2 / norm(T_train - mean(T_train))^2;R2 = 1 - norm(T_test - T_sim2')^2 / norm(T_test - mean(T_test ))^2;disp(['训练集数据的R2为:', num2str(R1)])disp(['测试集数据的R2为:', num2str(R2)])% MAEmae1 = sum(abs(T_sim1' - T_train)) ./ M ;mae2 = sum(abs(T_sim2' - T_test )) ./ N ;disp(['训练集数据的MAE为:', num2str(mae1)])disp(['测试集数据的MAE为:', num2str(mae2)])% MBEmbe1 = sum(T_sim1' - T_train) ./ M ;mbe2 = sum(T_sim2' - T_test ) ./ N ;disp(['训练集数据的MBE为:', num2str(mbe1)])disp(['测试集数据的MBE为:', num2str(mbe2)])%% 绘制散点图sz = 25;c = 'b';figurescatter(T_train, T_sim1, sz, c)hold onplot(xlim, ylim, '--k')xlabel('训练集真实值');ylabel('训练集预测值');xlim([min(T_train) max(T_train)])ylim([min(T_sim1) max(T_sim1)])title('训练集预测值 vs. 训练集真实值')figurescatter(T_test, T_sim2, sz, c)hold onplot(xlim, ylim, '--k')xlabel('测试集真实值');ylabel('测试集预测值');xlim([min(T_test) max(T_test)])ylim([min(T_sim2) max(T_sim2)])title('测试集预测值 vs. 测试集真实值')