用旧版本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'}
相关推荐
mosaic_born1 小时前
matlab-对比两张图片的Ycbcr分量的差值并形成直方图
matlab
通信仿真实验室2 小时前
MATLAB使用眼图分析QPSK通信系统接收端匹配滤波后的信号
开发语言·算法·matlab
简简单单做算法3 小时前
数字通信中不同信道类型对通信系统性能影响matlab仿真分析,对比AWGN,BEC,BSC以及多径信道
matlab·awgn·bec·bsc·多径信道
通信仿真实验室3 小时前
(15)衰落信道模型作用于信号是相乘还是卷积
开发语言·人工智能·算法·matlab
Sylvan Ding3 小时前
数值分析作业(第二章):代码+手写计算
matlab·数值计算·北京理工大学
通信仿真实验室15 小时前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
jk_1011 天前
MATLAB中decomposition函数用法
开发语言·算法·matlab
不想当个技术宅1 天前
【梯级水电站调度优化】基于自适应权值优化粒子群算法
matlab·粒子群算法·梯级水电站调度优化
蓝色洛特1 天前
【Matlab元胞自动机】《高速公路人工—自动驾驶混行交通流临界特征研究》
matlab·自动驾驶·元胞自动机·交通仿真
quaer1 天前
Open-Sora全面开源?
开发语言·算法·机器学习·matlab·矩阵