《MATLAB 神经网络43个案例分析》:第28章 决策树分类器的应用研究——乳腺癌诊断
《MATLAB 神经网络43个案例分析》:第28章 决策树分类器的应用研究——乳腺癌诊断
- 1. 前言
- 2. MATLAB 仿真示例
- 3. 小结
1. 前言
《MATLAB 神经网络43个案例分析》是由 MATLAB 技术论坛(www.matlabsky.com)负责策划与编写,并由王小川教授执笔完成于 2013 年由北京航空航天大学出版社正式发行的一本 MATLAB 实例教学类书籍。该书是对《MATLAB 神经网络 30 个案例分析》一书的修订与补充,在其原有基础上增添了新的内容和优化了部分案例。全书以“理论指导 + 案例解析 + 应用拓展”为编排特点,旨在帮助读者更加直观和生动地掌握神经网络知识。
《MATLAB神经网络43个案例分析》包含共43章内容,并全面涵盖了多种经典的神经网络模型(如BP型人工神经网络、径向基函数型人工神经网络等),同时也涉及多种智能计算技术(如支持向量机模型等)。此外,在书中有多篇章节专门探讨了传统优化算法与现代人工神经网络之间的融合问题。本书不仅系统性地介绍了MATLAB R2012b版本中所附带的人工智能工具箱的新增功能及使用特点(包括支持并行计算的技术功能以及提供了自定义设计能力的人工智能建模方法),还重点阐述了这些新型技术在实际应用中的高效编程实现方法。
近年来,在人工智能研究热度不断攀升的情况下
2. MATLAB 仿真示例
打开MATLAB,点击“主页”,点击“打开”,找到示例文件

选中main_2009a.m,点击“打开”
main_2009a.m源码如下:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:决策树分类器在乳腺癌诊断中的应用研究(2009a版本)
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-06-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 决策树分类器在乳腺癌诊断中的应用研究(2009a版本)
%% 清空环境变量
clear all
clc
warning off
tic
%% 导入数据
load data.mat
% 随机产生训练集/测试集
a = randperm(569);
Train = data(a(1:500),:);
Test = data(a(501:end),:);
% 训练数据
P_train = Train(:,3:end);
T_train = Train(:,2);
% 测试数据
P_test = Test(:,3:end);
T_test = Test(:,2);
%% 创建决策树分类器
ctree = classregtree(P_train,T_train);
% 查看决策树视图
view(ctree);
%% 仿真测试
T_sim = eval(ctree,P_test);
%% 结果分析
count_B = length(find(T_train == 1));
count_M = length(find(T_train == 2));
rate_B = count_B / 500;
rate_M = count_M / 500;
total_B = length(find(data(:,2) == 1));
total_M = length(find(data(:,2) == 2));
number_B = length(find(T_test == 1));
number_M = length(find(T_test == 2));
number_B_sim = length(find(T_sim == 1 & T_test == 1));
number_M_sim = length(find(T_sim == 2 & T_test == 2));
disp(['病例总数:' num2str(569)...
' 良性:' num2str(total_B)...
' 恶性:' num2str(total_M)]);
disp(['训练集病例总数:' num2str(500)...
' 良性:' num2str(count_B)...
' 恶性:' num2str(count_M)]);
disp(['测试集病例总数:' num2str(69)...
' 良性:' num2str(number_B)...
' 恶性:' num2str(number_M)]);
disp(['良性乳腺肿瘤确诊:' num2str(number_B_sim)...
' 误诊:' num2str(number_B - number_B_sim)...
' 确诊率p1=' num2str(number_B_sim/number_B*100) '%']);
disp(['恶性乳腺肿瘤确诊:' num2str(number_M_sim)...
' 误诊:' num2str(number_M - number_M_sim)...
' 确诊率p2=' num2str(number_M_sim/number_M*100) '%']);
toc
代码解读
添加完毕,点击“运行”,开始仿真,输出仿真结果如下:
病例总数:569 良性:357 恶性:212
训练集病例总数:500 良性:313 恶性:187
测试集病例总数:69 良性:44 恶性:25
良性乳腺肿瘤确诊:40 误诊:4 确诊率p1=90.9091%
恶性乳腺肿瘤确诊:22 误诊:3 确诊率p2=88%
时间已过 0.989542 秒。
代码解读

点击当前文件夹视图框中的main_2012b.m

