I found a very unusual error when trying to calculate the standard deviation of a two dimensional numpy array. Basically, I'm doing this:
np.std(myarray, axis=1)
which gives the following error:
/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
In the line 105, ret is defined as:
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],...
buy I can't see anything wrong with that array. np.sum and np.mean work correctly.
What could be the cause of this error?
It seems that the problem is the dtype set as object
. Even if numpy allows it, it is generally a bad idea, as you lose most internal optimizations.
The exception is present only with the axis keyword :
>>> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With