numpy:找到给定索引数组的行的值 [英] Numpy: Find the values of rows given an array of indexes
问题描述
我有一个2D值数组和一个1D索引数组。我想使用索引数组从每一行的索引中提取值。以下代码将成功完成此操作:
I have a 2D array of values and a 1D array of indexes. I want to pull the values from the index of each row using an array of indexes. The following code would do this successfully:
from pprint import pprint
import numpy as np
_2Darray = np.arange(100, dtype = np.float16)
_2Darray = _2Darray.reshape((10, 10))
array_indexes = [5,5,5,4,4,4,6,6,6,8]
index_values = []
for row, index in enumerate(array_indexes):
index_values.append(_2Darray[row, index])
pprint(_2Darray)
print index_values
返回
array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.],
[ 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.],
[ 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.],
[ 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.],
[ 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.],
[ 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.],
[ 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.],
[ 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.],
[ 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.]], dtype=float16)
[5.0, 15.0, 25.0, 34.0, 44.0, 54.0, 66.0, 76.0, 86.0, 98.0]
但是我只想使用numpy函数。我已经尝试了很多numpy函数,但是似乎没有一个函数可以简单地完成这项任务。
But I want to do it using only numpy functions. I have tried a whole bunch of numpy functions, but none of them seem to do this fairly simply task.
预先感谢!
编辑
我设法弄清楚我的实现是:
V_high = np.fromiter( (
Edit I managed to figure out what my implementation would be: V_high = np.fromiter((
index_values = _2Darray[ind[0], ind[1]] for ind in
enumerate(array_indexes)),
dtype = _2Darray.dtype,
count = len(_2Darray))
感谢扎根,我已经完成了两个实现。现在进行一些分析:
我的实现通过cProfiler
Thanks to root I've got both implementations worked out. Now for some profiling: My implementation run through cProfiler
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.274 0.137 0.622 0.311 {numpy.core.multiarray.fromiter}
20274 0.259 0.000 0.259 0.000 lazer_np.py:86(<genexpr>)
和root:
4 0.000 0.000 0.000 0.000 {numpy.core.multiarray.array}
1 0.000 0.000 0.000 0.000 {numpy.core.multiarray.arange}
我不敢相信,但是cProfiler根本没有检测到root需要花费任何时间的方法。我认为这一定是某种错误,但是肯定明显更快。在较早的测试中,我得到根的速度大约快3倍
注意:这些测试是在np的shape =(20273,200)数组上完成的.float16值。另外,每个索引必须为每个测试运行两次。
Note: these tests were done on a shape = (20273, 200) array of np.float16 values. Additionally, each indexing had to be run twice for each test.
推荐答案
这应该做到:
row = numpy.arange(_2Darray.shape[0])
index_values = _2Darray[row, array_indexes]
Numpy允许您使用两个数组对2d数组(或nd数组)进行索引,例如:
Numpy allows you to index 2d arrays (or nd arrays really) with two arrays such that:
for i in range(len(row)):
result1[i] = array[row[i], col[i]]
result2 = array[row, col]
numpy.all(result1 == result2)
这篇关于numpy:找到给定索引数组的行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!