这部分主要是参考教程后写的代码,比较简单易懂
clearclcclose allif strcmpi(computer, 'PCWIN') | strcmpi(computer, 'PCWIN64')compile_windowselsecompile_linuxendtotalTrainTime = 0;totalTestTime = 0;%---------------------载入数据--------------------bedroom = load('C:\Users\*****\Desktop\数据集\bedroom.mat');bedroomIndex = fieldnames(bedroom);bedroom = bedroom.(bedroomIndex{1});forest = load('C:\Users\*****\Desktop\数据集\forest.mat');forestIndex = fieldnames(forest);forest = forest.(forestIndex{1});label = load('C:\Users\*****\Desktop\数据集\labelset.mat');labelIndex = fieldnames(label);label = label.(labelIndex{1});%-------------将数据混合并且打乱-------------xData = [bedroom; forest];[N, D] = size(xData);randVector = randperm(N);xTrn = xData(randVector(1:15), :);yTrn = label(randVector(1:15));xTst = xData(randVector(16:end), :);yTst = label(randVector(16:end));%------------训练数据集---------------model = classRF_train(xTrn, yTrn, 500, 2);yHat = classRF_predict(xTst, model);fprintf('\n 错误率%f\n', length(find(yHat ~= yTst))/length(yTst));%------------绘出训练错误曲线------------figure('Name','OOB error rate');plot(model.errtr(:,1)); title('OOB error rate');xlabel('iteration (# trees)'); ylabel('OOB error rate');%{因为随机森林属于bagging类的集成学习器,所以最后结果是投票决定的,这个错误曲线是每个基学习器的分类错误曲线%}
