当前位置: 首页 > news >正文

python:sklearn 决策树(Decision Tree)

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

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

编写 test_dtree_1.py  如下

# -*- 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  如下

# -*- 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 

http://www.xdnf.cn/news/207307.html

相关文章:

  • 从 0 到 1:ComfyUI AI 工作流抠图构建全实践
  • Linux[配置vim]
  • 通信设备制造数字化转型中的创新模式与实践探索
  • 首页数据展示
  • 并发设计模式实战系列(9):消息传递(Message Passing)
  • Redis性能优化终极指南:从原理到实战的深度调优策略
  • 超越单体:进入微服务世界与Spring Cloud概述
  • Java Stream流
  • 【Fifty Project - D20】
  • 推荐系统实验指标置信度:p值核心原理与工程应用指南
  • TA学习之路——2.3图形的HLSL常用函数详解
  • 万界星空科技QMS质量管理系统几大核心功能详解
  • 【Linux】第十五章 调度未来任务
  • LeetCode - 02.02.返回倒数第 k 个节点
  • 深挖Java基础之:认识Java(创立空间/先导:Java认识)
  • javascript<——>进阶
  • 嵌入式开发面试常见编程题解析:pthread_join 与 pthread_detach 详解
  • 【动手学大模型开发】使用 LLM API:智谱 GLM
  • Java练习6
  • Linux 内核中 TCP 协议的支撑解析
  • 云蝠智能大模型智能呼叫:赋能零售行业服务,助力客户增长
  • 基于PyTorch的Fashion-MNIST图像分类数据集处理与可视化
  • Tauri(2.5.1)+Leptos(0.7.8)开发桌面应用---后台调用Python Matplotlib绘制图形
  • 【计算机架构】CISC(复杂指令集计算机)架构
  • 移远通信LG69T赋能零跑B10:高精度定位护航,共赴汽车智联未来
  • flink cdc 配置
  • 1.5 城镇道路工程安全质量控制
  • Go 1.25为什么要废除核心类型
  • Educational Codeforces Round 178 div2(题解ABCDE)
  • 简化excel校验提高开发效率