7

enter image description here

Error message: AttributeError: module 'numpy' has no attribute 'flip'

I can't understand why it's giving me this error, I've googled and made sure I'm up to the latest version of numpy. I definitely don't have another file called numpy in my working directory. Any help would be greatly appreciated!

1
  • 1
    docs Quote: New in version 1.12.0. Your version: 1.11.3 - and please post code and error messages as text, not as images. Commented Aug 16, 2017 at 6:56

3 Answers 3

16

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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])

Comments

0

One can reshape 1-D array apply fliplr and then get 1-D array back. That is possible to go from 1-D x to 2-D by using, e.g. x.reshape(1,x.size) or [x].

x = np.arange(5)
print(x)
x = np.fliplr([x])[0];  # fliplr works with at least 2-D arrays
print(x)

[0 1 2 3 4]
[4 3 2 1 0]

1 Comment

Although this code might (or might not) solve the problem, a good answer should always contain an explanation about what the problem is and how this code helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.