Numpy reshape on view

前端 未结 2 820
甜味超标
甜味超标 2020-12-10 18:50

I\'m confused about the results of numpy reshape operated on a view. In the following q.flags shows that it does not own the data, but q.base is neither x nor y, so what is

2条回答
  •  我在风中等你
    2020-12-10 19:25

    I like to use .__array_interface__.

    In [811]: x.__array_interface__
    Out[811]: 
    {'data': (149194496, False),
     'descr': [('', '

    Transpose was performed by reversing the strides. The base data pointer is the same.

    In [817]: q.__array_interface__
    Out[817]: 
    {'data': (165219304, False),
     'descr': [('', '

    So the q data is a copy (different pointer). Strides (8,) means its elements are accessed by stepping from one f8 to the next. But a x.reshape(16) is a view of x - because its data can be accessed with a simple 8 step.

    To access the original data in the q order, it would have to step 32 bytes 3 times (down x rows), then go back to the start and step 8 to the 2nd x column, followed by 3 row steps, etc. Since striding doesn't work this way, it has to work from a copy.

    Note also that y[0,0] changes x[0,0], but q[0] is independent of both.

    While OWNDATA for q is false, it is True for y.ravel() and y.flatten(). I suspect reshape() in this case is making a copy, and then reshaping, and it's the intermediate copy that 'owns' the data, q.base.

提交回复
热议问题