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

相关推荐
2301_813599551 分钟前
Go语言怎么用AWS S3_Go语言S3对象存储教程【总结】
jvm·数据库·python
程序员科科2 分钟前
2026最新ChatGPT Plus会员优惠充值,轻松使用GPT-5
python
编程小风筝3 分钟前
就业信息推荐系统 Python+Django+Vue.js
vue.js·python·django
曲幽3 分钟前
FastAPI数据库ORM怎么选?我肝了三个Demo后,终于不再纠结了
python·fastapi·web·orm·async·sqlalchemy·sqlmodel·tortoise
qq_189807034 分钟前
如何通过SSH隧道连接远程数据库_本地端口转发与phpMyAdmin
jvm·数据库·python
InfinteJustice4 分钟前
Golang map底层实现原理_Golang map哈希表原理教程【收藏】
jvm·数据库·python
21439655 分钟前
如何在MongoDB中监控集群中的僵尸连接_释放长时间不活跃的游标资源
jvm·数据库·python
亚林瓜子6 分钟前
AWS Glue Python Shell任务中pip安装依赖库
python·shell·pip·aws·glue·job
qq_206901396 分钟前
C#怎么使用全局Using C#global using全局引用怎么配置减少每个文件的using声明【语法】
jvm·数据库·python
好家伙VCC9 分钟前
# ARCore+ Kotlin 实战:打造沉浸式增强现实交互应用在
java·python·kotlin·ar·交互