数据分析+python+决策树

本文探讨了决策树的三种算法:ID3基于信息增益,C4.5使用信息增益率,而CART算法则基于基尼系数。通过实例展示了如何使用Python的sklearn库对iris数据集进行分类,对boston数据集进行回归预测,以及对digits数据集执行分类任务。
摘要由CSDN通过智能技术生成

决策树:

  • ID3 算法,基于信息增益做判断;
  • C4.5 算法,基于信息增益率做判断
  • CART算法,基于基尼系数做判断,回归树是基于偏差做判断

以下是cart算法分类和预测的实现过程

  • 第一个使用的sklearn的iris 数据集,进行分类
  • 第二个使用的sklearn的boston 数据集,进行回归预测
  • 第三个使用的sklearn的digits数据集,进行分类

 

# encoding=utf-8
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
# 准备数据集
iris=load_iris()
# 获取特征集和分类标识
features = iris.data
labels = iris.target
# 随机抽取 33% 的数据作为测试集,其余为训练集
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.33, random_state=0)
# 创建 CART 分类树
clf = DecisionTreeClassifier(criterion='gini')
# 拟合构造 CART 分类树
clf = clf.fit(train_features, train_labels)
# 用 CART 分类树做预测
test_predict = clf.predict(test_features)
# 预测结果与测试集结果作比对
score = accuracy_score(test_labels, test_predict)
print("CART 分类树准确率 %.4lf" % score)
# encoding=utf-8
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from sklearn.metrics import r2_score,mean_absolute_error,mean_squared_error
from sklearn.tree import DecisionTreeRegressor
# 准备数据集
boston=load_boston()
# 探索数据
print(boston.feature_names)
# 获取特征集和房价
features = boston.data
prices = boston.target
# 随机抽取 33% 的数据作为测试集,其余为训练集
train_features, test_features, train_price, test_price = train_test_split(features, prices, test_size=0.33)
# 创建 CART 回归树
dtr=DecisionTreeRegressor()
# 拟合构造 CART 回归树
dtr.fit(train_features, train_price)
# 预测测试集中的房价
predict_price = dtr.predict(test_features)
# 测试集的结果评价
print('回归树二乘偏差均值:', mean_squared_error(test_price, predict_price))
print('回归树绝对值偏差均值:', mean_absolute_error(test_price, predict_price)) 

 

 

# -*- coding: utf-8 -*-
"""
Created on Fri Mar  8 16:28:43 2019

@author: Administrator
"""
# encoding=utf-8
from  sklearn.metrics import accuracy_score    # 均方差
from  sklearn.model_selection import train_test_split  #训练和测试集的划分
from  sklearn.datasets import load_digits
#from  sklearn.metrics import r2_score,mean_absolute_error
from  sklearn.tree import DecisionTreeClassifier
# 准备数据集
digits=load_digits()

# 获取特征集和分类标识

features =digits.data
labels = digits.target
#print(features)
#print(labels)
# 随机抽取 33% 的数据作为测试集,其余为训练集
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.33)
# 创建 CART 分类树
clf = DecisionTreeClassifier()
# 拟合构造 CART 分类树
clf.fit(train_features,train_labels)
# 用 CART 分类树做预测
test_predict = clf.predict(test_features)
#测试集的结果评价
score=accuracy_score(test_labels,test_predict)
print('CART 的准确率 %4lf' % score)




 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值