解决scikit-learn中导入数据的问题

  在学习sklearn时,使用boston房价数据时,使用如下代码获取数据:

1、无法导入数据,我这里版本是1.2,具体:

import pandas as pd
import numpy as np

import sklearn
from sklearn import datasets #导入数据

boston = datasets.load_boston()
boston

错误如下:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 7
      4 import sklearn
      5 from sklearn import datasets #导入数据
----> 7 boston = datasets.load_boston()
      8 boston

File ~/.local/lib/python3.10/site-packages/sklearn/datasets/__init__.py:156, in __getattr__(name)
    105 if name == "load_boston":
    106     msg = textwrap.dedent(
    107         """
    108         `load_boston` has been removed from scikit-learn since version 1.2.
   (...)
    154         """
    155     )
--> 156     raise ImportError(msg)
    157 try:
    158     return globals()[name]

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?"
<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>

提示信息主要:

`load_boston` has been removed from scikit-learn since version 1.2.,也就是1.2库中的load_boston方法被移除:给的解决办法:

2、可以使用如下方法解决:

import pandas as pd
import numpy as np

import sklearn
from sklearn import datasets #导入数据集合

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]
data

  但是发现上述解决方法与鸢尾花中的解决方法不一致,还是有点不习惯

2、后来发现用版本1.1.1就没有上述问题,查看版本:

pip list | grep scik
scikit-learn                  1.2.0

卸载新版本1.2.0

pip uninstall scikit-learn                                                                                           INT ✘
Found existing installation: scikit-learn 1.2.0
Uninstalling scikit-learn-1.2.0:
  Would remove:
    /home/nication/.local/lib/python3.10/site-packages/scikit_learn-1.2.0.dist-info/*
    /home/nication/.local/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
    /home/nication/.local/lib/python3.10/site-packages/sklearn/*
Proceed (Y/n)? y
  Successfully uninstalled scikit-learn-1.2.0

中间过程需要输入y确认

3、可以安排指定版本1.1.1的scikit-learn

pip install scikit-learn==1.1.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

4、就接着愉快的玩耍了。

后来发现可以直接安装指定版本的scikit-learn,会自动卸载其他版本的,加入你已经安装scikit-learn 1.2.0,如果再使用安装1.1.1的版本时,也就是执行第3步的命令时,会自动先删除scikit-learn 1.2.0, 再安装scikit-learn 1.1.1,请看:

pip install scikit-learn==1.1.1 -i https://pypi.tuna.tsinghua.edu.cn/simple                                              ✔
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting scikit-learn==1.1.1
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/43/bc/7130ffd49a1cf72659c61eb94d8f037bc5502c94866f407c0219d929e758/scikit_learn-1.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.4 MB)
Requirement already satisfied: numpy>=1.17.3 in ./.local/lib/python3.10/site-packages (from scikit-learn==1.1.1) (1.23.5)
Requirement already satisfied: scipy>=1.3.2 in ./.local/lib/python3.10/site-packages (from scikit-learn==1.1.1) (1.9.3)
Requirement already satisfied: threadpoolctl>=2.0.0 in ./.local/lib/python3.10/site-packages (from scikit-learn==1.1.1) (3.1.0)
Requirement already satisfied: joblib>=1.0.0 in ./.local/lib/python3.10/site-packages (from scikit-learn==1.1.1) (1.2.0)
Installing collected packages: scikit-learn
  Attempting uninstall: scikit-learn
    Found existing installation: scikit-learn 1.2.0
    Uninstalling scikit-learn-1.2.0:
      Successfully uninstalled scikit-learn-1.2.0
Successfully installed scikit-learn-1.1.1

看到了吗,你学废了吗

 

posted @ 2023-01-08 07:20  叕叒双又  阅读(12876)  评论(2编辑  收藏  举报