最有效的方法来反转一个numpy数组。

[英]Most efficient way to reverse a numpy array


Believe it or not, after profiling my current code, the repetitive operation of numpy array reversion ate a giant chunk of the running time. What I have right now is the common view-based method:

信不信由你,在剖析了我当前的代码之后,numpy阵列的重复操作将消耗大量的运行时间。我现在所拥有的是常见的基于视图的方法:

reversed_arr = arr[::-1]

Is there any other way to do it more efficiently, or is it just an illusion from my obsession with unrealistic numpy performance?

还有其他的方法可以更有效地做到这一点吗?或者这只是我对不现实的麻木表现的一种错觉?

6 个解决方案

#1


157  

When you create reversed_arr you are creating a view into the original array. You can then change the original array, and the view will update to reflect the changes.

当您创建reversed_arr时,您将创建一个视图到原始数组中。然后可以更改原始数组,视图将更新以反映更改。

Are you re-creating the view more often than you need to? You should be able to do something like this:

你是否比你需要的更频繁地重新创建视图?你应该可以这样做:

arr = np.array(some_sequence)
reversed_arr = arr[::-1]

do_something(arr)
look_at(reversed_arr)
do_something_else(arr)
look_at(reversed_arr)

I'm not a numpy expert, but this seems like it would be the fastest way to do things in numpy. If this is what you are already doing, I don't think you can improve on it.

我不是一个麻木的专家,但这似乎是在numpy中做事情的最快方式。如果这是你已经在做的事情,我认为你不能改进它。

P.S. Great discussion of numpy views here:

p .关于numpy视图的大讨论:

View onto a numpy array?

查看一个numpy数组?

#2


31  

np.fliplr() flips the array left to right.

fliplr()将数组从左到右翻转。

Note that for 1d arrays, you need to trick it a bit:

注意,对于一维数组,您需要稍微欺骗一下:

arr1d = np.array(some_sequence)
reversed_arr = np.fliplr([arr1d])[0]

#3


27  

Because this seems to not be marked as answered yet... The Answer of Thomas Arildsen should be the proper one: just use

因为这似乎没有被标记为答案……Thomas Arildsen的答案应该是正确的:使用。

np.flipud(your_array) 

if it is a 1d array (column array).

如果它是一个一维数组(列数组)。

With matrizes do

matrizes做

fliplr(matrix)

if you want to reverse rows and flipud(matrix) if you want to flip columns. No need for making your 1d column array a 2dimensional row array (matrix with one None layer) and then flipping it.

如果想要翻转列,则需要反向行和flipud(矩阵)。不需要让你的1d列阵列一个二维的行阵列(一个没有层的矩阵)然后翻转它。

#4


15  

As mentioned above, a[::-1] really only creates a view, so it's a constant-time operation (and as such doesn't take longer as the array grows). If you need the array to be contiguous (for example because you're performing many vector operations with it), ascontiguousarray is about as fast as flipup/fliplr:

如上所述,a[::-1]实际上只创建一个视图,因此它是一个常量时间操作(而且随着数组的增长,这也不需要花更长的时间)。如果您需要该数组是连续的(例如,因为您正在使用它执行许多向量操作),那么,与flipup/fliplr类似的数组就有了:

enter image description here


Code to generate the plot:

产生情节的代码:

import numpy
import perfplot


perfplot.show(
    setup=lambda n: numpy.random.randint(0, 1000, n),
    kernels=[
        lambda a: a[::-1],
        lambda a: numpy.ascontiguousarray(a[::-1]),
        lambda a: numpy.fliplr([a])[0]
        ],
    labels=['a[::-1]', 'ascontiguousarray(a[::-1])', 'fliplr'],
    n_range=[2**k for k in range(20)],
    xlabel='len(a)',
    logx=True,
    logy=True,
    )

#5


3  

I will expand on the earlier answer about np.fliplr(). Here is some code that demonstrates constructing a 1d array, transforming it into a 2d array, flipping it, then converting back into a 1d array. time.clock() will be used to keep time, which is presented in terms of seconds.

我将详述关于np.fliplr()的早期答案。这里有一些代码演示了构建一个一维数组,将其转换为一个2d数组,将其翻转,然后转换回一维数组。time.clock()将用于保存时间,它以秒为单位表示。

import time
import numpy as np

start = time.clock()
x = np.array(range(3))
#transform to 2d
x = np.atleast_2d(x)
#flip array
x = np.fliplr(x)
#take first (and only) element
x = x[0]
#print x
end = time.clock()
print end-start

With print statement uncommented:

用print语句的注释:

[2 1 0]
0.00203907123594

With print statement commented out:

打印声明注释:

5.59799927506e-05

So, in terms of efficiency, I think that's decent. For those of you that love to do it in one line, here is that form.

所以,在效率方面,我认为这是不错的。对于那些喜欢在一行里做的人来说,这就是那张表。

np.fliplr(np.atleast_2d(np.array(range(3))))[0]

#6


0  

In order to have it working with negative numbers and a long list you can do the following:

为了让它与负数和长列表一起工作,你可以做如下的事情:

b = numpy.flipud(numpy.array(a.split(),float))

Where flipud is for 1d arra

在哪里,flipud在1d arra ?

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/07/21/822a0c0eb6621f50a2696ef4257948c0.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告