Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

python - Flip and rotate numpy array

Is there a faster way of flipping and rotating an array in 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]

P.S.: This question is not a duplicate of: Transposing a NumPy array. The present question does not contest the stability of the "transpose" function; it is asking for the function itself.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The code for np.rot90 does, in your case of k=3:

    # 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; it is a view, not a copy.

np.fliplr also creates a view, changing strides with the [:,::-1].

The original 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. Despite the use of 2 functions, x is still a view of ar.

The 2 functions aren't needed, but they aren't that expensive either. We are talking microseconds v nanoseconds of time. (my timeit times in Ipython are much smaller yours)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...