【发布时间】:2017-03-24 22:21:05
【问题描述】:
我有以下数组。
a = np.array([[0, 5, 0, 5],
[0, 9, 0, 9]])
>>>a.shape
Out[72]: (2, 4)
>>>np.all(a,axis=0)
Out[69]:
array([False, True, False, True], dtype=bool)
>>>np.all(a,axis=1)
Out[70]:
array([False, False], dtype=bool)
因为轴 0 表示二维数组中的第一个轴(按行),
我希望在给出np.all(a,axis=0) 时,它会检查每一行的所有元素是否为真。
但似乎检查每列,因为它会输出 4 个元素,例如 array([False, True, False, True], dtype=bool)。
我对 np.all 的功能有什么误解?
【问题讨论】:
-
axis=0是 2D 的每列。轴从左开始为 0、1、2 等。 -
我记得看到一个很好的副本,但我找不到它。
-
将
axis参数视为您正在折叠的轴。当您传递axis=0时,您会将所有行折叠成一行。 -
stackoverflow.com/questions/41733479/… - 解释了“沿轴求和”的含义。