查找不带循环的三维矩阵的最大行?

问题描述 投票:0回答:1

我有一个形状为(2,4,4)的矩阵,并且希望为每个矩阵按行获取最大值。

w = array([
       [[0.9695303 , 0.99844801, 0.99712014, 0.99493009],
        [0.99111579, 0.99997843, 0.99995245, 0.99998065],
        [0.99940958, 0.99998028, 0.99999291, 0.99996183],
        [0.99701626, 0.99997955, 0.99998736, 0.99998028]],

       [[0.99992947, 0.99999852, 0.99988576, 0.99975061],
        [0.99999059, 1.        , 0.99999982, 0.99999987],
        [0.99999622, 0.99999945, 0.99999837, 0.99999683],
        [0.99998404, 0.99999998, 0.99999842, 0.99999804]]
])

我想要的输出应该是

[array([0.99844801, 0.99998065, 0.99999291, 0.99998736]),
 array([0.99999852, 1.        , 0.99999945, 0.99999998])]

下面给出幼稚的方式

max_by_row = []
for matrix in w:
  max_by_row.append(np.max(matrix,axis=1))

print(max_by_row)

有没有没有循环的方法吗?

python python-3.x numpy tensorflow2.0
1个回答
0
投票

您可以使用list comprehension

max_by_row = [np.max(matrix,axis=1) for matrix in w]
© www.soinside.com 2019 - 2024. All rights reserved.