这部分主要是参考教程后写的代码,比较简单易懂

    1. clear
    2. clc
    3. close all
    4. if strcmpi(computer, 'PCWIN') | strcmpi(computer, 'PCWIN64')
    5. compile_windows
    6. else
    7. compile_linux
    8. end
    9. totalTrainTime = 0;
    10. totalTestTime = 0;
    11. %---------------------载入数据--------------------
    12. bedroom = load('C:\Users\*****\Desktop\数据集\bedroom.mat');
    13. bedroomIndex = fieldnames(bedroom);
    14. bedroom = bedroom.(bedroomIndex{1});
    15. forest = load('C:\Users\*****\Desktop\数据集\forest.mat');
    16. forestIndex = fieldnames(forest);
    17. forest = forest.(forestIndex{1});
    18. label = load('C:\Users\*****\Desktop\数据集\labelset.mat');
    19. labelIndex = fieldnames(label);
    20. label = label.(labelIndex{1});
    21. %-------------将数据混合并且打乱-------------
    22. xData = [bedroom; forest];
    23. [N, D] = size(xData);
    24. randVector = randperm(N);
    25. xTrn = xData(randVector(1:15), :);
    26. yTrn = label(randVector(1:15));
    27. xTst = xData(randVector(16:end), :);
    28. yTst = label(randVector(16:end));
    29. %------------训练数据集---------------
    30. model = classRF_train(xTrn, yTrn, 500, 2);
    31. yHat = classRF_predict(xTst, model);
    32. fprintf('\n 错误率%f\n', length(find(yHat ~= yTst))/length(yTst));
    33. %------------绘出训练错误曲线------------
    34. figure('Name','OOB error rate');
    35. plot(model.errtr(:,1)); title('OOB error rate');
    36. xlabel('iteration (# trees)'); ylabel('OOB error rate');
    37. %{因为随机森林属于bagging类的集成学习器,所以最后结果是投票决定的,
    38. 这个错误曲线是每个基学习器的分类错误曲线%}

    数据集.zip