因为在Anaconda更新了模块,再次使用jupyter运行代码,多个包/方法出现无法调用/名字不存在的问题。
最终明确这是模块版本问题。
无法调用sklearn.tree.DecisionTreeClassifier,是因为sklearn新版本删除了,所以无法调用,安装以前的版本就可以调用了。
#查看python位置
(base) C:\Users\fengc>where python
C:\ProgramData\Anaconda3\python.exe
C:\Users\fengc\AppData\Local\Microsoft\WindowsApps\python.exe
#查看python版本重要
(base) C:\Users\fengc>python -V
Python 3.6.5 :: Anaconda custom (64-bit)
#查看环境
(base) C:\Users\fengc>conda info -e
# conda environments:
#
base * C:\ProgramData\Anaconda3
rstudio C:\ProgramData\Anaconda3\envs\rstudio
#查看pip包的版本
(base) C:\Users\fengc>pip -V
pip 20.0.2 from C:\ProgramData\Anaconda3\lib\site-packages\pip (python 3.6)
#打开python
(base) C:\Users\fengc>python
Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
#python中运行,出错,无法导入
>>> from sklearn.tree import DecisionTreeClassifier
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'DecisionTreeClassifier'
>>> exit()
#查看scikit版本
(base) C:\Users\fengc>conda list scikit
# packages in environment at C:\ProgramData\Anaconda3:
#
# Name Version Build Channel
scikit-learn 0.22.2.post1 pypi_0 pypi
#安装匹配的版本(自动卸载原版本)
(base) C:\Users\fengc>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U scikit-learn==0.20.3
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting scikit-learn==0.20.3
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e7/e4/56757c30853899077cf042f78fb981c0b60f7af4e7a54ca18328b80e0bff/scikit_learn-0.20.3-cp36-cp36m-win_amd64.whl (4.8 MB)
|████████████████████████████████| 4.8 MB 819 kB/s
Requirement already satisfied, skipping upgrade: numpy>=1.8.2 in c:\programdata\anaconda3\lib\site-packages (from scikit-learn==0.20.3) (1.18.3)
Requirement already satisfied, skipping upgrade: scipy>=0.13.3 in c:\programdata\anaconda3\lib\site-packages (from scikit-learn==0.20.3) (1.4.1)
Installing collected packages: scikit-learn
Attempting uninstall: scikit-learn
Found existing installation: scikit-learn 0.22.2.post1
Uninstalling scikit-learn-0.22.2.post1:
Successfully uninstalled scikit-learn-0.22.2.post1
Successfully installed scikit-learn-0.20.3
#再次调用DecisionTreeClassifier,成功
(base) C:\Users\fengc>python
Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn.tree import DecisionTreeClassifier
>>>