numpy 重塑实现

问题描述 投票:0回答:1

我的主要关注点是此页面

首先,我看不懂该页面。对于第一个参数

shape

ndarray.reshape(shape, order='C')

为什么

this method on ndarray allows the elements of the shape parameter to be passed in as separate arguments

也就是说,为什么这个实现允许

a.reshape(10, 11)

第二,我想知道GitHub上的实现在哪里?搜索整个句子“返回包含具有新形状的相同数据的数组”并没有给我在 GitHub 上的正确链接。 我的尝试

谢谢!

python numpy reshape
1个回答
0
投票

冒着迂腐的风险,这里有一些使用函数和方法版本之间的比较。函数版本采用一个初始对象,将其解释为数组(或放入数组中)。该方法已经有了数组对象 (

self
)。

该函数具有 python 签名,该方法是从

c
编译的,因此可以在其签名中采取一些自由(特别是因为它是在 py3 和当前标准之前几年前编写的)。

In [66]: x=np.arange(12)

In [67]: np.reshape(x,(3,4))
Out[67]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

该方法可以采用元组或数字:

In [68]: x.reshape((3,4))
Out[68]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [69]: x.reshape(3,4)
Out[69]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

该函数尝试将第三个参数解释为

order
:

In [70]: np.reshape(x,3,4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:57, in _wrapfunc(obj, method, *args, **kwds)
     56 try:
---> 57     return bound(*args, **kwds)
     58 except TypeError:
     59     # A TypeError occurs if the object does have such a method in its
     60     # class, but its signature is not identical to that of NumPy's. This
   (...)
     64     # Call _wrapit from within the except clause to ensure a potential
     65     # exception has a traceback chain.

TypeError: order must be str, not int

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[70], line 1
----> 1 np.reshape(x,3,4)

File <__array_function__ internals>:200, in reshape(*args, **kwargs)

File ~\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:298, in reshape(a, newshape, order)
    198 @array_function_dispatch(_reshape_dispatcher)
    199 def reshape(a, newshape, order='C'):
    200     """
    201     Gives a new shape to an array without changing its data.
    202 
   (...)
    296            [5, 6]])
    297     """
--> 298     return _wrapfunc(a, 'reshape', newshape, order=order)

File ~\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:66, in _wrapfunc(obj, method, *args, **kwds)
     57     return bound(*args, **kwds)
     58 except TypeError:
     59     # A TypeError occurs if the object does have such a method in its
     60     # class, but its signature is not identical to that of NumPy's. This
   (...)
     64     # Call _wrapit from within the except clause to ensure a potential
     65     # exception has a traceback chain.
---> 66     return _wrapit(obj, method, *args, **kwds)

File ~\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:43, in _wrapit(obj, method, *args, **kwds)
     41 except AttributeError:
     42     wrap = None
---> 43 result = getattr(asarray(obj), method)(*args, **kwds)
     44 if wrap:
     45     if not isinstance(result, mu.ndarray):

TypeError: order must be str, not int

In [71]: np.reshape(x,(3,4),'F')
Out[71]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

该方法要求 order 是关键字:

In [72]: x.reshape(3,4,'F')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[72], line 1
----> 1 x.reshape(3,4,'F')

TypeError: 'str' object cannot be interpreted as an integer

In [73]: x.reshape(3,4,order='F')
Out[73]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

In [74]: x.reshape(3,4),'F')
  Cell In[74], line 1
    x.reshape(3,4),'F')
                      ^
SyntaxError: unmatched ')'


In [75]: x.reshape((3,4),'F')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[75], line 1
----> 1 x.reshape((3,4),'F')

TypeError: 'tuple' object cannot be interpreted as an integer

In [76]: x.reshape((3,4),order='F')
Out[76]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

比较newshape错误时的错误。同样的错误,但该函数具有额外的

wrapit
分层。

In [77]: x.reshape((3,5))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[77], line 1
----> 1 x.reshape((3,5))

ValueError: cannot reshape array of size 12 into shape (3,5)

In [78]: np.reshape(x,(3,5))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[78], line 1
----> 1 np.reshape(x,(3,5))

File <__array_function__ internals>:200, in reshape(*args, **kwargs)

File ~\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:298, in reshape(a, newshape, order)
    198 @array_function_dispatch(_reshape_dispatcher)
    199 def reshape(a, newshape, order='C'):
    200     """
    201     Gives a new shape to an array without changing its data.
    202 
   (...)
    296            [5, 6]])
    297     """
--> 298     return _wrapfunc(a, 'reshape', newshape, order=order)

File ~\miniconda3\lib\site-packages\numpy\core\fromnumeric.py:57, in _wrapfunc(obj, method, *args, **kwds)
     54     return _wrapit(obj, method, *args, **kwds)
     56 try:
---> 57     return bound(*args, **kwds)
     58 except TypeError:
     59     # A TypeError occurs if the object does have such a method in its
     60     # class, but its signature is not identical to that of NumPy's. This
   (...)
     64     # Call _wrapit from within the except clause to ensure a potential
     65     # exception has a traceback chain.
     66     return _wrapit(obj, method, *args, **kwds)

ValueError: cannot reshape array of size 12 into shape (3,5)
© www.soinside.com 2019 - 2024. All rights reserved.