Questions tagged [numpy-indexing]

questions related to indexing on numpy ndarray objects

ndarrays can be indexed using the standard Python x[obj] syntax, where x is the array and obj the selection. There are different kinds of indexing available depending on obj: basic indexing, advanced indexing and field access.

Read more:

62 questions
4
votes
2 answers

Vectorized way to contract Numpy array using advanced indexing

I have a Numpy array of dimensions (d1,d2,d3,d4), for instance A = np.arange(120).reshape((2,3,4,5)). I would like to contract it so as to obtain B of dimensions (d1,d2,d4). The d3-indices of parts to pick are collected in an indexing array Idx of…
4
votes
2 answers

More efficient way to access rows based on a list of indices in 2d numpy array?

So I have 2d numpay array arr. It's a relatively big one: arr.shape = (2400, 60000) What I'm currently doing is the following: randomly (with replacement) select arr.shape[0] indices access (row-wise) chosen indices of arr calculating column-wise…
Slaw
  • 43
  • 4
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…
3
votes
1 answer

How can I provide syntactic sugar for slicing a numpy array?

I would like to write more readable code by doing something like: import numpy as np SLICE_XY = slice(0, 2) SLICE_Z = slice(2, 3) data = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10,…
McManip
  • 155
  • 1
  • 9
3
votes
1 answer

Indexing a 1D Numpy array using 2D array

In Numpy, can we use a 2D array as index into a 1D array? What array array indexing rule applies to following code? # 1D array arr = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 2D index indx = np.array([[2, 4], [6, 8]]) # 2D index in…
Saurabhp75
  • 31
  • 5
2
votes
1 answer

Get 2D elements from 3D numpy array, corresponding to 2D indices

I have a 3D array that could be interpreted as a 2D matrix of positions, where each position is a 2D array of coordinates [x,y]. I then have a list of 2D indices, each one indicating a position in the matrix in terms of [row, column]. I would like…
etien
  • 301
  • 1
  • 9
2
votes
1 answer

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)

I have an np.ndarray of shape (5, 5, 2, 2, 2, 10, 8) named table. I can succesfully slice it like this: table[4, [0, 1], 1, 1, 1, slice(0, 10, None), slice(0, 8, None)] table[4, [0, 1], 1, 1, 1, [0, 2], slice(0, 8, None)] But for some reason when…
2
votes
1 answer

A faster way than "for loop" - Indexing 3d numpy array by 2d array

I have a 3d (3, 2, 3) array and first dimension(3) is flexible it can be any size. arr = np.array( [[[56, 24, 32], [56, 24, 32]], [[51, 27, 72], [51, 27, 72]], [[36, 14, 49], [36, 14, 49]]]) Indexing array is (2,3): idxs = np.array( [[1, 0,…
2
votes
1 answer

Add repeated elements of array indexed by another array

I have a relatively simple problem that I cannot solve without using loops. It is difficult for me to figure out the correct title for this problem. Lets say we have two numpy arrays: array_1 = np.array([[0, 1, 2], [3, 3, 3], …
2
votes
2 answers

Extract array from array without loop in python

I am trying to extract part of an array from an array. Let's assume I have an array array1 with shape (M, N, P). For my specific case, M = 10, N = 5, P = 2000. I have another array, array2 of shape (M, N, 1), which contains the starting points of…
Szinti
  • 23
  • 3
2
votes
1 answer

Showing wrong axis in 2D array when value is out of range

I have a 2D array (row*column)row means axis0 and column mean aixs1. example: a=np.array([[10,20,30],[40,50,60]]) now when I am trying to access the value(out of range along axis 0)I am getting below Exception which is correct. print(a[-3][-1]) …
2
votes
1 answer

Batched index_fill in PyTorch

I have an index tensor of size (2, 3): >>> index = torch.empty(6).random_(0,8).view(2,3) tensor([[6., 3., 2.], [3., 4., 7.]]) And a value tensor of size (2, 8): >>> value = torch.zeros(2,8) tensor([[0., 0., 0., 0., 0., 0., 0., 0.], …
namespace-Pt
  • 1,604
  • 1
  • 14
  • 25
2
votes
3 answers

why there is a difference in the output of the same indexing selection inside a numpy array

let's assume that I have a 2-dimensional NumPy array that looks like that and i want to extract the left bottom square (4x4): arr_2d = [[ 5,10,15], [20,25,30], [35,40,45]] why there is difference between this…
HOWL_CODER
  • 35
  • 8
2
votes
0 answers

multi-dimensional indexing with numpy

Say I have the following numpy arrays: import numpy as np np.random.seed(100) fp = np.random.rand(4000, 5) ix = np.random.randint(0, 5, (3, 3)) fp array([[ 0.54340494, 0.27836939, 0.42451759, 0.84477613, 0.00471886], [ 0.12156912, …
Eric B
  • 1,635
  • 4
  • 13
  • 27
1
vote
1 answer

Inverse array indexing in numpy

I have an array and index list in numpy: ar = np.array([4, 5, 3, -1, -1, 0, 1, 2]) indices = np.array([5, 6, 1, 2, 0, 7, 3, 4]) ar_permuted = ar[indices] #ar_permuted = array([0, 1, 5, 3, 4, 2, -1, -1]) Now, given ar_permuted and indices, what is…
Ars ML
  • 49
  • 4
1
2 3 4 5