matlab实现的C4.5分类决策树算法
function D = C4_5(train_features, train_targets, inc_node, region)
% Classify using Quinlan's C4.5 algorithm
% Inputs:
% features - Train features
% targets - Train targets
% inc_node - Percentage of incorrectly
assigned samples at a node
% region - Decision region vector: [-x x -y y number_of_points]
%
% Outputs
% D - Decision sufrace
%NOTE: In this implementation it is assumed that a feature 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_features);
inc_node = inc_node*M/100;
Nu = 10;
%For the decision region
N = region(5);
mx = ones(N,1) * linspace
(region(1),region(2),N);
my = linspace (region(3),region(4),N)' * ones(1,N);
flatxy = [mx(:), my(:)]';
%Preprocessing
%[f, t, UW, m] = PCA(train_features,
train_targets, Ni, region);
%train_features = UW * (train_features -
m*ones(1,M));;
%flatxy = UW * (flatxy - m*ones(1,N^2));;
%Find which of the input features are discrete, and discretisize the corresponding
%dimension on the decision region
discrete_dim = zeros(1,Ni);
for i = 1:Ni,
Nb = length(unique(train_features(i,:)));
if (Nb <= Nu),
%This is a discrete feature
discrete_dim(i) = Nb;
[H, flatxy(i,:)] =
high_histogram(flatxy(i,:), Nb);
end
end
%Build the tree recursively
disp('Building tree')
tree = make_tree(train_features,
train_targets, inc_node, discrete_dim,
max(discrete_dim), 0);
%Make the decision region according to the tree disp('Building decision surface using the tree') targets = use_tree(flatxy, 1:N^2, tree,
discrete_dim, unique(train_targets));
D = reshape(targets,N,N);
%END
function targets = use_tree(features, indices,
tree, discrete_dim, Uc)
%Classify recursively using a tree
targets = zeros(1, size(features,2));
if (tree.dim == 0)
%Reached the end of the tree
targets(indices) = tree.child;
break
end
%This is not the last level of the tree, so:
%First, find the dimension we are to work on
dim = tree.dim;
dims= 1:size(features,1);
%And classify according to it
if (discrete_dim(dim) == 0),
%Continuous feature
in = indices(find(features(dim,
indices) <= tree.split_loc));
targets = targets +
use_tree(features(dims, :), in, tree.child(1),
discrete_dim(dims), Uc);
in = indices(find(features(dim,
indices) > tree.split_loc));
targets = targets +
use_tree(features(dims, :), in, tree.child(2),
discrete_dim(dims), Uc);
else
%Discrete feature
Uf = unique(features(dim,:));
for i = 1:length(Uf),
in = indices(find(features(dim,
indices) == Uf(i)));
targets = targets +
use_tree(features(dims, :), in, tree.child(i),
discrete_dim(dims), Uc);
end
end
%END use_tree
function tree = make_tree(features, targets,
inc_node, discrete_dim, maxNbin, base)
%Build a tree recursively
[Ni, L] = size(features);
Uc = unique(targets);
tree.dim = 0;
%tree.child(1:maxNbin) = zeros(1,maxNbin);
tree.split_loc = inf;
if isempty(features),
break
end
%When to stop: If the dimension is one or the number of examples is small
if ((inc_node > L) | (L == 1) | (length(Uc) == 1)), H = hist(targets, length(Uc));
[m, largest] = max(H);
tree.child = Uc(largest);
break
end
%Compute the node's I
for i = 1:length(Uc),
Pnode(i) = length(find(targets == Uc(i))) / L; end
Inode = -sum(Pnode.*log(Pnode)/log(2));
%For each dimension, compute the gain ratio impurity %This is done separately for discrete and continuous features
delta_Ib = zeros(1, Ni);
split_loc = ones(1, Ni)*inf;
for i = 1:Ni,
data = features(i,:);
Nbins = length(unique(data));
if (discrete_dim(i)),
%This is a discrete feature
P = zeros(length(Uc), Nbins);
for j = 1:length(Uc),
for k = 1:Nbins,
indices = find((targets == Uc(j)) & (features(i,:) == k));
P(j,k) = length(indices);
end
end
Pk = sum(P);
P = P/L;
Pk = Pk/sum(Pk);
info = sum(-P.*log(eps+P)/log(2));
delta_Ib(i) =
(Inode-sum(Pk.*info))/-sum(Pk.*log(eps+Pk)/log(
2));
else
%This is a continuous feature
P = zeros(length(Uc), 2);
%Sort the features
[sorted_data, indices] = sort(data);
sorted_targets = targets(indices);
%Calculate the information for each possible split
I = zeros(1, L-1);
for j = 1:L-1,
for k =1:length(Uc),
P(k,1) = length(find(sorted_targets(1:j) == Uc(k)));
P(k,2) =
length(find(sorted_targets(j+1:end) == Uc(k))); end
Ps = sum(P)/L;
P = P/L;
info = sum(-P.*log(eps+P)/log(2));
I(j) = Inode - sum(info.*Ps);
end
[delta_Ib(i), s] = max(I);
split_loc(i) = sorted_data(s); end
end
%Find the dimension minimizing delta_Ib
[m, dim] = max(delta_Ib);
dims = 1:Ni;
tree.dim = dim;
相关推荐:
- [实用文档]李践-有效提升销售的12大黄金法则8-大
- [实用文档]党支部换届工作方案
- [实用文档]2013年下期电子商务专业部宣传工作计划
- [实用文档]方庄一矿通风、钻探绩效工资考核管理办
- [实用文档]项目一 认识企业物流认识企业物流
- [实用文档]MBI_Display_产品蓝图规画
- [实用文档]北京市建筑业劳务作业人员普法维权培训
- [实用文档]锅炉燃烧调整与运行优化
- [实用文档]4支付结算业务的核算
- [实用文档]米什金_货币金融学_第9版各章学习指导
- [实用文档]水泥混凝土路面硬化工程施工组织设计
- [实用文档]钢筋工程安全技术交底书
- [实用文档]关于公布华中师范大学本科毕业论文
- [实用文档]太原市园林绿化施工合同范本 2
- [实用文档]周日辅导 初中英语分类复习单项选择题(
- [实用文档]第四章 文化经纪人的管理形式 第二节
- [实用文档]学宪法讲宪法竞赛题库
- [实用文档]《数值计算方法》期末考试模拟试题二
- [实用文档]爱词霸学英语:每日一句( 十月)
- [实用文档]2014年国家公务员面试:无领导小组讨论
- 新课程主要理念和教学案例分析汇编(24
- 英国人的快乐源于幸福的家庭生活
- 七年级上册第一次月考模拟数学试卷
- 真丝及仿真丝的种类有哪些?
- 【最新】华师大版八年级数学下册第十六
- 高中英语3500个必背单词
- 我可以接受失败,但我不能接受放弃!
- 最近更新沪科版八年级物理上册期末试卷
- 绿化工作先进乡镇事迹材料
- 鲁教版九年级上册思想品德教学计划
- 英语音标的分类
- 地下室底板无梁楼盖与普通梁板结构形式
- 美容师黄金销售话术
- 雅思写作满分作文备考方法
- 血清甲状腺激素测定与高频彩色多普勒超
- 1度浅析装修对室内空气品质的影响
- 2017-2022年中国汞矿行业深度分析与投
- 计算机二级VB公共基础知识
- (何勇)秸秆禁烧_重在寻找出路
- 内外墙抹灰工程分包施工合同1




