Questions tagged [advanced-indexing]

This tag should be used for problems dealing with retrieving elements or updating or modifying an array using either integer or boolean array in NumPy.

In NumPy, when we use array or "array-like" for indexing into an array either for accessing or modifying the elements, then it is termed as Advanced Indexing. This indexing will always return a copy of the array. More details can be found in NumPy-arrays.indexing#advanced-indexing

11 questions
3
votes
4 answers

How this numpy advance indexing code works?

I am learning numpy framework.This piece of code I don't understand. import numpy as np a =np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]]) print(a) row = np.array([[0,0],[3,3]]) col = np.array([[0,2],[0,2]]) b = a[row,col] print("This is b…
2
votes
0 answers

How to do advanced indexing in lat-lon-time in xarray

I have atmospheric data on a lat-lon-time grid, in an xarray Dataset. I want to select from that data along a 'path', so on (lat, lon, time) coordinates. I have tried to use xarray advanced indexing. If I use only (lat, lon), I get the expected…
Aleid
  • 21
  • 1
2
votes
2 answers

Best way to convert a tensor from a condensed representation

I have a Tensor that is in a condensed format representing a sparse 3-D matrix. I need to convert it to a normal matrix (the one that it is actually representing). So, in my case, each row of any 2-D slice of my matrix can only contain one non-zero…
cauthon14
  • 269
  • 1
  • 3
  • 14
1
vote
1 answer

In Pytorch how to slice tensor across multiple dims with BoolTensor masks?

I want to use BoolTensor indices to slice a multidimensional tensor in Pytorch. I expect for the indexed tensor, the parts where the indices are true are kept, while the parts where the indices are false are sliced out. My code is like import…
nanimonai
  • 73
  • 6
1
vote
1 answer

How to interpret numpy advanced indexing solution

I have a piece of numpy code that I know works. I know this because I have tested it in my generic case successfully. However, I arrived at the solution after two hours of back and forth referencing the docs and trial and error. I can't grasp how I…
chr0nikler
  • 468
  • 4
  • 13
0
votes
1 answer

NumPy, advanced indexing and slicilng

I am trying to figure out how to index both "standalone" elements and ranges simultaneously in NumPy. Suppose we have an array arr = np.arange(0, 20) and we want to take, say, 2nd, 4th, 6th elements and also all elements from 10th to the end of the…
I. Dakhtin
  • 13
  • 2
0
votes
0 answers

Advanced interpolation with xarray along one line

I would like to interpolate and extrapolate the air values along the red line with xarray. I was following the example in the documentation, but I'm missing something, I think my code is interpolating for the entire image? I only want for the red…
0
votes
1 answer

Problem applying an operation by NumPy advanced indexing

I have two arrays by shape c = (2, 10, 2, 3) and p = (2, 5, 3). I want to pick the first vectors in the 3d dimension of c and minus it from each vectors in the corresponding row of p. It will be better understandable by this example, we can do this…
Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
0
votes
1 answer

Apply a function to only array slices which satisfy a condition (NumPy)

I have an array A[i,j]. The last index contains various input values for a function myfunc that will be applied for each i and produce an output B[i]. However, many values indexed by j will not contribute to B, so I would like to avoid unnecessary…
BGreen
  • 370
  • 3
  • 17
0
votes
2 answers

Python: Numpy slices not comparing / broadcasting together. [x:y] vs [0: y-x] Claiming different dimensions

I am comparing two slices of the same array. The two slices are the same, except they are offset in the [1] axis. I compare these two slices and return their minimum repeatedly in a while loop. My expectation: The size of the arrays stay the same…
0
votes
1 answer

Easy way to do nd-array contraction using advanced indexing in Python

I know there must be an elegant way to do this using advanced indexing, I just can't figure it out. Suppose I have the (2,3,4) array x = array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], …
Zen
  • 137
  • 5