利用随机森林进行分类:
% 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