在多维数组上使用numpy.argmax() [英] Using numpy.argmax() on multidimensional arrays
问题描述
我有一个4维数组,即 data.shape =(20,30,33,288)
。我找到了最接近n的数组的索引
I have a 4 dimensional array, i.e., data.shape = (20,30,33,288)
. I am finding the index of the closest array to n using
index = abs(data - n).argmin(axis = 1), so
index.shape = (20,33,288) with the indices varying.
我想使用 data [index] =values
带 values.shape =(20,33,288)
,但 data [index]
返回错误index(8)超出范围(0< = index< 1)在维度0或此操作需要相对长的时间来计算并返回具有似乎不具有的形状的矩阵有意义。
I would like to use data[index] = "values"
with values.shape = (20,33,288)
, but data[index]
returns the error "index (8) out of range (0<=index<1) in dimension 0" or this operation takes a relatively long time to compute and returns a matrix with a shape that doesn't seem to make sense.
如何返回正确值的数组?即,
How do I return a array of correct values? i.e.,
data[index] = "values" with values.shape = (20,33,288)
这看起来像一个简单的问题,有一个简单的答案吗?
This seems like a simple problem, is there a simple answer?
我最终想找到 index2 = abs(data-n2).argmin(axis = 1)
,所以我可以执行一个操作,比如数据索引处的和数据在index2没有循环变量。这可能吗?
I would eventually like to find index2 = abs(data - n2).argmin(axis = 1)
, so I can perform an operation, say sum data at index to data at index2 without looping through the variables. Is this possible?
我使用的是python2.7和numpy版本1.5.1。
I am using python2.7 and numpy version 1.5.1.
推荐答案
您应该能够使用 numpy.indices()
index 索引的最大值>:
You should be able to access the maximum values indexed by index
using numpy.indices()
:
x, z, t = numpy.indices(index.shape)
data[x, index, z, t]
这篇关于在多维数组上使用numpy.argmax()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!