已导入 sklearn 但无法调用 sklearn.linear_model
imported sklearn but can't call sklearn.linear_model
我已经安装了 sklearn 并且能够使用命令导入 sklearn
导入 sklearn
但是我发现sklearn的子模块无法被sklearn.module_name调用,eg:
In [1]: import sklearn
In [2]: sklearn.linear_model
Traceback (most recent call last):
File "<ipython-input-2-e64c575ed22e>", line 1, in <module>
sklearn.linear_model
AttributeError: 'module' object has no attribute 'linear_model'
同时我可以直接导入sklearn.linear_model
In [3]: import sklearn.linear_model
In [4]: sklearn.linear_model
Out[4]: <module 'sklearn.linear_model' from
'C:\Users\sng\AppData\Local\conda\conda\envs\python2\lib\site-
packages\sklearn\linear_model\__init__.pyc'>
以上不成立,例如在matplotlib中:
In [5]: import matplotlib
In [6]: matplotlib.pyplot
Out[6]: <module 'matplotlib.pyplot' from
'C:\Users\sng\AppData\Local\conda\conda\envs\python2\lib\site-
packages\matplotlib\pyplot.pyc'>
我很好奇这是怎么回事?包 sklearn 与其他包不同吗?我的 IDE 是 python 2.7
的 Spyder
没有。只是当您调用 import matplotlib
时,它会在内部导入 pyplot,因此您可以按原样使用它,而无需专门导入 pyplot
。
看看执行import matplotlib
时导入的the source of the file。它有一些行,如:
...
...
from matplotlib.cbook import (
_backports, mplDeprecation, dedent, get_label, sanitize_sequence)
from matplotlib.compat import subprocess
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
...
...
其中一些是从 cbook
、compat
、rcsetup
导入的,我确定反过来从某个地方的 pyplot 导入 pyplot
或 classes。
sklearn 不是这种情况。 sklearn的__init__.py
看源码](https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/init.py#L15)
它只有以下导入:
import sys
import re
import warnings
import os
from contextlib import contextmanager as _contextmanager
import logging
决不会使用 linear_model
因此出现错误。
您可以通过像这样导入相关包来验证此行为:
import sklearn
import sklearn.svm
现在当你执行sklearn.linear_model
时,你会得到正确的输出:
<module 'sklearn.linear_model' from '/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/__init__.pyc'>
因为,这一次,至少定义的导入 in svm init 正在某处使用(导入)linear_model
的一些 class。因此 linear_model 可用于系统。
我已经安装了 sklearn 并且能够使用命令导入 sklearn 导入 sklearn
但是我发现sklearn的子模块无法被sklearn.module_name调用,eg:
In [1]: import sklearn
In [2]: sklearn.linear_model
Traceback (most recent call last):
File "<ipython-input-2-e64c575ed22e>", line 1, in <module>
sklearn.linear_model
AttributeError: 'module' object has no attribute 'linear_model'
同时我可以直接导入sklearn.linear_model
In [3]: import sklearn.linear_model
In [4]: sklearn.linear_model
Out[4]: <module 'sklearn.linear_model' from
'C:\Users\sng\AppData\Local\conda\conda\envs\python2\lib\site-
packages\sklearn\linear_model\__init__.pyc'>
以上不成立,例如在matplotlib中:
In [5]: import matplotlib
In [6]: matplotlib.pyplot
Out[6]: <module 'matplotlib.pyplot' from
'C:\Users\sng\AppData\Local\conda\conda\envs\python2\lib\site-
packages\matplotlib\pyplot.pyc'>
我很好奇这是怎么回事?包 sklearn 与其他包不同吗?我的 IDE 是 python 2.7
的 Spyder没有。只是当您调用 import matplotlib
时,它会在内部导入 pyplot,因此您可以按原样使用它,而无需专门导入 pyplot
。
看看执行import matplotlib
时导入的the source of the file。它有一些行,如:
...
...
from matplotlib.cbook import (
_backports, mplDeprecation, dedent, get_label, sanitize_sequence)
from matplotlib.compat import subprocess
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
...
...
其中一些是从 cbook
、compat
、rcsetup
导入的,我确定反过来从某个地方的 pyplot 导入 pyplot
或 classes。
sklearn 不是这种情况。 sklearn的__init__.py
看源码](https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/init.py#L15)
它只有以下导入:
import sys
import re
import warnings
import os
from contextlib import contextmanager as _contextmanager
import logging
决不会使用 linear_model
因此出现错误。
您可以通过像这样导入相关包来验证此行为:
import sklearn
import sklearn.svm
现在当你执行sklearn.linear_model
时,你会得到正确的输出:
<module 'sklearn.linear_model' from '/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/__init__.pyc'>
因为,这一次,至少定义的导入 in svm init 正在某处使用(导入)linear_model
的一些 class。因此 linear_model 可用于系统。