【发布时间】:2014-06-15 23:21:20
【问题描述】:
当涉及nans 和零时,我注意到numpy.dot 中的行为不一致。
任何人都可以理解它吗?这是一个错误吗?这是dot 函数特有的吗?
我正在使用 numpy v1.6.1,64 位,在 linux 上运行(也在 v1.6.2 上测试过)。我还在 windows 32bit 上的 v1.8.0 上进行了测试(所以我无法判断差异是由于版本、操作系统还是拱门)。
from numpy import *
0*nan, nan*0
=> (nan, nan) # makes sense
#1
a = array([[0]])
b = array([[nan]])
dot(a, b)
=> array([[ nan]]) # OK
#2 -- adding a value to b. the first value in the result is
# not expected to be affected.
a = array([[0]])
b = array([[nan, 1]])
dot(a, b)
=> array([[ 0., 0.]]) # EXPECTED : array([[ nan, 0.]])
# (also happens in 1.6.2 and 1.8.0)
# Also, as @Bill noted, a*b works as expected, but not dot(a,b)
#3 -- changing a from 0 to 1, the first value in the result is
# not expected to be affected.
a = array([[1]])
b = array([[nan, 1]])
dot(a, b)
=> array([[ nan, 1.]]) # OK
#4 -- changing shape of a, changes nan in result
a = array([[0],[0]])
b = array([[ nan, 1.]])
dot(a, b)
=> array([[ 0., 0.], [ 0., 0.]]) # EXPECTED : array([[ nan, 0.], [ nan, 0.]])
# (works as expected in 1.6.2 and 1.8.0)
案例 #4 似乎在 v1.6.2 和 v1.8.0 中正常工作,但案例 #2 则不然......
编辑:@seberg 指出这是一个 blas 问题,所以这是我通过运行 from numpy.distutils.system_info import get_info; get_info('blas_opt') 找到的有关 blas 安装的信息:
1.6.1 linux 64bit
/usr/lib/python2.7/dist-packages/numpy/distutils/system_info.py:1423: UserWarning:
Atlas (http://math-atlas.sourceforge.net/) libraries not found.
Directories to search for the libraries can be specified in the
numpy/distutils/site.cfg file (section [atlas]) or by setting
the ATLAS environment variable.
warnings.warn(AtlasNotFoundError.__doc__)
{'libraries': ['blas'], 'library_dirs': ['/usr/lib'], 'language': 'f77', 'define_macros': [('NO_ATLAS_INFO', 1)]}
1.8.0 windows 32bit (anaconda)
c:\Anaconda\Lib\site-packages\numpy\distutils\system_info.py:1534: UserWarning:
Blas (http://www.netlib.org/blas/) sources not found.
Directories to search for the sources can be specified in the
numpy/distutils/site.cfg file (section [blas_src]) or by setting
the BLAS_SRC environment variable.
warnings.warn(BlasSrcNotFoundError.__doc__)
{}
(我个人不知道该怎么做)
【问题讨论】:
-
案例 2 很有趣,
a*b给出了想要的结果,但np.dot(a,b)没有。 -
dot 的结果取决于你使用的 blas 库。例如,我在 openblas 上看到了同样的情况(但不是在 atlas 上),所以要么这是未指定的,要么是 blas 库中的错误。乘法真的不相关......
-
嗯,试试
from numpy.distutils.system_info import get_info; get_info('blas_opt') -
@seberg,谢谢。我将信息添加到问题中