Your array of examples actually gives the same problem as the scalar:
>>> a = np.array([1.0,2.0,3.0]) >>> np.sum(a, axis=1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.4/site-packages/numpy/core/fromnumeric.py", line 1724, in sum out=out, keepdims=keepdims) File "/usr/lib/python3.4/site-packages/numpy/core/_methods.py", line 32, in _sum return umr_sum(a, axis, dtype, out, keepdims) ValueError: 'axis' entry is out of bounds
The good news is that there is a numpy function just to ensure that numpy calls are made using axis=1 - this is called np.atleast_2d :
>>> np.sum(np.atleast_2d(a), axis=1) array([ 6.]) >>> np.sum(np.atleast_2d(6.0), axis=1) array([ 6.])
But since you obviously want a scalar answer, instead you can simply completely reject the axis argument:
>>> np.sum(a) 6.0 >>> np.sum(6.0) 6.0
source share