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
1
vote
2 answers

Indexing numpy array with half-values efficiently

I would like to index a numpy array with integer and half values. This is roughly what I have in mind: >>> a = HalfIndexedArray([[0,1,2,3],[10,11,12,13],[20,21,22,23]]) >>> print(a[0,0]) 0 >>> print(a[0.5,0.5]) 11 >>> print(a[0,1+0.5]) 3 Only…
D__
  • 239
  • 3
  • 13
1
vote
1 answer

Combine counts from multiple numpy.uniques

I have multiple returns of numpy.unique(a, return_counts=True) and unfortunately do not have access to the original arrays. I want to combine these results to one array with the unique values and to one storing the respective counts. I do not want…
Helmut
  • 311
  • 1
  • 9
1
vote
0 answers

Is there a general way to determine the number of indices used in a numpy indexing expression?

Many different things can be used as indexers in numpy, in my case most commonly lists of integers and Boolean expressions, but slices etc are also on the table. Now I have a function that takes as an argument an indexing expression and uses…
JPhibs
  • 56
  • 5
1
vote
1 answer

extracting index from a 2D array Python

I have an image (named gray_image) of shape (1830, 1830). After some image processing (I have created superpixels) I got a 2D array named segments (of shape (1830, 1830)), which contains values from 0 to 72. I need to take the index from where I…
1
vote
1 answer

How can I get the number of indexed elements of array of known shape without actually indexing the array?

I have an index IDX (which may be either list of indices, boolean mask, tuple of slices etc.) indexing some abstract numpy array of known shape shape (possibly big). I know I can create a dummy array, index it and count the elements: A =…
abukaj
  • 2,582
  • 1
  • 22
  • 45
1
vote
1 answer

How to index multidimensional array multiple times in a vectorized way numpy?

I am trying to index a multidimensional array (4-dimensions) in numpy. The shape of the array is of (125,125,125,3). I have 3 separate 2D lists of index arrays. The lists are of size (N,4), (M,4), and (1,4) respectively. The 3 separate lists…
rhingo3
  • 13
  • 2
1
vote
1 answer

manipulating a numpy array with another array

I am doing my head in with brackets and ':' whilst trying to do 2 dimensional indexing with an other index So I would be really pleased if somebody could straighten me out I have an greyscale image BlurredFlip shape is : (480, 640) then I have…
jc508
  • 91
  • 5
1
vote
1 answer

How to indexing multi-dimensional arrays given by indices in a certain axis?

Let's say I have a 4d array A with shape (D0, D1, D2, D3). I have a 1d array B with shape (D0,), which includes the indices I need at axis 2. The trivial way to implement what I need: output_lis = [] for i in range(D0): output_lis.append(A[i, :,…
1
vote
1 answer

Numpy Indexing problem..... Advance indexing what is X[0] doing here?

import numpy as np X = np.array([[0, 1, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1], [1, 0, 1, 0]]) y = np.array([0, 1, 0, 1]) counts = {} print(X[y == 0]) # prints = [[0 1 0 1] # [0 0 0 1]] I want to know why X[y==0] printing two data point. Shouldn't…
1
vote
0 answers

Is there a way to slice out multiple 2D numpy arrays from one 2D numpy array in one batch operation?

I have a numpy array heatmap of shape (img_height, img_width) and another array bboxes of shape (K, 4), where K is a number of bounding boxes. Each bounding box is defined like so: [x_top_left, y_top_left, width, height]. Here's an example of such…
1
vote
1 answer

Advanced Indexing in 3 Dimensional Numpy ndarray In Python

I have a ndarray of shape (68, 64, 64) called 'prediction'. These dimensions correspond to image_number, height, width. For each image, I have a tuple of length two that contains coordinates that corresponds to a particular location in each 64x64…
Kyle Vrooman
  • 33
  • 1
  • 3
1
vote
1 answer

How to remove nested for loops and use numpy arrays insead

I have a video comprising of 580 frames. I need to be able to detect the green color from the video and create a mask so as to put zero values where green is found and the rest should be 255. I have converted the video in HSV format and am using…
1
vote
2 answers

Numpy advanced indexing fails

I have a numpy array looking like this: a = np.array([[0.87, 1.10, 2.01, 0.81 , 0.64, 0. ], [0.87, 1.10, 2.01, 0.81 , 0.64, 0. ], [0.87, 1.10, 2.01, 0.81 , 0.64, 0. ], [0.87, 1.10, 2.01, 0.81 ,…
E. Sommer
  • 710
  • 1
  • 7
  • 28
1
vote
3 answers

python index in 2d matrix

Given the following code: import numpy as np mat = np.arange(1,26).reshape(5,5) My understanding was the following lines are identical: mat[:3][1:2] mat[:3,1:2] But they are not. Why?
R71
  • 4,283
  • 7
  • 32
  • 60
1
vote
0 answers

numpy: arr[True] creates a new axis

I don't understand why True does not behave like a boolean mask: >>> x = np.arange(5) >>> x[(x<3) & True].shape (3,) >>> x[np.repeat(True, 5)].shape (5,) >>> x[True].shape (1, 5)
Labo
  • 2,482
  • 2
  • 18
  • 38