数据连接:提取码:dzdfhttp://链接:https://pan.baidu.com/s/1eAGh9HqWfxokC7uFDr59Ag
clc;clear all;close all;
%加载数据
f1 = importdata('D:\桌面\附件1.xlsx');
a=f1.textdata;
c1=f1.data;
Y = c1(:,end);
X = c1(:,1:end-1);
isCategorical = [zeros(15,1);ones(size(X,2)-15,1)]; % Categorical variable flag
leaf =5;
ntrees = 200;
fboot = 1;
disp('训练随机森林(TreeBagger)')
b = TreeBagger(ntrees, X,Y, 'Method','regression', 'oobvarimp','on', 'surrogate', 'on', 'minleaf',leaf,'FBoot',fboot);
%训练好的模型进行预测
disp('预测')
x = Y;
y1 = predict(b, X);
% 计算相关系数
x1=x(1:30000);
y11=y1(1:30000);
cct=corrcoef(x1,y11);
cct=cct(2,1);
% 创建散点图
disp('创建散点图')
% 绘制线
plot(x1,x1,'LineWidth',4);
hold on
scatter(x1,y11,'filled');
hold off
grid on
%创建R方图
set(gca,'FontSize',20)
xlabel('真实值','FontSize',20)
ylabel('预测值','FontSize',20)
title(['Training Dataset, R^2=' num2str(cct^2,2)],'FontSize',20)
drawnow
% Calculate the relative importance of the input variables
tic
disp('Sorting importance into descending order')
weights=b.OOBPermutedVarDeltaError;
[B,iranked] = sort(weights,'descend');
toc
disp(['Plotting a horizontal bar graph of sorted labeled weights.'])
figure
barh(weights(iranked),'g');
xlabel('Variable Importance','FontSize',20,'Interpreter','latex');
ylabel('Variable Rank','FontSize',20,'Interpreter','latex');
title(...
['Relative Importance of Inputs in estimating Redshift'],...
'FontSize',10,'Interpreter','latex'...
);
hold on
barh(weights(iranked(1:10)),'y');
barh(weights(iranked(1:5)),'r');
xt = get(gca,'XTick');
xt_spacing=unique(diff(xt));
xt_spacing=xt_spacing(1);
yt = get(gca,'YTick');
ylim([0.25 length(weights)+0.75]);
xl=xlim;
xlim([0 2.5*max(weights)]);
% 为每个栏添加文本标签
for ii=1:length(weights)
text(...
max([0 weights(iranked(ii))+0.05*max(weights)]),ii,...
['Column ' num2str(iranked(ii))],'Interpreter','latex','FontSize',11);
end
set(gca,'FontSize',16)
set(gca,'XTick',0:2*xt_spacing:1.1*max(xl));
set(gca,'YTick',yt);
set(gca,'TickDir','out');
set(gca, 'ydir', 'reverse' )
set(gca,'LineWidth',2);
drawnow
fn='RelativeImportanceInputs';
fnpng=[fn,'.png'];
print('-dpng',fnpng);
% 绘制权重等级变化
disp('绘制bag与生成树的关系')
figure
plot(b.oobError,'LineWidth',3);
xlabel('Number of Trees','FontSize',20)
ylabel('Out of Bag Error','FontSize',20)
title('Out of Bag Error','FontSize',20)
set(gca,'FontSize',16)
set(gca,'LineWidth',2);
grid on
drawnow
fn='EroorAsFunctionOfForestSize';
fnpng=[fn,'.png'];
print('-dpng',fnpng);
%模型评测
y2=y1(1:30000);
Y1=Y(1:30000);
Ape=abs(y2-Y1)./Y1;
Mape1=0;
[r,c]=size(Ape);
for i=1:r
Mape1=Mape1+Ape(i,c);
end
disp('平均相对误差Mape:')
Mape=Mape1/r
count1=sum(Ape(:)<=0.05)
disp('5%标准误差率')
Accuracy=count1/30000
disp('模型评测')
zzz=0.2*(1-Mape)+0.8*Accuracy
?
|