如果我有一个形状数组(500,363,3)看起来像下面的形状,如何将其减小为形状(500,363,1),其中每个值都是一个与结果相等的布尔值全部()。那么[False,False,False]会变成False吗?
Array([[[False, False, False],
[False, False, False],
[False, False, False],
...,
[False, False, False],
[False, False, False],
[False, False, False]],
[[False, False, False],
[False, False, False],
[False, False, False],
...,
[False, False, False],
[False, False, False],
[False, False, False]],
[[False, False, False],
[False, False, False],
[False, False, False],
...,
[False, False, False],
[False, False, False],
[False, False, False]],
...,
[[False, False, False],
[False, False, False],
[False, False, False],
...,
[False, False, False],
[False, False, False],
[False, False, False]],
[[False, False, False],
[False, False, False],
[False, False, False],
...,
[False, False, False],
[False, False, False],
[False, False, False]],
[[False, False, False],
[False, False, False],
[False, False, False],
...,
[False, False, False],
[False, False, False],
[False, False, False]]])
答案 0 :(得分:3)
np.all
有一个轴参数,在这种情况下,您想沿最后一个轴取all
,因此您需要:
a.all(-1)
a = np.random.choice([0,1], size=(500, 363, 3))
print(a.all(-1).shape)
# (500, 363)
答案 1 :(得分:2)
您可以
import numpy as np
reduced = np.all(arr, axis = 2)