Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战

✅ 今日目标

  • 理解决策树(Decision Tree)的基本原理
  • 掌握信息熵、基尼系数等分裂标准
  • 使用 DecisionTreeClassifierRandomForestClassifier 构建模型
  • 学会可视化决策树与查看特征重要性
  • 对比单棵树与集成模型(随机森林)的泛化能力

📘 一、决策树模型简介

特性 描述
本质 以"特征条件"划分决策路径,形成一棵判断树
优点 逻辑清晰、可解释性强、不需归一化
缺点 易过拟合、对噪声敏感
应用 信用评分、规则建模、分类可视化

🧠 二、常用模型 API

决策树:

python 复制代码
from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier(max_depth=3, criterion='gini')
clf.fit(X_train, y_train)

随机森林:

python 复制代码
from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)

📊 三、评估方式建议

模型 适合场景 可解释性 精度表现
决策树 可视化逻辑、规则推理 ✅ 强 中等
随机森林 提高精度、降低过拟合 中等 ✅ 强

📈 四、可视化与分析

python 复制代码
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
plot_tree(clf, feature_names=["成绩", "性别"], class_names=["不及格", "及格"], filled=True)
plt.show()
python 复制代码
# 特征重要性
import pandas as pd
importance = rf.feature_importances_
pd.DataFrame({"特征": ["成绩", "性别"], "重要性": importance})

💡 今日思路建议

  1. 构建同样的"是否及格预测"分类数据集
  2. 训练决策树模型,尝试调节 max_depth 查看影响
  3. 训练随机森林模型,查看是否提升性能
  4. 输出特征重要性对比
  5. 可视化决策树结构图

📁 练习脚本:decision_tree_forest_demo.py

python 复制代码
# 决策树 & 随机森林实战:预测学生是否及格

from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.rcParams['font.family'] = 'Arial Unicode MS'  # Mac 用户可用
plt.rcParams['axes.unicode_minus'] = False
# 1. 构造数据
np.random.seed(42)
size = 100
scores = np.random.randint(40, 100, size)
genders = np.random.choice([0, 1], size=size)
labels = (scores >= 60).astype(int)

# 标准化成绩 + 性别作为特征
X = np.column_stack(((scores - scores.mean()) / scores.std(), genders))
y = labels

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 2. 决策树模型
dt_model = DecisionTreeClassifier(max_depth=3, criterion='gini', random_state=42)
dt_model.fit(X_train, y_train)
y_pred_dt = dt_model.predict(X_test)

print("=== 决策树模型评估 ===")
print("准确率:", accuracy_score(y_test, y_pred_dt))
print(classification_report(y_test, y_pred_dt))

# 决策树可视化
plt.figure(figsize=(10, 6))
plot_tree(dt_model, feature_names=["成绩", "性别"], class_names=["不及格", "及格"], filled=True)
plt.title("决策树可视化")
plt.tight_layout()
plt.show()

# 3. 随机森林模型
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
y_pred_rf = rf_model.predict(X_test)

print("\n=== 随机森林模型评估 ===")
print("准确率:", accuracy_score(y_test, y_pred_rf))
print(classification_report(y_test, y_pred_rf))

# 特征重要性对比
feature_importance = rf_model.feature_importances_
features = ["成绩", "性别"]
importance_df = pd.DataFrame({"特征": features, "重要性": feature_importance})
print("\n=== 特征重要性(随机森林) ===")
print(importance_df)

运行输出:

python 复制代码
=== 决策树模型评估 ===
准确率: 1.0
              precision    recall  f1-score   support

           0       1.00      1.00      1.00         7
           1       1.00      1.00      1.00        13

    accuracy                           1.00        20
   macro avg       1.00      1.00      1.00        20
weighted avg       1.00      1.00      1.00        20
相关推荐
BJ_Bonree3 分钟前
圆桌论坛精华实录 | AI是重构运维逻辑的颠覆性革命?博睿数据与行业大咖亲授“AI+可观测性”的破局之道
运维·人工智能·重构
终端域名5 分钟前
从 Grok 4 多智能体协同到 RAG 范式革命:2025 年 AI 工作流的技术重构生成
人工智能·重构
Dfreedom.11 分钟前
卷积神经网络(CNN)全面解析
人工智能·神经网络·cnn·卷积神经网络
apocelipes15 分钟前
POSIX兼容系统上read和write系统调用的行为总结
linux·c语言·c++·python·golang·linux编程
暴风鱼划水25 分钟前
算法题(Python)数组篇 | 6.区间和
python·算法·数组·区间和
Derrick__139 分钟前
Web Js逆向——加密参数定位方法(Hook)
python·js
zl_vslam42 分钟前
SLAM中的非线性优-3D图优化之轴角在Opencv-PNP中的应用(一)
前端·人工智能·算法·计算机视觉·slam se2 非线性优化
南汐汐月1 小时前
重生归来,我要成功 Python 高手--day33 决策树
开发语言·python·决策树
koo3641 小时前
李宏毅机器学习笔记43
人工智能·笔记·机器学习
lzjava20241 小时前
Spring AI使用知识库增强对话功能
人工智能·python·spring