scikit-learn版本不匹配__version__ '和' pip show scikit-learn '.<



在SO上有两个类似的帖子(post1, post2),询问类似的情况,然而,在遵循这些问题中的所有步骤之后,我仍然无法弄清楚为什么sklearn版本不匹配。

我在一个云托管的Jupyter笔记本上工作,我假设运行时来自一些我不确定它是如何设置的图像。

以下输出确保:

  • 使用的pip与正确的python相关联。
  • pip安装的sklearn确实是一个特定的版本。
  • sklearn.__version__.
  • 输出输入:

import sklearn
# Ensures that the proper bin is used:
print("echo $PATH output: ")
!echo $PATH
print()
# Shows that the scikit-learn version is 1.0.2
print("Attempting to upgrade scikit-learn with the correct pip: ")
!python3 -m pip install scikit-learn --upgrade
print()
# Check if the pip used is the one associated with the Python I'm using:
print("which pip output: ")
!which pip
!pip --version
!python3 -m pip --version
print()
# Doesn't matter which Python I call, they all point to 3.7
print("All python points to 3.7: ")
!which python3
!ls -lah /opt/conda/bin | grep python
print()
# Shows scikit-learn version 1.0.2
print("pip list grep output: ")
!pip list | grep scikit
print()
# Shows scikit-learn version 1.0.2
print("pip show output: ")
!pip show scikit-learn
print()
# Shows scikit-learn version 0.22.2
print("sklearn.__version__ output: ")
print(sklearn.__version__)
print()
# sklearn is in the correct python3.7 sub-directory
print("sklearn.__path__ output: ")
print(sklearn.__path__)
输出:

echo $PATH output: 
/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Attempting to upgrade scikit-learn with the correct pip: 
Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.7/site-packages (1.0.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (3.1.0)
Requirement already satisfied: numpy>=1.14.6 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.18.4)
Requirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (0.14.1)
Requirement already satisfied: scipy>=1.1.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.4.1)
which pip output: 
/opt/conda/bin/pip
pip 23.0.1 from /opt/conda/lib/python3.7/site-packages/pip (python 3.7)
pip 23.0.1 from /opt/conda/lib/python3.7/site-packages/pip (python 3.7)
All python points to 3.7: 
/opt/conda/bin/python3
-rwxr-xr-x  1 jovyan users  230 Apr  8 22:30 ipython
-rwxr-xr-x  1 jovyan users  230 Apr  8 22:30 ipython3
lrwxrwxrwx  1 jovyan users    9 May  3  2020 python -> python3.7
lrwxrwxrwx  1 jovyan users    9 May  3  2020 python3 -> python3.7
-rwxrwxr-x  1 jovyan users  13M May  3  2020 python3.7
lrwxrwxrwx  1 jovyan users   17 May  3  2020 python3.7-config -> python3.7m-config
lrwxrwxrwx  1 jovyan users    9 May  3  2020 python3.7m -> python3.7
-rwxrwxr-x  1 jovyan users 3.3K May  3  2020 python3.7m-config
lrwxrwxrwx  1 jovyan users   17 May  3  2020 python3-config -> python3.7m-config
pip list grep output: 
scikit-image           0.16.2
scikit-learn           1.0.2
pip show output: 
Name: scikit-learn
Version: 1.0.2
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: 
Author-email: 
License: new BSD
Location: /opt/conda/lib/python3.7/site-packages
Requires: joblib, numpy, scipy, threadpoolctl
Required-by: sklearn
sklearn.__version__ output: 
0.22.2.post1
sklearn.__path__ output: 
['/opt/conda/lib/python3.7/site-packages/sklearn']

故障排除企图:

  • 验证使用了正确的pip
  • pip upgrade sklearn
  • 检查所有python版本指向同一个(即Python3.7)
  • 比较sklearn.__path__匹配pip安装到
  • 的位置

经过一番挖掘,下面的过程解决了版本不匹配问题。

  1. Pip安装所需的scikit-learn版本!python3 -m pip install --force-reinstall "scikit-learn==1.0.2"

  2. 重启Jupyter notebook内核

  3. 导入sklearn并验证版本:

!pip show scikit-learn      # Should show v1.0.2
import sklearn
print(sklearn.__version__)  # Should also show v1.0.2

故事寓意:

在任何更新或安装后重新启动内核并重新导入,以便更新后的库反映在ipynb中。

相关内容

  • 没有找到相关文章

最新更新