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]
PS: 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.
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
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.
numpy supports the numpy.transpose
function:
numpy.transpose(a, axes=None)
Permute the dimensions of an array.
Parameters :
a : array_like
: Input array.axes
: list of ints, optional By default, reverse the dimensions, otherwise permute the axes according to the values given.Returns :
p : ndarray
: a with its axes permuted. A view is returned whenever possible.
That will be 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]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.