简单的python决策树案例

本文是关于机器学习入门的决策树实例,通过分析tree(1).csv数据集,利用DictVectorizer对非数字化特征进行抽取,并建立决策树模型。同时介绍了如何使用Graphviz工具可视化决策树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

机器学习入门——决策树的实例

写文章的目的是为了巩固所学,和方便回顾查找。如有讲错的地方,欢迎指出,谢谢。

我们的数据集 tree(1).csv 长这样:

RID age income student credit_rating buy
1 youth high no fair no
2 youth high no excellent no
3 middle_aged high no fair yes
4 senior medium no fair yes
5 senior low yes fair yes
6 senior low yes excellent no
7 middle_aged low yes excellent yes
8 youth medium no fair no
9 youth low yes fair yes
10 senior medium yes fair yes
11 youth medium yes excellent yes
12 middle_aged medium no excellent yes
13 middle_aged high yes fair yes
14 senior medium no excellent no
#导入必要的库
from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import preprocessing
from sklearn import tree
#加载数据文件
load_file = open(r'tree(1).csv')  
reader = csv.reade
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值