在学习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、就接着愉快的玩耍了。
标签:datasets,boston,scikit,导入,learn,import,sklearn From: https://www.cnblogs.com/guochaoxxl/p/17034057.html