numpy中的一些数组索引 [英] Some array indexing in numpy
本文介绍了numpy中的一些数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
lookup = np.array([60, 40, 50, 60, 90])
以下数组中的值等于查找索引.
The values in the following arrays are equal to indices of lookup.
a = np.array([1, 2, 0, 4, 3, 2, 4, 2, 0])
b = np.array([0, 1, 2, 3, 3, 4, 1, 2, 1])
c = np.array([4, 2, 1, 4, 4, 0, 4, 4, 2])
array 1st column elements lookup value
a 1 --> 40
b 0 --> 60
c 4 --> 90
最大为90.
所以,结果的第一个元素是4.
So, first element of result is 4.
这样,
预期结果= array([4,2,0,4,4,4,4,4,0])
expected result = array([4, 2, 0, 4, 4, 4, 4, 4, 0])
如何获取?
我尝试过:
d = np.vstack([a, b, c])
print (d)
res = lookup[d]
res = np.max(res, axis = 0)
print (d[enumerate(lookup)])
我出错了
IndexError:只有整数,切片(:),省略号(...),numpy.newaxis(无)以及整数或布尔数组是有效索引
IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices
推荐答案
您要这样做吗?
d = np.vstack([a,b,c])
# option 1
rows = lookup[d].argmax(0)
d[rows, np.arange(d.shape[1])]
# option 2
(lookup[:,None] == lookup[d].max(0)).argmax(0)
输出:
array([4, 2, 0, 4, 4, 4, 4, 4, 0])
这篇关于numpy中的一些数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文