In MATLAB, Decision Forests go under the rather deceiving name of TreeBagger.

随机森林分类器(Random Forest)

B = TreeBagger(nTree,train_data,train_label,'Method','classification');
predict_label = predict(B,test_data);

利用随机森林做分类

Here’s a quick tutorial on how to do classification with the TreeBagger class in MATLAB.

% Since TreeBagger uses randomness we will get different results each
% time we run this.
% This makes sure we get the same results every time we run the code.
rng default% Here we create some training data.
% The rows< represent the samples or individuals.
% The first two columns represent the individual's features.
% The last column represents the class label (what we want to predict)
trainData = [ ...[6,  300,  1];[3,  300,  0];[8,  300,  1];[11, 2000, 0];[3,  100,  0];[6,  1000, 0];];features = trainData(:,(1:2))
classLabels = trainData(:,3)% How many trees do you want in the forest?
nTrees = 20;% Train the TreeBagger (Decision Forest).
B = TreeBagger(nTrees,features,classLabels, 'Method', 'classification');% Given a new individual WITH the features and WITHOUT the class label,
% what should the class label be?
newData1 = [7, 300];% Use the trained Decision Forest.
predChar1 = B.predict(newData1);% Predictions is a char though. We want it to be a number.
predictedClass = str2double(predChar1)
% predictedClass =
%      1% So we predict that for our new piece of data, we will have a class label of 1 % Okay let's try another piece of data.
newData2 = [7, 1500];predChar2 = B.predict(newData2);
predictedClass2 = str2double(predChar2)
% predictedClass2 =
%      0% It predicts that the new class label is a 0.

Found out how to inspect the trees, by running the view() command. E.g. for inspecting the first tree of the example:

view(B.Trees{1})Decision tree for classification
1  if x2<650 then node 2 elseif x2>=650 then node 3 else 0
2  if x1<4.5 then node 4 elseif x1>=4.5 then node 5 else 1
3  class = 0
4  class = 0
5  class = 1

By passing some more arguments to the view() command, the tree can also be visualized:

view(B.Trees{1},'mode','graph')

利用随机森林进行回归:

x=[1:1:30];
y=x.^2;
B= TreeBagger(100,x',y','Method','regression');
x2=[1:0.5:40];
y2=x2.^2;
y3=zeros(size(x2));
for i=1:size(x2,2)  y3(i)=B.predict(x2(i));
end
plot(x2,y2,'.r');
hold on;
plot(x2,y3,'.b');
title('Random Forest for Regression');

There’s an excellent tutorial in the MATLAB documentation here that covers a lot more.

本文转自:

http://kawahara.ca/matlab-treebagger-example/

http://blog.csdn.net/dan1900/article/details/39030867

MATLAB – TreeBagger example相关推荐

  1. Matlab TreeBagger随机森林分类实例

    例子 clc; clear all; close all;rng(6,'twister')% 载入数据,花的数据 load fisheriris% 随机划分训练数据和验证数据 index = logi ...

  2. 毕设论文数据分析记录-part3:各变量因子的相对贡献程度

    20220311-了解随机森林回归模型.多元非线性回归 Matlab TreeBagger随机森林回归实例_wokaowokaowokao12345的专栏-CSDN博客_matlab treebagg ...

  3. Matlab之随机森林TreeBagger

    MATLAB之随机森林TreeBagger TreeBagger 1 方法: 2 属性: TreeBagger TreeBagger用来创建一个袋装决策树的集合. 1 方法: append compa ...

  4. MATLAB中的分类器

    目前了解到的 MATLAB 中分类器有: K 近邻分类器,随机森林分类器,朴素贝叶斯,集成学习方法,鉴别分析分类器,支持向量机.现将其主要函数使用方法总结如下,更多细节需参考 MATLAB  帮助文件 ...

  5. matlab分类器函数

    train_data是训练特征数据, train_label是分类标签. Predict_label是预测的标签. MatLab训练数据, 得到语义标签向量 Scores(概率输出). 1.逻辑回归( ...

  6. matlab中的分类器使用小结(SVM、KNN、RF、AdaBoost、Naive Bayes、DAC)

    1.前言 目前了解到的MATLAB分类器有:K近邻,随机森林,朴素贝叶斯,集成学习方法,鉴别分析,支持向量机.现将其主要函数使用方法总结如下,更多细节需参考MATLAB 帮助文件. 设: 训练样本:t ...

  7. python分类器knn、svm_[转载]MatLab分类器大全(svm,knn,随机森林等)

    train_data是训练特征数据, train_label是分类标签. Predict_label是预测的标签. MatLab训练数据, 得到语义标签向量 Scores(概率输出). 1.逻辑回归( ...

  8. MATLAB 在图像处理和机器视觉的应用举例01 - 官网培训视频笔记(下)分类/灰度共生矩阵/纹理分类学习

    前言: 本节继续讨论Matlab的机器视觉工具集举例,这次为分类的综合实现:该例子,用到了图像处理,统计,并行计算等方法. 1 分类的难度: [计算机视觉里面,分类的精髓在选取适当的数据集和算法,这一 ...

  9. matlab决策树模型过程,利用MATLAB统计工具箱进行决策树分类的一个例子

    这个例子开始从lda线性分类算法,最后引出决策树分类算法,不错,初学者可以参考下 网上的很多决策树算法都没有例子,都是就一堆代码都不知道参数怎么传递.直接用工具箱里面的决策树算法,不懂得就help一下 ...

最新文章

  1. WMI技术介绍和应用——查询驱动信息
  2. 【c语言】蓝桥杯算法训练 sign函数
  3. c/c++左值和右值
  4. python怎么读取txt文件内容然后保存到excel-Python实现读取txt文件并转换为excel的方法示例...
  5. 磁盘文件读写和数据库读写哪个效率更高
  6. java怎么更改id名_java - 尽管ID已更改为_id,但列'_id'不存在
  7. iOS逆向工程——非越狱调试
  8. 【lucene】Lucene 自定义 Parser
  9. 计算机安全日志,如何回复被删除的电脑安全日志
  10. 再获数千万融资,湃方科技将工业智联革命进行到底
  11. linux虚拟化技术 教程,Linux上实现虚拟化技术的优势
  12. Angular之constructor和ngOnInit差异及适用场景
  13. 【预测模型】基于matlab模糊小波神经网络目标威胁评估【含Matlab源码 1621期】
  14. 使用pycharm创建Django项目
  15. 几何画板椭圆九种画法_详解椭圆的五种画法,很全面!!!
  16. 遇到的问题集合(倒序)
  17. sqli-labs(38-41)
  18. 小程序添加插屏广告教程
  19. 计算机辅助翻译优缺点,计算机辅助翻译优缺点
  20. 关于java.lang.ArithmeticException

热门文章

  1. linux配置ip地址 suse_suse linux中为单网卡配置多IP的方法
  2. 西华大学计算机学院陈鹏,中国计算机学会CCF服务计算专委会走进西华大学
  3. [蓝桥杯][算法提高VIP]学霸的迷宫(bfs+dfs)
  4. Food Buying CodeForces - 1296B
  5. qdu_ACM集训队3月5号组队训练
  6. head在linux命令中什么意思,linux系统中head命令使用说明
  7. matlab八节点六面体程序,平面8节点等参元完整程序
  8. java不用析构函数,堆栈分配的类--C发生不需要的析构函数调用
  9. access denied for_abm怎么样?ACCESS集团携8大国际品牌在进博会首秀,展示abmr 硬核实力!...
  10. 深度学习之循环神经网络(12)预训练的词向量