📄 usec45.m
字号:
%UseC45.m
%Shiliang Sun (shiliangsun@gmail.com), Apr. 9, 2007
%Show how to use the C4.5 algorithm to train a decision tree and classify
%test samples.
%This code is based on the C4_5.m file from "Classification Toolbox for Matlab"
%(http://www.yom-tov.info/cgi-bin/list_uploaded_files.pl). Several bugs have been fixed.
clear;
load DataForC45;
train_patterns=Traindata';
train_patterns=train_patterns(1:4,:); train_targets=Traindata(:,end)';
test_patterns=[
7 1 1 1 2
7 1 2 2 1
7 3 1 2 2];
test_patterns=test_patterns(:,1:4)';
%================================================C4.5 decision tree
% Inputs:
% training_patterns - Train patterns, (the number of features) * (the number of samples)
% training_targets - Train targets, 1*(the number of samples)
% test_patterns - Test patterns, (the number of features) * (the number of samples)
% Fra_inc_node - Fraction of incorrectly assigned samples at a
% node. To avoid overfitting.
% Outputs
% test_targets - Predicted targets, 1*(the number of samples)
Fra_inc_node=0;
%NOTE: In this implementation it is assumed that a pattern vector with fewer than 10 unique values (the parameter Nu)
%is discrete, and will be treated as such. Other vectors will be treated as continuous
[Ni, M] = size(train_patterns);
inc_node = Fra_inc_node*M; %Stop building a subtree if the total samples are less than inc_node.
Nu = 10;
%Find which of the input patterns are discrete, and discretisize the corresponding
%dimension on the test patterns
discrete_dim = zeros(1,Ni);
for i = 1:Ni,
Ub = unique(train_patterns(i,:));
Nb = length(Ub);
if (Nb <= Nu),
%This is a discrete pattern
discrete_dim(i) = Nb;
dist = abs(ones(Nb ,1)*test_patterns(i,:) - Ub'*ones(1, size(test_patterns,2)));
[m, in] = min(dist);
test_patterns(i,:) = Ub(in);
end
end
%=======================Build the tree recursively
disp('Building a tree...')
tree = C4_5TrainFun(train_patterns, train_targets, inc_node, discrete_dim);
%======================Classify test samples
disp('Classify test samples using the tree...')
test_targets = C4_5TestFun(test_patterns, tree, discrete_dim);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -