Numpy: views vs copy by slicing

前端 未结 3 1125
花落未央
花落未央 2020-12-05 14:44

When I am doing the slicing, an unexpected thing happened that seems the first to be view but the second is copy.

First

First slice of row, then slice of

3条回答
  •  失恋的感觉
    2020-12-05 15:45

    This is my understanding, for your reference

    a[0:3:2, :]                     # basic indexing, a view
    ... = a[0:3:2, :][:, [0, 2]]    # getitme version, a copy,
                                    # because you use advanced
                                    # indexing [:,[0,2]]
    a[0:3:2, :][:, [0, 2]] = ...    # howver setitem version is
                                    # like a view, setitem version
                                    # is different from getitem version,
                                    # this is not c++
    a[:, [0, 2]]                    # getitem version, a copy,
                                    # because you use advanced indexing
    a[:, [0, 2]][0:3:2, :] = 0      # the copy is modied,
                                    # but a keeps unchanged.
    

    If I have any misunderstanding, please point it out.

提交回复
热议问题