Numpy: pass by reference does not work by itself

I tried the following:

p=np.array([[1,1,1],[2,2,2],[3,3,3]])
p[0,:] = p[1,:]
y = p[1,:]
print(p)
p[1,1] = 4
print(p)
print(y)

      

As you can see, the output is:

[[2 2 2]
 [2 2 2]
 [3 3 3]]
[[2 2 2]
 [2 4 2]
 [3 3 3]]
[2 4 2]

      

So when I assigned the second line from p

to y

, it was passed by reference. When I assigned the second line to the p

first line p

, it was passed by copy. Why is this so?

Expected Result:

[[2 2 2]
 [2 2 2]
 [3 3 3]]
[[2 4 2]
 [2 4 2]
 [3 3 3]]
[2 4 2]

      

+3


source to share


1 answer


In short, the difference is:

  • accessing a slice of the array ( ... = p[]

    ) creates, if possible, a new kind in the data.
  • assignment to an array slice ( p[] = ...

    ) copies the elements into the array.

Long story:

After p = np.array([[1,1,1],[2,2,2],[3,3,3]])

p

that, the original data is stored.



y = p[1,:]

gives you an idea of ​​the data stored in p

(you can check this by looking y.base

at which is p

). This is not an assignment to an array, but it y

is a brand new object that shares data with p

. Think of it this way:

Data: [1, 1, 1, 2, 2, 2, 3, 3, 3]
       ^        ^
       p        y

      

Data is just a block of memory. p

points to the beginning of this memory and y

points somewhere in the middle. (They don't just "indicate", but they also provide additional information about the dimensions and who owns the data, but that doesn't matter here.)

It's important to recognize that an array only points to the beginning of its associated data. It then simply uses sizes and step sizes (steps) to interpret the data as vectors, matrices, etc. Remember, one array is one pointer.

p[0,:] = p[1,:]

here we assign part p to another part of the array. To get the expected behavior, different parts p

must point to the same block of data. This is not possible (but something like this can be achieved in a limited way by skillfully manipulating the steps). Instead, the data is copied.

+2


source







All Articles