本文介绍了沿Numpy ndarray轴的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恐怕我无法描述问题,所以我要画一个草图.无论如何,我需要的是在一个numpy ndarray中找到沿着第0轴的最大值,即array.shape(5, 5,3)及其对应的层号",并使用层号"创建形状为(1,5,3)的新2d数组.希望在此给出清晰的说明.很多.

I'm afraid that I can't describe the problem so I draw a sketch of it.Anyway,what I need is to find the max values along the 0th axis in a numpy ndarray,i.e.array.shape(5,5,3), and their corresponding "layer numbers", and use the "layer numbers" to create a new 2d array with shape of (1,5,3).Hope I'm giving a clear description here..thanks a lot.

推荐答案

如果查看np.max的文档,则会看到它带有axis自变量:

If you check the documentation of np.max, you'll see it takes an axis argument:

a.max(axis=0)

但是那对您没有帮助.但是,有一个函数argmax为您提供沿给定轴的最大值的索引:

But that won't help you yet. However, there's a function argmax that gives you the indices of the maxima along a given axis:

a.argmax(axis=...)

因此,让我们找到您的第一个(5,5)数组:它是a[...,0].您可以使用a[...,0].max(axis=1)(或0)找到每行(或每一列)的最大值的位置,然后使用它来查找另一侧的值.

So, let's find your first (5,5) array: it's a[...,0]. You can find the position of the maxima per rows (or columns) with a[...,0].max(axis=1) (or 0), and use that to find the values on the other sides.

这篇关于沿Numpy ndarray轴的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 12:45