Skip to Content
机器学习速查手册
book

机器学习速查手册

by Matt Harrison
July 2025
Intermediate to advanced
320 pages
3h 10m
Chinese
O'Reilly Media, Inc.
Content preview from 机器学习速查手册

第 13 章 解释模型

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

预测模型具有不同的特性。有些模型旨在处理线性数据。其他模型则可以处理更复杂的输入。有些模型很容易解释,有些则像黑盒子,无法让人深入了解预测是如何产生的。

在本章中,我们将探讨如何解释不同的模型。我们将以泰坦尼克号数据为例进行说明。

>>> dt = DecisionTreeClassifier(
...     random_state=42, max_depth=3
... )
>>> dt.fit(X_train, y_train)

回归系数

截距和回归系数解释了预期值以及特征对预测的影响。 正系数表示随着特征值的增加,预测值也会增加。

特征重要性

scikit-learn 库 中基于树的模型包括一个.fea⁠ture_importances_ 属性,用于查看数据集的特征对模型的影响。我们可以检查或绘制它们。

LIME

LIME可以帮助解释黑箱模型。它执行的是局部解释,而不是整体解释。它有助于解释单个样本。

对于给定的数据点或样本,LIME 可以指出哪些特征在决定结果时很重要。线性模型近似于接近样本的模型(见图 13-1)。

下面的例子解释了训练数据中的最后一个样本(决策树预测该样本将存活):

>>> from lime import lime_tabular
>>> explainer = lime_tabular.LimeTabularExplainer(
...     X_train.values,
...     feature_names=X.columns,
...     class_names=["died", "survived"],
... )
>>> exp = explainer.explain_instance(
...     X_train.iloc[-1].values, dt.predict_proba
... )

LIME 不喜欢使用 DataFrames 作为输入。请注意,我们使用.values 将数据转换为 numpy 数组。

提示

如果您在 Jupyter 中执行此操作,请跟进此代码:

exp.show_in_notebook()

这将呈现 HTML 版本的解释。

如果想导出解释(或不使用 Jupyter),我们可以创建一个 matplotlib 图:

>>> fig = exp.as_pyplot_figure()
>>> fig.tight_layout()
>>> fig.savefig("images/mlpr_1301.png")
LIME explanation for the Titanic dataset. Features for the sample push the prediction toward the right (survival) or left (deceased).
图 13-1. 泰坦尼克号数据集的 LIME 解释。样本特征将预测推向右侧(存活)或左侧(死亡)。

玩一下这个,注意如果切换性别,结果会受到影响。下面我们以训练数据中倒数第二行为例。该行的预测结果是 48% 死亡,52% 幸存。如果我们切换性别,就会发现预测结果变为 88% 死亡:

>>> data = X_train.iloc[-2].values.copy()
>>> dt.predict_proba(
...     [data]
... )  # predicting that a woman lives
[[0.48062016 0.51937984]] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

雷达趋势观察:2025年7月

雷达趋势观察:2025年7月

Mike Loukides
R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
Python高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé

Publisher Resources

ISBN: 9798341663046