Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: views vs copy by slicing

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 column. It seems is a view.

>>> a = np.arange(12).reshape(3, 4)    >>> a[0:3:2, :][:, [0, 2]] = 100 >>> a array([[100,   1, 100,   3],        [  4,   5,   6,   7],        [100,   9, 100,  11]]) 

Second

But if I first slice of column, then slice of row, it seems a copy:

>>> a[:, [0, 2]][0:3:2, :] = 0 >>> a array([[100,   1, 100,   3],        [  4,   5,   6,   7],        [100,   9, 100,  11]]) 

I am confused because the two methods finally will cause seem position to change, but why the second actually doesn't change the number?

like image 581
Caesium Avatar asked Nov 08 '17 13:11

Caesium


People also ask

What is the difference between copy and view in NumPy?

The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array. The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.

What does NumPy view do?

What is a view of a NumPy array? ¶ As its name is saying, it is simply another way of viewing the data of the array. Technically, that means that the data of both objects is shared.

How can you shallow copy the data in NumPy view () in method is method or assignments?

copy() is supposed to create a shallow copy of its argument, but when applied to a NumPy array it creates a shallow copy in sense B, i.e. the new array gets its own copy of the data buffer, so changes to one array do not affect the other. x_copy = x. copy() is all you need to make a copy of x .

Is copy faster than NumPy?

As AGN pointed out, np. array is faster than np. copy because essentially the latter is a wrapper of the former. This means python "loses" some extra time searching for both functions.


2 Answers

The accepted answer by John Zwinck is actually false (I just figured this out the hard way!). The problem in the question is a combination of doing "l-value indexing" with numpy's fancy indexing. The following doc explains exactly this case

https://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.html

in the section "But fancy indexing does seem to return views sometimes, doesn't it?"

Edit:

To summarize the above link:

Whether a view or a copy is created is determined by whether the indexing can be represented as a slice.

Exception: If one does "fancy indexing" then always a copy is created. Fancy indexing is something like a[[1,2]].

Exception to the exception: If one does l-value indexing (i.e. the indexing happens left of the = sign), then the rule for when a view or a copy are created doesn't apply anymore (though see below for a further exception). The python interpreter will directly assign the values to the left hand side without creating a copy or a view.

To prove that a copy is created in both cases, you can do the operation in two steps:

>>> a = np.arange(12).reshape(3, 4) >>> b = a[0:3:2, :][:, [0, 2]] >>> b[:] = 100 >>> a array([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]]) 

and

>>> b = a[:, [0, 2]][0:3:2, :] >>> b[:] = 0 >>> a array([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]]) 

Just as an aside, the question by the original poster is the exact problem stated at the end of the scipy-cookbook link above. There is no solution given in the book. The tricky thing about the question is that there are two indexing operations done in a row.

Exception to the exception to the exception: If there are two indexing operations done in a row on the left hand side (as is the case in this question), the direct assignment in l-value indexing only works if the first indexing operation can be represented as a slice. Otherwise a copy has to be created even though it is l-value indexing.

like image 61
Maltimore Avatar answered Sep 28 '22 02:09

Maltimore


All that matters is whether you slice by rows or by columns. Slicing by rows can return a view because it is a contiguous segment of the original array. Slicing by column must return a copy because it is not a contiguous segment. For example:

A1 A2 A3 B1 B2 B3 C1 C2 C3 

By default, it is stored in memory this way:

A1 A2 A3 B1 B2 B3 C1 C2 C3 

So if you want to choose every second row, it is:

[A1 A2 A3] B1 B2 B3 [C1 C2 C3] 

That can be described as {start: 0, size: 3, stride: 6}.

But if you want to choose every second column:

[A1] A2 [A3 B1] B2 [B3 C1] C2 [C3] 

And there is no way to describe that using a single start, size, and stride. So there is no way to construct such a view.

If you want to be able to view every second column instead of every second row, you can construct your array in column-major aka Fortran order instead:

np.array(a, order='F') 

Then it will be stored as such:

A1 B1 C1 A2 B2 C2 A3 B3 C3 
like image 39
John Zwinck Avatar answered Sep 28 '22 02:09

John Zwinck