简体   繁体   English

Numpy:改善数组的奇特索引

[英]Numpy: improve fancy indexing on arrays

Looking for faster fancy indexing for numpy, the code I am running slows down, at np.take() . 为numpy寻找更快的花式索引,我正在运行的代码在np.take()np.take() I tried order=F/C with np.reshape() , no improvement. 我用np.reshape()尝试了order=F/C ,没有任何改善。 Python operator works well without the double transpose , but with them is equal to np.take(). Python operator没有double transpose情况下可以很好地工作,但是它们等于np.take().

p    = np.random.randn(3500, 51)
rows = np.asarray(range(p.shape[0]))
cols = np.asarray([1,2,3,4,5,6,7,8,9,10,15,20,25,30,40,50])

%timeit p[rows][:, cols]
%timeit p.take(cols, axis = 1 )
%timeit np.asarray(operator.itemgetter(*cols)(p.T)).T

1000 loops, best of 3: 301 µs per loop
10000 loops, best of 3: 132 µs per loop
10000 loops, best of 3: 135 µs per loop

A test of several options: 测试几个选项:

In [3]: p[rows][:,cols].shape
Out[3]: (3500, 16)
In [4]: p[rows[:,None],cols].shape
Out[4]: (3500, 16)
In [5]: p[:,cols].shape
Out[5]: (3500, 16)
In [6]: p.take(cols,axis=1).shape
Out[6]: (3500, 16)

time tests - plain p[:,cols] is fastest. 时间测试-普通p[:,cols]最快。 Use a slice where possible. 尽可能使用切片。

In [7]: timeit p[rows][:,cols].shape
100 loops, best of 3: 2.78 ms per loop
In [8]: timeit p.take(cols,axis=1).shape
1000 loops, best of 3: 739 µs per loop
In [9]: timeit p[rows[:,None],cols].shape
1000 loops, best of 3: 1.43 ms per loop
In [10]: timeit p[:,cols].shape
1000 loops, best of 3: 649 µs per loop

I've seen itemgetter used for lists, but not arrays. 我已经看到itemgetter用于列表,但没有用于数组。 It's a class that iterates of a set of indexes. 这是一个迭代一组索引的类。 These 2 lines are doing the same thing: 这两条线在做同样的事情:

In [23]: timeit np.asarray(operator.itemgetter(*cols)(p.T)).T.shape
1000 loops, best of 3: 738 µs per loop
In [24]: timeit np.array([p.T[c] for c in cols]).T.shape
1000 loops, best of 3: 748 µs per loop

Notice that pT[c] is pT[c,:] or p[:,c].T . 注意, pT[c]pT[c,:]p[:,c].T With relatively few cols , and by ignoring advanced indexing with rows , it times close to p[:,cols] . 由于cols相对较少,并且忽略了rows高级索引,因此其时间接近p[:,cols]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM