【scikit-learn 1.2版本后】sklearn.datasets中load_boston报错 使用 fetch_openml 函数来加载波士顿房价

ImportError:
load_boston has been removed from scikit-learn since version 1.2.

由于 load_boston 已经在 scikit-learn 1.2 版本中被移除,需要使用 fetch_openml 函数来加载波士顿房价数据集。

python 复制代码
# 导入sklearn数据集模块
from sklearn import datasets
# 导入波士顿房价数据集
data_x, data_y = datasets.fetch_openml(name="boston", version=1, as_frame=True, return_X_y=True, parser="pandas")

这段代码的功能是从 OpenML 数据集库中获取名为"boston"的数据集,并将其加载为 Pandas DataFrame 格式。具体步骤如下:

  1. 调用 datasets.fetch_openml 函数。
  2. 指定数据集名称为 "boston",版本为 1。
  3. 设置 as_frame=True,使数据以 Pandas DataFrame 格式返回。
  4. 设置 return_X_y=True,返回特征数据和目标数据。
  5. 设置 parser="pandas",使用 Pandas 解析器。

以下是完整的报错信息,包含了修改的建议:

ImportError:
load_boston has been removed from scikit-learn since version 1.2.

The Boston housing prices dataset has an ethical problem: as investigated in [1], the authors of this dataset engineered a non-invertible variable "B" assuming that racial self-segregation had a positive impact on house prices [2]. Furthermore the goal of the research that led to the creation of this dataset was to study the impact of air quality but it did not give adequate demonstration of the

validity of this assumption.

The scikit-learn maintainers therefore strongly discourage the use of this dataset unless the purpose of the code is to study and educate about ethical issues in data science and machine learning.

In this special case, you can fetch the dataset from the original

source::

复制代码
import pandas as pd
import numpy as np

data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]

Alternative datasets include the California housing dataset and the Ames housing dataset. You can load the datasets as follows::

复制代码
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()

for the California housing dataset and::

复制代码
from sklearn.datasets import fetch_openml
housing = fetch_openml(name="house_prices", as_frame=True)

for the Ames housing dataset.

1\] M Carlisle. "Racist data destruction?" \[2\] Harrison Jr, David, and Daniel L. Rubinfeld. "Hedonic housing prices and the demand for clean air." Journal of environmental economics and management 5.1 (1978): 81-102.

相关推荐
m0_734949796 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
m0_514520577 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey7 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
Jmayday7 小时前
机器学习基本理论
人工智能·机器学习
王_teacher7 小时前
机器学习 矩阵求导 完整公式+严谨推导
人工智能·线性代数·考研·机器学习·矩阵·线性回归
Polar__Star8 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
m0_743623928 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python
xiaotao1319 小时前
02-机器学习基础: 无监督学习——scikit-learn实战与模型管理
学习·机器学习·scikit-learn
FreakStudio9 小时前
无硬件学LVGL—定时器篇:基于Web模拟器+MicroPython速通GUI开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
gCode Teacher 格码致知9 小时前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest