背景
- 想把原来r2015a版本的代码升级到r2021b,用2021b运行原来的代码时,报错
- 搜索发现R2019a中已经去除了classregtree函数和classregtree类
解决方法
新版本的Matlab load(‘TreeModel.mat’)后,查看TreeModel的值
val =
分类的决策树
1 if SL<5.45 then node 2 elseif SL>=5.45 then node 3 else setosa
2 if SW<2.8 then node 4 elseif SW>=2.8 then node 5 else setosa
3 if SL<6.15 then node 6 elseif SL>=6.15 then node 7 else virginica
4 class = versicolor
5 class = setosa
6 if SW<3.45 then node 8 elseif SW>=3.45 then node 9 else versicolor
7 if SL<7.05 then node 10 elseif SL>=7.05 then node 11 else virginica
8 if SL<5.75 then node 12 elseif SL>=5.75 then node 13 else versicolor
9 class = setosa
10 if SW<2.4 then node 14 elseif SW>=2.4 then node 15 else virginica
11 class = virginica
12 class = versicolor
13 if SW<3.1 then node 16 elseif SW>=3.1 then node 17 else versicolor
14 class = versicolor
15 if SL<6.95 then node 18 elseif SL>=6.95 then node 19 else virginica
16 if SW<2.95 then node 20 elseif SW>=2.95 then node 21 else versicolor
17 class = versicolor
18 if SW<3.15 then node 22 elseif SW>=3.15 then node 23 else virginica
19 class = versicolor
20 class = versicolor
21 class = virginica
22 if SL<6.55 then node 24 elseif SL>=6.55 then node 25 else virginica
23 class = virginica
24 if SW<2.95 then node 26 elseif SW>=2.95 then node 27 else virginica
25 if SL<6.65 then node 28 elseif SL>=6.65 then node 29 else versicolor
26 if SL<6.45 then node 30 elseif SL>=6.45 then node 31 else virginica
27 class = virginica
28 class = versicolor
29 if SW<2.65 then node 32 elseif SW>=2.65 then node 33 else virginica
30 if SW<2.85 then node 34 elseif SW>=2.85 then node 35 else virginica
31 class = versicolor
32 class = virginica
33 if SW<2.9 then node 36 elseif SW>=2.9 then node 37 else versicolor
34 class = virginica
35 class = versicolor
36 class = versicolor
37 class = virginica
TreeModel共有37个nodes,
决策树model就是根据特征值是否满足条件 进而判断哪一类别,因此,把model改成判断的function就可以重新调用了,每个node对应一个子function,共37个子function文章来源:https://www.toymoban.com/news/detail-832482.html
function result = TreeModelFunction(Features)
% 分类的决策树
% 1 if SL<5.45 then node 2 elseif SL>=5.45 then node 3 else setosa
% 2 if SW<2.8 then node 4 elseif SW>=2.8 then node 5 else setosa
% 3 if SL<6.15 then node 6 elseif SL>=6.15 then node 7 else virginica
% 4 class = versicolor
% 5 class = setosa
% 6 if SW<3.45 then node 8 elseif SW>=3.45 then node 9 else versicolor
% 7 if SL<7.05 then node 10 elseif SL>=7.05 then node 11 else virginica
% 8 if SL<5.75 then node 12 elseif SL>=5.75 then node 13 else versicolor
% 9 class = setosa
% 10 if SW<2.4 then node 14 elseif SW>=2.4 then node 15 else virginica
% 11 class = virginica
% 12 class = versicolor
% 13 if SW<3.1 then node 16 elseif SW>=3.1 then node 17 else versicolor
% 14 class = versicolor
% 15 if SL<6.95 then node 18 elseif SL>=6.95 then node 19 else virginica
% 16 if SW<2.95 then node 20 elseif SW>=2.95 then node 21 else versicolor
% 17 class = versicolor
% 18 if SW<3.15 then node 22 elseif SW>=3.15 then node 23 else virginica
% 19 class = versicolor
% 20 class = versicolor
% 21 class = virginica
% 22 if SL<6.55 then node 24 elseif SL>=6.55 then node 25 else virginica
% 23 class = virginica
% 24 if SW<2.95 then node 26 elseif SW>=2.95 then node 27 else virginica
% 25 if SL<6.65 then node 28 elseif SL>=6.65 then node 29 else versicolor
% 26 if SL<6.45 then node 30 elseif SL>=6.45 then node 31 else virginica
% 27 class = virginica
% 28 class = versicolor
% 29 if SW<2.65 then node 32 elseif SW>=2.65 then node 33 else virginica
% 30 if SW<2.85 then node 34 elseif SW>=2.85 then node 35 else virginica
% 31 class = versicolor
% 32 class = virginica
% 33 if SW<2.9 then node 36 elseif SW>=2.9 then node 37 else versicolor
% 34 class = virginica
% 35 class = versicolor
% 36 class = versicolor
% 37 class = virginica
%
% 注意: Features 是单个样本的特征值,大小为1*2
% 这个TreeModelFunction根据特征值判断该样本属于以下三类中的哪一类:'setosa' 'virginica' 'versicolor'
% TreeModelFunction函数只能判断单个样本的类别,如要判断多个样本的类别,请循环调用TreeModelFunction函数
% 例子:
% >> result = TreeModelFunction([4.5 2.5])
%
% result =
%
% 1×1 cell 数组
%
% {'versicolor'}
SL = Features(1);
SW = Features(2);
result = node01(SL,SW);
end
% --------------------------------------------------------------------------
function result = node01(SL,SW)
if SL<5.45
result = node02(SL,SW);
elseif SL>=5.45
result = node03(SL,SW);
else
result{1} = 'setosa';
end
end
function result = node02(SL,SW)
if SW<2.8
result = node04(SL,SW);
elseif SW>=2.8
result = node05(SL,SW);
else
result{1} = 'setosa';
end
end
function result = node03(SL,SW)
if SL<6.15
result = node06(SL,SW);
elseif SL>=6.15
result = node07(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node04(SL,SW)
result{1} = 'versicolor';
end
function result = node05(SL,SW)
result{1} = 'setosa';
end
function result = node06(SL,SW)
if SW<3.45
result = node08(SL,SW);
elseif SW>=3.45
result = node09(SL,SW);
else
result{1} = 'versicolor';
end
end
function result = node07(SL,SW)
if SL<7.05
result = node10(SL,SW);
elseif SL>=7.05
result = node11(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node08(SL,SW)
if SL<5.75
result = node12(SL,SW);
elseif SL>=5.75
result = node13(SL,SW);
else
result{1} = 'versicolor';
end
end
function result = node09(SL,SW)
result{1} = 'setosa';
end
function result = node10(SL,SW)
if SW<2.4
result = node14(SL,SW);
elseif SW>=2.4
result = node15(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node11(SL,SW)
result{1} = 'virginica';
end
function result = node12(SL,SW)
result{1} = 'versicolor';
end
function result = node13(SL,SW)
if SW<3.1
result = node16(SL,SW);
elseif SW>=3.1
result = node17(SL,SW);
else
result{1} = 'versicolor';
end
end
function result = node14(SL,SW)
result{1} = 'versicolor';
end
function result = node15(SL,SW)
if SL<6.95
result = node18(SL,SW);
elseif SL>=6.95
result = node19(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node16(SL,SW)
if SW<2.95
result = node20(SL,SW);
elseif SW>=2.95
result = node21(SL,SW);
else
result{1} = 'versicolor';
end
end
function result = node17(SL,SW)
result{1} = 'versicolor';
end
function result = node18(SL,SW)
if SW<3.15
result = node22(SL,SW);
elseif SW>=3.15
result = node23(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node19(SL,SW)
result{1} = 'versicolor';
end
function result = node20(SL,SW)
result{1} = 'versicolor';
end
function result = node21(SL,SW)
result{1} = 'virginica';
end
function result = node22(SL,SW)
if SL<6.55
result = node24(SL,SW);
elseif SL>=6.55
result = node25(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node23(SL,SW)
result{1} = 'virginica';
end
function result = node24(SL,SW)
if SW<2.95
result = node26(SL,SW);
elseif SW>=2.95
result = node27(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node25(SL,SW)
if SL<6.65
result = node28(SL,SW);
elseif SL>=6.65
result = node29(SL,SW);
else
result{1} = 'versicolor';
end
end
function result = node26(SL,SW)
if SL<6.45
result = node30(SL,SW);
elseif SL>=6.45
result = node31(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node27(SL,SW)
result{1} = 'virginica';
end
function result = node28(SL,SW)
result{1} = 'versicolor';
end
function result = node29(SL,SW)
if SW<2.65
result = node32(SL,SW);
elseif SW>=2.65
result = node33(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node30(SL,SW)
if SW<2.85
result = node34(SL,SW);
elseif SW>=2.85
result = node35(SL,SW);
else
result{1} = 'virginica';
end
end
function result = node31(SL,SW)
result{1} = 'versicolor';
end
function result = node32(SL,SW)
result{1} = 'virginica';
end
function result = node33(SL,SW)
if SW<2.9
result = node36(SL,SW);
elseif SW>=2.9
result = node37(SL,SW);
else
result{1} = 'versicolor';
end
end
function result = node34(SL,SW)
result{1} = 'virginica';
end
function result = node35(SL,SW)
result{1} = 'versicolor';
end
function result = node36(SL,SW)
result{1} = 'versicolor';
end
function result = node37(SL,SW)
result{1} = 'virginica';
end
例子:文章来源地址https://www.toymoban.com/news/detail-832482.html
例子:
>> result = TreeModelFunction([4.5 2.5])
result =
1×1 cell 数组
{'versicolor'}
到了这里,关于用旧版本Matlab训练的 classregtree类的决策树model 在新版Matlab无法使用的解决方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!