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

机器学习速查手册

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

第 12 章 度量和分类评估

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

本章将介绍以下指标和评估工具:混淆矩阵、各种指标、分类报告和一些可视化工具。

这将作为预测泰坦尼克号生还的决策树模型进行评估。

混淆矩阵

混淆矩阵有助于了解分类器的性能。

二元分类器有四种分类结果:真阳性 (TP)、真阴性 (TN)、假阳性 (FP) 和假阴性 (FN)。前两种是正确的分类。

下面是一个记住其他结果的常见例子。假设阳性表示怀孕,阴性表示没有怀孕,那么假阳性就好比声称一个男人怀孕了。假阴性是指声称一名孕妇没有怀孕(而她明显有怀孕迹象)(见图 12-1)。后两种误差分别称为1 型和2 型误差(见表 12-1)。

另一种记忆方法是,P(表示假阳性)中有一条直线(类型 1 错误),N(表示假阴性)中有两条垂直线。

Classification errors.
图 12-1. 分类错误
表 12-1. 混淆矩阵的二元分类结果
实际 预测阴性 预测阳性

实际负值

真阴性

假阳性(类型 1)

实际阳性

假阴性(类型 2)

真阳性

下面是计算分类结果的 pandas 代码,注释显示了计算结果。我们将使用这些变量来计算其他指标:

>>> y_predict = dt.predict(X_test)
>>> tp = (
...     (y_test == 1) & (y_test == y_predict)
... ).sum()  # 123
>>> tn = (
...     (y_test == 0) & (y_test == y_predict)
... ).sum()  # 199
>>> fp = (
...     (y_test == 0) & (y_test != y_predict)
... ).sum()  # 25
>>> fn = (
...     (y_test == 1) & (y_test != y_predict)
... ).sum()  # 46

表现良好的分类器最好在真对角线上有较高的计数。我们可以使用 sklearnconfusion_matrix 函数创建一个 DataFrame:

>>> from sklearn.metrics import confusion_matrix
>>> y_predict = dt.predict(X_test)
>>> pd.DataFrame(
...     confusion_matrix(y_test, y_predict),
...     columns=[
...         "Predict died",
...         "Predict Survive",
...     ],
...     index=["True Death", "True Survive"],
... )
              Predict died  Predict Survive
True Death             199               25
True Survive            46              123

Yellowbrick 提供了混淆矩阵图(见图 12-2):

>>> import matplotlib.pyplot as plt
>>> from yellowbrick.classifier import (
...     ConfusionMatrix,
... )
>>> 
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