[英]Using numpy.argmax() on multidimensional arrays
I have a 4 dimensional array, ie, data.shape = (20,30,33,288)
. 我有一个4维数组,即
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.
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. 我想将
data[index] = "values"
与values.shape = (20,33,288)
,但data[index]
返回错误“index(8)out of range(0 <= index <1)in dimension 0“或此操作需要相对较长的时间来计算并返回具有似乎没有意义的形状的矩阵。
How do I return a array of correct values? 如何返回正确值的数组? ie,
即
data[index] = "values" with values.shape = (20,33,288)
This seems like a simple problem, is there a simple answer? 这似乎是一个简单的问题,有一个简单的答案吗?
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. 我最终想找到
index2 = abs(data - n2).argmin(axis = 1)
,所以我可以执行一个操作,比如索引处的数据和index2处的数据,而不循环遍历变量。 Is this possible? 这可能吗?
I am using python2.7 and numpy version 1.5.1. 我使用python2.7和numpy版本1.5.1。
You should be able to access the maximum values indexed by index
using numpy.indices()
: 您应该能够访问由索引的最大值
index
使用numpy.indices()
x, z, t = numpy.indices(index.shape)
data[x, index, z, t]
If I understood you correctly, this should work: 如果我理解正确,这应该有效:
numpy.put(data, index, values)
I learned something new today, thanks. 今天我学到了新东西,谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.