日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MATLAB – TreeBagger example

發(fā)布時間:2023/12/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MATLAB – TreeBagger example 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。