matlab实现cart(回归分类树)

本文记录了MATLAB新手尝试实现CART回归分类树的过程,数据集来源于UCI,目标是预测鲍鱼的年龄。文章详细介绍了创建树的各个步骤,包括获取信息熵、分割数据集、选择最佳特征等,并提供了代码链接。尽管尝试了Bootstrap抽样以提高准确性,但测试结果仍低于官方库的准确率,表明还有改进空间。
摘要由CSDN通过智能技术生成

作为机器学习的小白和matlab的小白自己参照 python的 《机器学习实战》 写了一下分类回归树,这里记录一下。

关于决策树的基础概念就不过多介绍了,至于是分类还是回归。。我说不清楚。。我用的数据集是这个http://archive.ics.uci.edu/ml/datasets/Abalone 就是通过一些属性来预测鲍鱼有多少头,下面看一下

Length / continuous / mm / Longest shell measurement 
Diameter / continuous / mm / perpendicular to length 
Height / continuous / mm / with meat in shell 
Whole weight / continuous / grams / whole abalone 
Shucked weight / continuous / grams / weight of meat 
Viscera weight / continuous / grams / gut weight (after bleeding) 
Shell weight / continuous / grams / after being dried 
Rings / integer / -- / +1.5 gives the age in years

这些属性除了最后的Rings是整数,可以看做是离散的,其他都是浮点数,是连续的。所以还是用cart中二分的思想,就是小于等于分一边,大于分一边。但是没有用gini指数,因为熵还是好一点。

代码在github:https://github.com/jokermask/matlab_cart

参照《机器学习实战》代码有5个部分:getEnt(获取信息熵),splitDataset(通过属性和阈值分割数据集),chooseBestFeatureToSplit(寻找最佳分割点和阈值),createTree(建树),predict(预测)。

 

我按流程梳理一下,首先函数脚本来将数据集划分成,训练集和测试集,然后用训练集建树,用测试集测试,(更改后变成bootstrap sampleing)

dataset = importdata('abalone.data.txt') ;
origin_data = dataset.data ;
labels = {
   'Length';'Diam';'Height';    'Whole';'Shucked';'Viscera';'Shell';'Rings'} ;
test_runtimes = 50 ;
ae = 0 ;
rr = 0 ;
for i=1:test_runtimes
    data = sampleWithReplace(origin_data) ;%bootstrap sampling
    len = floor(length(data)/4*3) ;
    train_data = data(1:len,:) ;
    test_data = data(len:end,:) ;
    test_y_truth = test_data(:,end) ;
%     tree = createTree(train_data,labels,0) ;
%     predict_y = predict(tree,test_data,labels) ;
%     com_matrix = [predict_y,test_y_truth] ;
%     count = sum(predict_y==
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值