We have a numpy-based algorithm that is supposed to handle data of different type.
我们有一个基于numpy的算法,应该处理不同类型的数据。
def my_fancy_algo(a):
b = np.sum(a, axis=1)
# Do something b
return b
If we pass a=np.array[1.0, 2.0, 3.0]
then b
evaluates to [6.0]
.
如果我们传递a = np.array [1.0,2.0,3.0],那么b的计算结果为[6.0]。
If we pass a=6.0
then we get
如果我们通过= 6.0然后我们得到
*** ValueError: 'axis' entry is out of bounds
The desired behavior would be that we get same return value 6.0
not ([6.0]
) for both inputs.
期望的行为是我们得到两个输入的相同返回值6.0而不是([6.0])。
What is the correct pythonic and safe way to handle this? type
? shape
?
什么是正确的pythonic和安全的方法来处理这个?类型?形状?
2 个解决方案
#1
9
Your example array actually gives the same problem as a 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's a numpy function exactly for ensuring making numpy calls with axis=1
will work - it's called np.atleast_2d
:
好消息是,有一个numpy函数正是为了确保使用axis = 1的numpy调用可以工作 - 它被称为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 apparently want a scalar answer, you could instead just drop the axis
argument entirely:
但是因为你显然想要一个标量答案,所以你可以完全放弃轴参数:
>>> np.sum(a)
6.0
>>> np.sum(6.0)
6.0
#2
3
np.sum(np.array([1.0, 2.0, 3.0]), axis=1)
produces ValueError: 'axis' entry is out of bounds
for me.
np.sum(np.array([1.0,2.0,3.0]),axis = 1)产生ValueError:'axis'条目对我来说超出范围。
Did you mean to put axis=0
in line 2? Then it works for arrays as well as scalars:
你的意思是把轴= 0放在第2行吗?然后它适用于数组和标量:
>>> np.sum(np.array([1.0, 2.0, 3.0]), axis=0)
6
>>> np.sum(3, axis=0)
3
#1
9
Your example array actually gives the same problem as a 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's a numpy function exactly for ensuring making numpy calls with axis=1
will work - it's called np.atleast_2d
:
好消息是,有一个numpy函数正是为了确保使用axis = 1的numpy调用可以工作 - 它被称为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 apparently want a scalar answer, you could instead just drop the axis
argument entirely:
但是因为你显然想要一个标量答案,所以你可以完全放弃轴参数:
>>> np.sum(a)
6.0
>>> np.sum(6.0)
6.0
#2
3
np.sum(np.array([1.0, 2.0, 3.0]), axis=1)
produces ValueError: 'axis' entry is out of bounds
for me.
np.sum(np.array([1.0,2.0,3.0]),axis = 1)产生ValueError:'axis'条目对我来说超出范围。
Did you mean to put axis=0
in line 2? Then it works for arrays as well as scalars:
你的意思是把轴= 0放在第2行吗?然后它适用于数组和标量:
>>> np.sum(np.array([1.0, 2.0, 3.0]), axis=0)
6
>>> np.sum(3, axis=0)
3