我在 IBM BlueMix 上运行一个 ipython 笔记本,sklearn 的默认版本是 0.11 版 -- 我想升级到 sklearn-0.18。
编辑:我的意思是我想升级到sklearn-0.17。
import sklearn
print sklearn.__version__
0.11
BlueMix 文档说要使用:
!pip install -U --user sklearn
这是输出:
You are using pip version 7.1.0, however version 8.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Requirement already up-to-date: sklearn in /gpfs/global_fs01/sym_shared/YPProdSpark/user/s3b2-519e0ce881616c-546ab68fb1ed/.local/lib/python2.7/site-packages
Collecting scikit-learn (from sklearn)
Using cached scikit-learn-0.17.tar.gz
Installing collected packages: scikit-learn
Running setup.py install for scikit-learn
Successfully installed scikit-learn-0.11
这似乎行不通。也许这与旧版本的pip有关...
!pip install --upgrade --user pip
You are using pip version 7.1.0, however version 8.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pip
Using cached pip-8.0.2-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-7.1.0
import pip
print pip.__version__
7.1.0
好的,所以这不仅仅是sklearn。问题似乎是以某种方式升级的软件包未安装在正确的路径中。
经过一番侦查,我发现:
!ls -al ~/.local/lib/python2.7/site-packages
total 0
drwx------ 8 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 18:06 .
drwx------ 3 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 15:30 ..
drwxr-xr-x 10 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 15:46 pip
drwxr-xr-x 2 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 15:53 pip-8.0.1.dist-info
drwxr-xr-x 2 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 18:40 pip-8.0.2.dist-info
drwxr-xr-x 2 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 18:06 scikit_learn-0.17-py2.7.egg-info
drwxr-xr-x 25 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 18:06 sklearn
drwxr-xr-x 2 s3b2-519e0ce881616c-546ab68fb1ed users 4096 Jan 22 15:48 sklearn-0.0-py2.7.egg-info
所以,让我们试试这个...
import sys
sys.path.insert(0, '/gpfs/fs01/user/s3b2-519e0ce881616c-546ab68fb1ed/.local/lib/python2.7/site-packages/')
reload(sklearn)
print sklearn.__version__
0.17
现在,看起来版本是正确的,所以尝试从 sklearn-0.17 导入一些新功能。
from sklearn.metrics import roc_auc_score
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-169-8985e3efac9f> in <module>()
----> 1 from sklearn.metrics import roc_auc_score
ImportError: cannot import name roc_auc_score
我对应该在 0.17 版中的任何内容都出现相同的错误。似乎python在尝试导入时引用了旧的sklearn版本0.11。
任何帮助,不胜感激。
解决了!
BlueMix 似乎每次登录时都会将 iPython 环境初始化为 sklearn-0.11。显然,一旦我导入了 sklearn verison 0.11,使用 0.17 版的新路径重新加载 sklearn 不会改变任何东西。
在进行初始导入之前,我需要修改路径以指向版本 0.17,然后初始导入将引用更新的版本,如下所示......
import sys
sys.path.insert(0, '/gpfs/fs01/user/s3b2-519e0ce881616c-546ab68fb1ed/.local/lib/python2.7/site-packages/')
import sklearn
print sklearn.__version__
0.17
现在,我可以成功执行以下操作而不会出错:
from sklearn.metrics import roc_auc_score
from sklearn.ensemble import RandomForestClassifier