python:sklearn 决策树(Decision Tree)

5. 决策树(Decision Tree) - 第5章

算法思想:基于信息增益(ID3)或基尼不纯度(CART)递归划分特征。

编写 test_dtree_1.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" 5. 决策树(Decision Tree) """
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split

# 加载 乳腺癌数据
data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = DecisionTreeClassifier(criterion='entropy', max_depth=3)
model.fit(X_train, y_train)
print("Accuracy:", model.score(X_test, y_test))

Anaconda 3

运行 python test_dtree_1.py

Accuracy: 0.9736842105263158


编写 test_dtree_2.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" 5. 决策树(Decision Tree) """
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import preprocessing
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,accuracy_score
from sklearn.tree import plot_tree

# 加载鸢尾花数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
f_names = iris.feature_names
t_names = iris.target_names

# 数据预处理:按列归一化
X = preprocessing.scale(X)
# 切分数据集:测试集 20%
X_train,X_test,y_train,y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 初始化 决策树 分类模型
dtc = DecisionTreeClassifier()
# 模型训练
dtc.fit(X_train,y_train)
# 模型预测
y_pred = dtc.predict(X_test)
# 模型评估
# 混淆矩阵
#print(confusion_matrix(y_test,y_pred))
print("准确率: %.4f" % accuracy_score(y_test,y_pred))

# 可视化决策树
plt.figure(figsize=(12,10))
plot_tree(dtc, feature_names=f_names, class_names=t_names, filled=True)
plt.show()

运行 python test_dtree_2.py

相关推荐
weixin_5142218542 分钟前
FDTD与matlab、python耦合
python·学习·matlab·fdtd
递归不收敛2 小时前
吴恩达机器学习课程(PyTorch 适配)学习笔记大纲
pytorch·学习·机器学习
递归不收敛5 小时前
吴恩达机器学习课程(PyTorch适配)学习笔记:2.4 激活函数与多类别处理
pytorch·学习·机器学习
F_D_Z6 小时前
数据集相关类代码回顾理解 | StratifiedShuffleSplit\transforms.ToTensor\Counter
python·torchvision·transforms
tao3556677 小时前
【Python刷力扣hot100】283. Move Zeroes
开发语言·python·leetcode
小宁爱Python7 小时前
从零搭建 RAG 智能问答系统1:基于 LlamaIndex 与 Chainlit实现最简单的聊天助手
人工智能·后端·python
湖南人爱科技有限公司7 小时前
RaPhp和Python某音最新bd-ticket-guard-client-data加密算法解析(视频评论)
android·python·php·音视频·爬山算法·raphp
~kiss~8 小时前
K-means损失函数-收敛证明
算法·机器学习·kmeans
eqwaak09 小时前
数据预处理与可视化流水线:Pandas Profiling + Altair 实战指南
开发语言·python·信息可视化·数据挖掘·数据分析·pandas
心态特好10 小时前
详解WebSocket及其妙用
java·python·websocket·网络协议