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

机器学习速查手册

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

第 14 章 回归 回归

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

回归是一种有监督的机器学习过程。它与分类类似,但我们试图预测的不是一个标签,而是一个连续值。如果你想预测一个数字,那就使用回归。

事实证明,sklearn 支持许多用于回归问题的相同分类模型。事实上,API 也是一样的,调用.fit,.score, 和.predict 。新一代提升库 XGBoost 和 LightGBM 也是如此。

虽然分类模型和超参数有相似之处,但回归的评估指标有所不同。本章将回顾多种类型的回归模型。我们将使用波士顿住房数据集来探讨这些模型。

在这里,我们将加载数据,创建一个用于训练和测试的分割版本,并创建另一个包含标准化数据的分割版本:

>>> import pandas as pd
>>> from sklearn.datasets import load_boston
>>> from sklearn import (
...     model_selection,
...     preprocessing,
... )
>>> b = load_boston()
>>> bos_X = pd.DataFrame(
...     b.data, columns=b.feature_names
... )
>>> bos_y = b.target

>>> bos_X_train, bos_X_test, bos_y_train, bos_y_test = model_selection.train_test_split(
...     bos_X,
...     bos_y,
...     test_size=0.3,
...     random_state=42,
... )


>>> bos_sX = preprocessing.StandardScaler().fit_transform(
...     bos_X
... )
>>> bos_sX_train, bos_sX_test, bos_sy_train, bos_sy_test = model_selection.train_test_split(
...     bos_sX,
...     bos_y,
...     test_size=0.3,
...     random_state=42,
... )

以下是对住房数据集特征的描述,摘自数据集:

CRIM

各城镇人均犯罪率

ZN

占地面积超过 25,000 平方英尺的住宅用地比例

INDUS

各城镇非零售商业用地比例

CHAS

查尔斯河虚拟变量(如果有界河,则为 1;否则为 0

NOX

一氧化氮浓度(千万分率)

RM

每栋住宅的平均房间数

年龄

1940 年前建成的业主自住单元比例

辐射

到波士顿五个就业中心的加权距离

RAD

辐射高速公路可达性指数

税收

每 10,000 美元的房产税全额税率

税率

各城镇的师生比率

B

1000(Bk-0.63)^2,其中 Bk 为各城镇黑人比例(该数据集来自 1978 年)

LSTAT

地位较低人口的百分比

媒体

以 1000 美元为增量的自有住房中值

基线模型

基线回归模型将为我们提供与其他模型进行比较的依据。在 sklearn 中,.score 方法的默认结果是决定系数(r² 或 R²)。该值通常介于 0 和 1 之间,但在模型特别糟糕的情况下也可能是负值。

DummyRegressor 的默认策略是预测训练集的平均值。我们可以看到,这个模型的表现并不是很好: ...

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