MATLAB随机森林回归模型

时间:2024-11-16 08:05:49

MATLAB随机森林回归模型:

调用matlab自带的TreeBagger.m

T=textread('E:\datasets-orreview\discretized-regression\10bins\abalone10\matlab\test_abalone10.2');
X=textread('E:\datasets-orreview\discretized-regression\10bins\abalone10\matlab\train_abalone10.2');
%nTree = round(sqrt(size(X,2)-1));
nTree = 50;
train_data = X(:,1:end-1);train_label = X(:,end); test_data = T(:,1:end-1);
Factor = TreeBagger(nTree, train_data, train_label,'Method','regression');
[Predict_label,Scores] = predict(Factor, test_data);
%Predict_label=cellfun(@str2num,Predict_label(1:end));
MZE = mean(round(Predict_label) ~= T(:,end))
MAE = mean(abs(round(Predict_label) - T(:,end)))

 

调用外部函数forestTrain.m来自https://github.com/karpathy/Random-Forest-Matlab

T=textread('E:\datasets-orreview\ordinal-regression\ERA\matlab\test_ERA.1');
X=textread('E:\datasets-orreview\ordinal-regression\ERA\matlab\train_ERA.1');
opts= struct;
opts.depth= 9;
opts.numTrees= 60;
opts.numSplits= 5;
opts.verbose= true;
opts.classifierID= 2; % weak learners to use. Can be an array for mix of weak learners too
train_data = X(:,1:end-1);train_label = X(:,end); test_data = T(:,1:end-1);
tic;
m= forestTrain(train_data, train_label, opts);
timetrain= toc;
tic;
yhatTrain = forestTest(m, test_data);
timetest= toc;
MZE = mean(round(yhatTrain) ~= T(:,end))
MAE = mean(abs(round(yhatTrain) - T(:,end)))