Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a numpy array view with integer or boolean indexing

Tags:

python

numpy

numpy arrays can be partially assigned using integer or boolean indices like this:

import numpy as np
x = np.arange(5)
x[[2,4]]=0
x
## array([0., 0., 1., 0., 1.])
x[[True]*2+[False]*3]=2
x
## array([2., 2., 1., 0., 1.])

However, even though x[[2,4]] is in lvalue in this context, the lvalue cannot be assigned to another variable, because in this case the assignment is done by __setitem__, whereas __getitem__, when passed an integer or boolean list, creates a copy:

x = np.arange(5)
y = x[[2,4]]
y[:] = 1
x
array([0., 0., 0., 0., 0.])

Question: is there any simple/clean way to get a writeable array view based on an integer-indexed or boolean-indexed index subset? By "simple/clean" I mean that I want to avoid writing a new class, or keeping track of the sub-indices myself. Basically I'm looking for some numpy function or trick that I haven't been able to google.

The point of this question is to be able to do this recursively, to be able to create functions that assign to pieces of an array by just passing views of the array around, rather than passing indices as well as the base array.

like image 884
mrip Avatar asked Oct 18 '19 12:10

mrip


People also ask

Does NumPy arrays support boolean indexing?

We can also index NumPy arrays using a NumPy array of boolean values on one axis to specify the indices that we want to access. This will create a NumPy array of size 3x4 (3 rows and 4 columns) with values from 0 to 11 (value 12 not included).

How do you use boolean indexing in NumPy?

You can index specific values from a NumPy array using another NumPy array of Boolean values on one axis to specify the indices you want to access. For example, to access the second and third values of array a = np. array([4, 6, 8]) , you can use the expression a[np.

How do you make a boolean NumPy array?

Declaring a Numpy Boolean Array A boolean array can be made using dtype=bool, manually. All values other than '0', 'False', 'None', or empty strings are considered True in a boolean array.

How do you find the index of an element in an array in NumPy?

Using ndenumerate() function to find the Index of value It is usually used to find the first occurrence of the element in the given numpy array.


1 Answers

A nice explanation to your question here:

You can create views by selecting a slice of the original array, or also by changing the dtype (or a combination of both). The rule of thumb for creating a slice view is that the viewed elements can be addressed with offsets, strides, and counts in the original array. (...)

The reason why a fancy indexing is not returning a view is that, in general, it cannot be expressed as a slice (in the sense stated above of being able to be addressed with offsets, strides, and counts).

For example, fancy indexing for could have been expressed by , but it is not possible to do the same for by means of a slice. So, this is why an object with a copy of the original data is returned instead.

So as a general rule, no, you can't.

In my opinion the most "numpy" way of working with views is by working with masks, and keeping track of these instead of assigning the views to a new variable. I'd simply do:

m = [2, 4]
x[m] = some_function(x[m]) # whatever you need to do
like image 89
Tarifazo Avatar answered Oct 01 '22 05:10

Tarifazo