clear; clc;
% 导入数据
z = [zeros(1,250); ones(1,250)];
z1 = [ones(1,250); zeros(1,250)];
z2 = [zeros(1,30); ones(1,30)];
z3 = [ones(1,39); zeros(1,39)];
data = [z z1 z2 z3]';
% 随机产生训练集和测试集
a = randperm(569);
train = data(a(1:500), :);
test = data(a(501:end), :);
% 训练数据
Ptrain = train(:,1); % 特征值
Ttrain = train(:,2); % 分类结果
% 测试数据
Ptest = test(:,1);
Ttest = test(:,2);
% 创建决策树分类器
ctree = fitctree(Ptrain, Ttrain);
% 1. 查看决策树图
view(ctree, 'Mode', 'graph');
% 仿真测试
Tsim = predict(ctree, Ptest);
% 结果分析
countb = length(find(Ttrain == 0));
countm = length(find(Ttrain == 1)); % 两种类别训练集的数目
rateb = countb / 500;
ratem = countm / 500;
totalb = length(find(data(:,2) == 0));
totalm = length(find(data(:,2) == 1)); % 原数据两种类别的数目
numberb = length(find(Ttest == 0));
numberm = length(find(Ttest == 1)); % 测试集中两类别的数目
numberbsim = length(find(Tsim == 0 & Ttest == 0));
numbermsim = length(find(Tsim == 1 & Ttest == 1)); % 测试集预测与原类别相等的数目
d11 = numberb - numberbsim;
d12 = numberm - numbermsim;
d21 = numberbsim / numberb;
d22 = numbermsim / numberm;
disp(['判断错误:', num2str(d11)]); % 两种类型的准确率
disp(['准确度:', num2str(d21)]);
disp(['判断错误:', num2str(d12)]); % 两种类型的准确率
disp(['准确度:', num2str(d22)]);
% 叶子节点含有的最小样本数对决策树性能的影响
leafs = logspace(1, 2, 10); % 10到100
N = numel(leafs);
err = zeros(N, 1);
for n = 1:N
t = fitctree(Ptrain, Ttrain, 'CrossVal', 'on', 'MinLeaf', leafs(n));
err(n) = kfoldLoss(t);
end
figure;
plot(leafs, err);
xlabel('叶子节点含有的最小样本数');
ylabel('交叉验证误差');
title('叶子节点含有的最小样本数对决策树性能的影响');
% 设置minfit为上述图中最低点,x的值
Optimaltree = fitctree(Ptrain, Ttrain, 'MinLeaf', 13);
view(Optimaltree, 'Mode', 'graph');
% 计算优化前和优化后决策树的重采样误差和交叉验证误差,主要看后一个
resubDefault = resubLoss(ctree);
lossDefault = kfoldLoss(crossval(ctree));
resubOpt = resubLoss(Optimaltree);
lossOpt = kfoldLoss(crossval(Optimaltree));
% 剪枝
[~, ~, ~, bestlevel] = cvLoss(ctree, 'Subtrees', 'all', 'Treesize', 'min');
cptree = prune(ctree, 'Level', bestlevel);
view(cptree, 'Mode', 'graph');
% 计算剪枝后误差
resubPrune = resubLoss(cptree);
lossPrune = kfoldLoss(crossval(cptree));
12-07
2539
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)