当前位置: 首页 > news >正文

【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 函数来加载波士顿房价数据集。

# 导入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 npdata_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?” https://medium.com/@docintangible/racist-data-destruction-113e3eff54a8

[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.
https://www.researchgate.net/publication/4974606_Hedonic_housing_prices_and_the_demand_for_clean_air

http://www.lryc.cn/news/476968.html

相关文章:

  • vxe-table v4.8+ 与 v3.10+ 导出 xlsx、支持导出合并、设置样式、宽高、边框、字体、背景、超链接、图片的详细介绍,一篇就够了
  • 江协科技STM32学习- P36 SPI通信外设
  • 【大数据】ClickHouse常见的表引擎及建表语法
  • explain执行计划分析 ref_
  • 网络学习/复习4传输层
  • Notepad++ 更改字体大小和颜色
  • 基于SSM+小程序的宿舍管理系统(宿舍1)
  • 【案例分享】TeeChart 如何为人类绩效解决方案提供数据洞察
  • 细谈 Linux 中的多路复用epoll
  • 51c自动驾驶~合集4
  • 回归预测 | MATLAB实现BO-BiGRU贝叶斯优化双向门控循环单元多输入单输出回归预测
  • 2-ARM Linux驱动开发-设备树平台驱动
  • C语言函数与递归
  • Linux下的Debugfs
  • 【FFmpeg】调整音频文件的音量
  • mac 打开访达快捷键
  • Ubuntu学习笔记 - Day2
  • c++基础12比较/逻辑运算符
  • mac-ubuntu虚拟机(扩容-共享-vmtools)
  • 数学建模学习(135):使用Python基于WSM、WPM、WASPAS的多准则决策分析
  • VScode的C/C++点击转到定义,不是跳转定义而是跳转声明怎么办?(内附详细做法)
  • 设备管理网关(golang版本)
  • Armv8的安全启动
  • 冒泡排序、选择排序、计数排序、插入排序、快速排序、堆排序、归并排序JAVA实现
  • SQL CASE表达式与窗口函数
  • 基于SpringBoot的植物园管理小程序【附源码】
  • asp.net网站项目如何设置定时器,定时获取数据
  • 单元/集成测试解决方案
  • 高效作业跟踪:SpringBoot作业管理系统
  • keepalived + nginx 实现网站高可用性(HA)