简体   繁体   English

翻转和旋转numpy数组

[英]Flip and rotate numpy array

Is there a faster way of flipping and rotating an array in numpy? 有没有更快的方法来翻转和旋转numpy中的数组? For example, rotating one time clockwise and then flipping? 例如,顺时针旋转一次然后翻转?

import numpy as np
a = np.arange(0,10)
b = np.arange(-11,-1)

ar = np.array([a,b])

print ar
print ar.shape

ar = np.rot90(ar, 3)
print np.fliplr(ar)
print ar.shape

Output: 输出:

[[  0   1   2   3   4   5   6   7   8   9]
 [-11 -10  -9  -8  -7  -6  -5  -4  -3  -2]]
(2, 10)

[[  0 -11]
 [  1 -10]
 [  2  -9]
 [  3  -8]
 [  4  -7]
 [  5  -6]
 [  6  -5]
 [  7  -4]
 [  8  -3]
 [  9  -2]]
(10, 2)
[Finished in 0.1s]

PS: This question is not a duplicate of: Transposing a NumPy array . PS:此问题不是以下内容的重复: 转置NumPy数组 The present question does not contest the stability of the "transpose" function; 当前的问题并不反对“移调”功能的稳定性; it is asking for the function itself. 它要求功能本身。

The code for np.rot90 does, in your case of k=3 : k=3情况下, np.rot90的代码可以:

    # k == 3
    return fliplr(m.swapaxes(0, 1))

So 所以

In [789]: np.fliplr(ar.swapaxes(0, 1))
Out[789]: 
array([[-11,   0],
     ...
       [ -3,   8],
       [ -2,   9]])

So your 所以你

fliplr(rot90(ar, 3))

becomes 变成

 np.fliplf(np.fliplr(ar.swapaxes(0, 1)))
 # the flips cancel
 ar.swapaxes(0,1)
 # but this is just
 ar.T

So your pair of actions reduce to transpose. 因此,您的一对动作减少了转置。

transpose (and the swap ) just changes the .shape and strides attributes of the array; transpose (和swap )只是改变了.shapestrides阵列的属性; it is a view, not a copy. 它是一个视图,而不是副本。

np.fliplr also creates a view, changing strides with the [:,::-1] . np.fliplr还会创建一个视图,并通过[:,::-1]改变步幅。

The original ar : 原始ar

In [818]: ar
Out[818]: 
array([[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9],
       [-11, -10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2]])

In [819]: x=np.fliplr(np.rot90(ar,3))  # your pair of actions

In [820]: x
Out[820]: 
array([[  0, -11],
       [  1, -10],
         ...
       [  8,  -3],
       [  9,  -2]])

In [821]: x[0,1]=11

In [822]: x
Out[822]: 
array([[  0,  11],
       [  1, -10],
        ...
       [  9,  -2]])

In [823]: ar
Out[823]: 
array([[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9],
       [ 11, -10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2]])

Changing a value of x changes a value of ar . 更改x的值将更改ar的值。 Despite the use of 2 functions, x is still a view of ar . 尽管使用了2个函数,但x仍然是arview

The 2 functions aren't needed, but they aren't that expensive either. 这两个函数不是必需的,但是它们也不是那么昂贵。 We are talking microseconds v nanoseconds of time. 我们所说的是微秒v纳秒的时间。 (my timeit times in Ipython are much smaller yours) (我在Ipython中的timeit时间要小得多)

In [824]: timeit np.fliplr(np.rot90(ar,3))
100000 loops, best of 3: 8.28 µs per loop

In [825]: timeit ar.T
1000000 loops, best of 3: 455 ns per loop

A flip and rotate together (based on your example) is a matrix transpose : a matrix transpose is a permutation of the matrix's dimensions: for instance the first dimension becomes the second dimension and vice versa. 一起翻转和旋转(根据您的示例)是矩阵转置 :矩阵转置是矩阵尺寸的排列:例如,第一维变成第二维,反之亦然。

supports the numpy.transpose function: 支持numpy.transpose函数:

 numpy.transpose(a, axes=None) 

Permute the dimensions of an array. 排列数组的尺寸。

Parameters : 参数

  • a : array_like : Input array. a : array_like :输入数组。
  • axes : list of ints, optional By default, reverse the dimensions, otherwise permute the axes according to the values given. axes :整数列表,可选,默认情况下,反转尺寸,否则根据给定的值对坐标轴进行排列。

Returns : 返回值

  • p : ndarray : a with its axes permuted. p : ndarray :一个轴被置换的a。 A view is returned whenever possible. 尽可能返回一个视图。

That will be transpose : 那将被transpose

>>> import numpy as np
>>> a = np.arange(0,10)
>>> b = np.arange(-11,-1)
>>> ar = np.array([a,b])
>>> ar.T
array([[  0, -11],
       [  1, -10],
       [  2,  -9],
       [  3,  -8],
       [  4,  -7],
       [  5,  -6],
       [  6,  -5],
       [  7,  -4],
       [  8,  -3],
       [  9,  -2]])
>>> np.transpose(ar)
array([[  0, -11],
       [  1, -10],
       [  2,  -9],
       [  3,  -8],
       [  4,  -7],
       [  5,  -6],
       [  6,  -5],
       [  7,  -4],
       [  8,  -3],
       [  9,  -2]])

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

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