用旧版本Matlab训练的 classregtree类的决策树model 在新版Matlab无法使用的解决方法

背景

  • 想把原来r2015a版本的代码升级到r2021b,用2021b运行原来的代码时,报错
  • 搜索发现R2019a中已经去除了classregtree函数和classregtree类

解决方法

新版本的Matlab load('TreeModel.mat')后,查看TreeModel的值

matlab 复制代码
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

matlab 复制代码
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

例子:

matlab 复制代码
例子:
   >> result = TreeModelFunction([4.5 2.5])

      result =

          1×1 cell 数组

          {'versicolor'}
相关推荐
吐泡泡科技12 小时前
MATLAB 车牌自动识别系统设计 SVM支持向量机方法 车牌识别
算法·支持向量机·matlab
喵帕栞13 小时前
MATLAB基础
开发语言·matlab
今天吃饺子15 小时前
未发表!一个小创新分类模型,融合SE注意力机制的TCN-SVM模型,江南大学、西储大学轴承数据为例---MATLAB代码...
人工智能·机器学习·支持向量机·matlab·分类
Evand J16 小时前
【PSINS】EKF、UKF、CKF三个滤波下的组合导航(松组合)对比
算法·matlab·导航
爱代码的小黄人16 小时前
什么是自治系统和非自治系统
matlab
浊酒南街17 小时前
决策树(理论知识2)
算法·决策树·机器学习
studyer_domi19 小时前
matlab simlink汽车横向三自由度模型
开发语言·算法·matlab·汽车
IT猿手20 小时前
最新高性能多目标优化算法:融合高斯扰动与竞争学习的多目标加权平均算法(MOWAA)求解TP1-TP10及工程应用---盘式制动器设计,提供完整MATLAB代码
深度学习·学习·算法·机器学习·matlab·目标跟踪·多目标优化
的不对不21 小时前
MATLAB程序转C# WPF,dll集成,混合编程
matlab·c#·.net·wpf
机器学习之心1 天前
工程设计优化问题:改进海鸥算法(Matlab)
算法·matlab