知道如何处理 mglearn 导入错误吗?

问题描述 投票:0回答:3

我是一名新的数据科学训练营学生。最近买了一本书《Introduction to Machine Learning with Pyhton》。然而,这本书大量使用了 mglearn 库。当我想导入库时出现错误。 (您可以从下面看到。)我无法演示书中提供的示例。有什么办法可以解决这个问题吗?

提前致谢!

ImportError                               Traceback (most recent call last)
Cell In [3], line 1
----> 1 import mglearn

File c:\Users\murad\AppData\Local\Programs\Python\Python310\lib\site-packages\mglearn\__init__.py:1
----> 1 from . import plots
      2 from . import tools
      3 from .plots import cm3, cm2

File c:\Users\murad\AppData\Local\Programs\Python\Python310\lib\site-packages\mglearn\plots.py:5
      3 from .plot_animal_tree import plot_animal_tree
      4 from .plot_rbf_svm_parameters import plot_svm
----> 5 from .plot_knn_regression import plot_knn_regression
      6 from .plot_knn_classification import plot_knn_classification
      7 from .plot_2d_separator import plot_2d_classification, plot_2d_separator

File c:\Users\murad\AppData\Local\Programs\Python\Python310\lib\site-packages\mglearn\plot_knn_regression.py:7
      4 from sklearn.neighbors import KNeighborsRegressor
      5 from sklearn.metrics import euclidean_distances
----> 7 from .datasets import make_wave
      8 from .plot_helpers import cm3
     11 def plot_knn_regression(n_neighbors=1):

File c:\Users\murad\AppData\Local\Programs\Python\Python310\lib\site-packages\mglearn\datasets.py:5
      3 import os
      4 from scipy import signal
----> 5 from sklearn.datasets import load_boston
      6 from sklearn.preprocessing import MinMaxScaler, PolynomialFeatures
      7 from .make_blobs import make_blobs

File c:\Users\murad\AppData\Local\Programs\Python\Python310\lib\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
...
[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>

我试图从网上找到答案,但找不到任何东西。

python machine-learning scikit-learn importerror
3个回答
2
投票

mglearn
包似乎依赖于其依赖项中已弃用和删除的功能。希望作者会升级他们的包,或者至少更好地指定它的依赖项。您可以在项目问题网页上了解用户遇到的几个问题。

要成功导入

mglearn
,这就是我所做的。

在专用目录中,我创建了一个虚拟环境:

python -m venv _venv

...然后激活它:

. _venv/bin/activate

然后我升级/安装了至少允许

import mglearn
完成而不会抛出错误的包。

python -m pip install --upgrade --upgrade-strategy eager pip setuptools ipython mglearn "scikit-learn==1.0.2" "joblib<0.12"

如果你需要安装更多的包,对这些包使用

--upgrade-strategy eager
可能不是最好的选择。 YMMV.

可能有一个

conda
等同于上述过程,但我不熟悉它。

当您使用

mglearn
功能时,可能会有更多的依赖问题需要解决。祝你好运!


0
投票

查看错误提示,发现:

C:< path-to-your-local-system >\site-packages\sklearn\datasets_init_.py

取消注释uncomment the line here, &,

取消注释指向此导入的函数: this function is to be commented out

希望对您有所帮助!


0
投票

我刚试过这个:

conda install scikit-learn=1.1

而且它“似乎”已经成功了。也就是说,01-introduction.ipynb notebook 运行完成。从 In [28]: 开始有一些 FutureWarnings,但我可以接受它们。当我完成这本书时,我将升级 scikit-learn 并避免 load_boston()

© www.soinside.com 2019 - 2024. All rights reserved.