7

在尝试计算二维 numpy 数组的标准偏差时,我发现了一个非常不寻常的错误。基本上,我正在这样做:

np.std(myarray, axis=1)

这给出了以下错误:

/home/user/env/local/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in std(a, axis, dtype, out, ddof, keepdims)
   2588 
   2589     return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
-> 2590                                 keepdims=keepdims)
   2591 
   2592 def var(a, axis=None, dtype=None, out=None, ddof=0,

/home/user/env/local/lib/python2.7/site-packages/numpy/core/_methods.pyc in _std(a, axis, dtype, out, ddof, keepdims)
    103 
    104     if isinstance(ret, mu.ndarray):
--> 105         ret = um.sqrt(ret, out=ret)
    106     else:
    107         ret = um.sqrt(ret)

AttributeError: sqrt

在第 105 行,ret 定义为:

array([0.0757800982464383, 0.6065241443345735, 0.3162436337971689,
       0.025387106329804794, 0.023465650294750118, 0.01234409423996419,
       0.03686346121524665, 0.456152653196993, 0.15598749370862977,
       0.0041977155187445945, 0.018816207536006213, 0.018011541017004237,
       0.01046808236307669, 0.0037176987848958156, 0.004346127061033225,
       0.06885161954332783, 0.004758430435294487, 0.010064124660786879,
       0.08732648466448349, 0.14957009536890314, 0.007277246755033778,
       0.0043521569980290355, 0.010174973078043143, 0.33905025844712544,
       0.7960121881423348], dtype=object)

type(myarray): <type 'numpy.ndarray'>

repr(myarray): array([[1.2258313, 1.2258313, 1.3756552, 1.1849703, 1.334794, 1.1849703,
            1.1441092, 1.334794, 1.3075534, 1.2258313, 1.3756552, 0.95342433,
            1.1441092, 1.0760075, 1.1168685, 1.1168685, 1.334794, 0.8036005,
            0.46309182, 0.3405087],
           [1.3756552, 0.95342433, 1.1441092, 1.0760075, 1.1168685, 1.1168685,
            1.334794, 0.8036005, 0.46309182, 0.3405087, 0.313268, 0.38136974,
            0.27240697, 0.38136974, -1.8387468999999996, -0.50395286,
            -0.14982383, -0.46309182, -0.3405087, -0.19068487],...

买我看不出那个阵列有什么问题。np.sum 和 np.mean 工作正常。

此错误的原因可能是什么?

4

1 回答 1

8

似乎问题在于 dtype 设置为object. 即使 numpy 允许它,这通常也是一个坏主意,因为您会丢失大多数内部优化。

例外只存在于轴关键字:

>>> import numpy as np
>>> a = np.arange(10).reshape(5,2)
>>> b = np.arange(10, dtype=object).reshape(5,2)
>>> np.std(a)
2.8722813232690143
>>> np.std(a, axis=1)
array([ 0.5,  0.5,  0.5,  0.5,  0.5])
>>> np.std(b)
2.8722813232690143
>>> np.std(b, axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2590, in std
    keepdims=keepdims)
  File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 105, in _std
    ret = um.sqrt(ret, out=ret)
AttributeError: sqrt
于 2013-08-19T14:14:01.683 回答