打开main_2012b.m源码如下:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%功能:决策树分类器在乳腺癌诊断中的应用研究(2012b版本)
%环境:Win7,Matlab2015b
%Modi: C.S
%时间:2022-06-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 决策树分类器在乳腺癌诊断中的应用研究(2012b版本)
%% 清空环境变量
clear all
clc
warning off
tic
%% 导入数据
load data.mat
% 随机产生训练集/测试集
a = randperm(569);
Train = data(a(1:500),:);
Test = data(a(501:end),:);
% 训练数据
P_train = Train(:,3:end);
T_train = Train(:,2);
% 测试数据
P_test = Test(:,3:end);
T_test = Test(:,2);
%% 创建决策树分类器
ctree = ClassificationTree.fit(P_train,T_train);
% 查看决策树视图
view(ctree);
view(ctree,'mode','graph');
%% 仿真测试
T_sim = predict(ctree,P_test);
%% 结果分析
count_B = length(find(T_train == 1));
count_M = length(find(T_train == 2));
rate_B = count_B / 500;
rate_M = count_M / 500;
total_B = length(find(data(:,2) == 1));
total_M = length(find(data(:,2) == 2));
number_B = length(find(T_test == 1));
number_M = length(find(T_test == 2));
number_B_sim = length(find(T_sim == 1 & T_test == 1));
number_M_sim = length(find(T_sim == 2 & T_test == 2));
disp(['病例总数:' num2str(569)...
' 良性:' num2str(total_B)...
' 恶性:' num2str(total_M)]);
disp(['训练集病例总数:' num2str(500)...
' 良性:' num2str(count_B)...
' 恶性:' num2str(count_M)]);
disp(['测试集病例总数:' num2str(69)...
' 良性:' num2str(number_B)...
' 恶性:' num2str(number_M)]);
disp(['良性乳腺肿瘤确诊:' num2str(number_B_sim)...
' 误诊:' num2str(number_B - number_B_sim)...
' 确诊率p1=' num2str(number_B_sim/number_B*100) '%']);
disp(['恶性乳腺肿瘤确诊:' num2str(number_M_sim)...
' 误诊:' num2str(number_M - number_M_sim)...
' 确诊率p2=' num2str(number_M_sim/number_M*100) '%']);
%% 叶子节点含有的最小样本数对决策树性能的影响
leafs = logspace(1,2,10);
N = numel(leafs);
err = zeros(N,1);
for n = 1:N
t = ClassificationTree.fit(P_train,T_train,'crossval','on','minleaf',leafs(n));
err(n) = kfoldLoss(t);
end
plot(leafs,err);
xlabel('叶子节点含有的最小样本数');
ylabel('交叉验证误差');
title('叶子节点含有的最小样本数对决策树性能的影响')
%% 设置minleaf为28,产生优化决策树
OptimalTree = ClassificationTree.fit(P_train,T_train,'minleaf',28);
view(OptimalTree,'mode','graph')
% 计算优化后决策树的重采样误差和交叉验证误差
resubOpt = resubLoss(OptimalTree)
lossOpt = kfoldLoss(crossval(OptimalTree))
% 计算优化前决策树的重采样误差和交叉验证误差
resubDefault = resubLoss(ctree)
lossDefault = kfoldLoss(crossval(ctree))
%% 剪枝
[~,~,~,bestlevel] = cvLoss(ctree,'subtrees','all','treesize','min')
cptree = prune(ctree,'Level',bestlevel);
view(cptree,'mode','graph')
% 计算剪枝后决策树的重采样误差和交叉验证误差
resubPrune = resubLoss(cptree)
lossPrune = kfoldLoss(crossval(cptree))
toc
代码解读
点击“运行”,开始仿真,输出仿真结果如下:
Decision tree for classification
1 if x23<106.2 then node 2 elseif x23>=106.2 then node 3 else 1
2 if x28<0.1589 then node 4 elseif x28>=0.1589 then node 5 else 1
3 if x23<115.35 then node 6 elseif x23>=115.35 then node 7 else 2
4 if x28<0.1347 then node 8 elseif x28>=0.1347 then node 9 else 1
5 if x22<23.47 then node 10 elseif x22>=23.47 then node 11 else 2
6 if x25<0.13825 then node 12 elseif x25>=0.13825 then node 13 else 2
7 if x9<0.1324 then node 14 elseif x9>=0.1324 then node 15 else 2
8 if x11<1.04755 then node 16 elseif x11>=1.04755 then node 17 else 1
9 if x2<20.785 then node 18 elseif x2>=20.785 then node 19 else 1
10 class = 1
11 class = 2
12 if x30<0.070125 then node 20 elseif x30>=0.070125 then node 21 else 1
13 if x2<13.42 then node 22 elseif x2>=13.42 then node 23 else 2
14 class = 1
15 class = 2
16 if x14<48.975 then node 24 elseif x14>=48.975 then node 25 else 1
17 class = 2
18 class = 1
19 class = 2
20 class = 2
21 if x14<42.95 then node 26 elseif x14>=42.95 then node 27 else 1
22 class = 1
23 class = 2
24 if x15<0.003294 then node 28 elseif x15>=0.003294 then node 29 else 1
25 class = 1
26 class = 1
27 class = 2
28 class = 1
29 if x22<33.35 then node 30 elseif x22>=33.35 then node 31 else 1
30 class = 1
31 if x22<33.56 then node 32 elseif x22>=33.56 then node 33 else 1
32 class = 2
33 class = 1
病例总数:569 良性:357 恶性:212
训练集病例总数:500 良性:317 恶性:183
测试集病例总数:69 良性:40 恶性:29
良性乳腺肿瘤确诊:36 误诊:4 确诊率p1=90%
恶性乳腺肿瘤确诊:27 误诊:2 确诊率p2=93.1034%
resubOpt =
0.0720
lossOpt =
0.0820
resubDefault =
0.0080
lossDefault =
0.0740
bestlevel =
1
resubPrune =
0.0100
lossPrune =
0.0880
时间已过 4.620338 秒。
代码解读




3. 小结
决策树是一种层级结构,在其中每个内部节点代表对一个属性进行测试的过程,在这种情况下分支则对应于测试的结果,在此过程中叶子节点则对应于不同的类别。基于实例的学习方法用于归纳决策树模型,并且该模型通常采用自顶而下的递归策略构建。其核心思想是利用信息熵作为度量标准来构建一棵熵值不断递减的最优决策树模型,在到达叶子节点时信息熵达到最小值零。本章详细介绍了matlab2009a版本与matlab2012b版本中进行仿真时的具体输出效果,并在视觉机器学习20讲专栏中也有相关仿真示例可供参考。对于本章内容感兴趣的朋友或者希望深入学习相关内容的人士,请参考本书第二十八章的内容以获取更多补充知识。后期将在理解的基础上进一步完善相关内容,并欢迎大家一起交流学习
基于MATLAB平台实现的视觉机器学习第4讲——决策树构建过程
