用旧版本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'}
相关推荐
哈听星13 小时前
解非线性方程组
数学建模·matlab
亚图跨际14 小时前
MATLAB和R及Python伪时间分析
python·matlab·r语言·伪时间分析
小馒头学python15 小时前
机器学习中的分类:决策树、随机森林及其应用
人工智能·python·决策树·随机森林·机器学习·分类
fanxbl95715 小时前
LVQ 神经网络的 MATLAB 函数详解
人工智能·神经网络·matlab
吃葡萄不图葡萄皮16 小时前
3D绘制动态爱心Matlab
开发语言·matlab
远望清一色19 小时前
基于MATLAB边缘检测博文
开发语言·算法·matlab
远望清一色21 小时前
基于MATLAB的图片中文字的提取及识别
算法·matlab
黑曼巴、。;1 天前
直方图均衡化及Matlab实现
图像处理·计算机视觉·matlab
慕容复之巅1 天前
基于MATLAB+opencv人脸疲劳检测
开发语言·opencv·matlab
MATLAB滤波2 天前
鲁棒自适应滤波,MATLAB
开发语言·算法·matlab