美文网首页
numpy 数组复制问题 数组的几种复制

numpy 数组复制问题 数组的几种复制

作者: 你好_兔先生 | 来源:发表于2018-06-28 10:19 被阅读0次

    引自https://docs.scipy.org/doc/numpy/user/quickstart.html#copies-and-views

    Copies and Views

    When operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases:

    No Copy at All

    Simple assignments make no copy of array objects or of their data.

    a = np.arange(12)

    b = a # no new object is created
    b is a # a and b are two names for the same ndarray object
    True
    b.shape = 3,4 # changes the shape of a
    a.shape
    (3, 4)

    Python passes mutable objects as references, so function calls make no copy.

    def f(x):
    ... print(id(x))
    ...
    id(a) # id is a unique identifier of an object
    148293216
    f(a)
    148293216

    View or Shallow Copy

    Different array objects can share the same data. The view method creates a new array object that looks at the same data.

    c = a.view()
    c is a
    False
    c.base is a # c is a view of the data owned by a
    True
    c.flags.owndata
    False

    c.shape = 2,6 # a's shape doesn't change
    a.shape
    (3, 4)
    c[0,4] = 1234 # a's data changes
    a
    array([[ 0, 1, 2, 3],
    [1234, 5, 6, 7],
    [ 8, 9, 10, 11]])

    Slicing an array returns a view of it:

    s = a[ : , 1:3] # spaces added for clarity; could also be written "s = a[:,1:3]"
    s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10
    a
    array([[ 0, 10, 10, 3],
    [1234, 10, 10, 7],
    [ 8, 10, 10, 11]])

    Deep Copy

    The copy method makes a complete copy of the array and its data.

    d = a.copy() # a new array object with new data is created
    d is a
    False
    d.base is a # d doesn't share anything with a
    f(a)
    148293216


    ### View or Shallow Copy

    Different array objects can share the same data. The view method creates a new array object that looks at the same data.

    >>> c = a.view()
    >>> c is a
    False
    >>> c.base is a # c is a view of the data owned by a
    True
    >>> c.flags.owndata
    False
    >>>
    >>> c.shape = 2,6 # a's shape doesn't change
    >>> a.shape
    (3, 4)
    >>> c[0,4] = 1234 # a's data changes
    >>> a
    array([[ 0, 1, 2, 3],
    [1234, 5, 6, 7],
    [ 8, 9, 10, 11]])


    Slicing an array returns a view of it:

    >>> s = a[ : , 1:3] # spaces added for clarity; could also be written "s = a[:,1:3]"
    >>> s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10
    >>> a
    array([[ 0, 10, 10, 3],
    [1234, 10, 10, 7],
    [ 8, 10, 10, 11]])


    ### Deep Copy

    The copy method makes a complete copy of the array and its data.

    >>> d = a.copy() # a new array object with new data is created
    >>> d is a
    False
    >>> d.base is a # d doesn't share anything with a
    False
    d[0,0] = 9999
    a
    array([[ 0, 10, 10, 3],
    [1234, 10, 10, 7],
    [ 8, 10, 10, 11]])

    相关文章

      网友评论

          本文标题:numpy 数组复制问题 数组的几种复制

          本文链接:https://www.haomeiwen.com/subject/ghtgyftx.html