在多维数组上使用 numpy.argmax()
我有一个 4 维数组,即 data.shape = (20,30,33,288)
。 查找最接近 n 的数组的索引
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"
和 values.shape = (20,33,288)
,但data[index]
返回错误“index (8) out of range (0<=index<1) in Dimension 0”或者此操作需要相对较长的时间计算并返回一个矩阵形状似乎没有意义。
如何返回正确值的数组?即,
data[index] = "values" with values.shape = (20,33,288)
这似乎是一个简单的问题,有一个简单的答案吗?
我最终希望找到 index2 = abs(data - n2).argmin(axis = 1)
,这样我就可以执行一个操作,例如将索引处的数据与索引2处的数据求和,而无需循环遍历变量。这可能吗?
我正在使用 python2.7 和 numpy 版本 1.5.1。
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.
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?
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?
I am using python2.7 and numpy version 1.5.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用
numpy.indices()
访问由index
索引的最大值:You should be able to access the maximum values indexed by
index
usingnumpy.indices()
:如果我理解正确的话,这应该有效:
我今天学到了一些新东西,谢谢。
If I understood you correctly, this should work:
I learned something new today, thanks.