AttributeError: module 'numpy' has no attribute 'flip'
np.flip
has been introduced for versions v.1.12.0 and beyond. For older versions, you can consider using np.fliplr
and np.flipud
.
Alternatively, upgrade your numpy version using pip install --user --upgrade numpy
.
Yes,flip
is new, but there isn't anything magical about it. Here's the code:
def flip(m, axis):
if not hasattr(m, 'ndim'):
m = asarray(m)
indexer = [slice(None)] * m.ndim
try:
indexer[axis] = slice(None, None, -1)
except IndexError:
raise ValueError("axis=%i is invalid for the %i-dimensional input array"
% (axis, m.ndim))
return m[tuple(indexer)]
The essence of the action is that it indexes your array with one or more instances of ::-1
(the slice(None,None,-1)
). flipud/lr
do the same thing.
With this x
, flip
does:
In [826]: np.array([1,2,3])[::-1]
Out[826]: array([3, 2, 1])