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

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

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:

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

sudo pip install --upgrade numpy 


回答2:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!