Questions tagged [numpy-slicing]

questions related to numpy indexing routines, in particular, slicing, indexing and concatenation

Read more:

518 questions
144
votes
4 answers

Selecting specific rows and columns from NumPy array

I've been going crazy trying to figure out what stupid thing I'm doing wrong here. I'm using NumPy, and I have specific row indices and specific column indices that I want to select from. Here's the gist of my problem: import numpy as np a =…
Mike C
  • 1,959
  • 2
  • 17
  • 17
8
votes
1 answer

Indexing different sized ranges in a 2D numpy array using a Pythonic vectorized code

I have a numpy 2D array, and I would like to select different sized ranges of this array, depending on the column index. Here is the input array a = np.reshape(np.array(range(15)), (5, 3)) example [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] [12…
xicocaio
  • 867
  • 1
  • 10
  • 27
8
votes
2 answers

Python NumPy - angled slice of 3D array

Working in NumPy, I understand how to slice 2D arrays from a 3D array using this article. Depending on the axis I'd want to slice in: array = [[[0 1 2] [3 4 5] [6 7 8]] [[9 10 11] [12 13 14] …
8
votes
8 answers

Replace elements in numpy array avoiding loops

I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would…
Jann
  • 635
  • 1
  • 6
  • 17
7
votes
2 answers

How to truncate a numpy array?

I am trying to truncate 'data' (which is size 112943) to shape (1,15000) with the following line of code: data = np.reshape(data, (1, 15000)) However, that gives me the following error: ValueError: cannot reshape array of size 112943 into shape…
1arnav1
  • 187
  • 2
  • 3
  • 9
6
votes
2 answers

can I split numpy array with mask?

I want to split array into array with mask and index like below a = array([ 0, 1, 2, 3, 4, 5])) b = [0,2,3] into c = array([[0, 2, 3], [1, 3, 4], [2, 4, 5]]) can I do this without loop? Edit: More examples... Say, we have an array a…
JY Won
  • 78
  • 5
5
votes
2 answers

How to understand the following fancy index behaviour for multi-dimensional arrays?

We noticed that the mixed usage of fancy indexing and slicing is so confusing and undocumented for multi-dimensional arrays, for example: In [114]: x = np.arange(720).reshape((2,3,4,5,6)) In [115]: x[:,:,:,0,[0,1,2,4,5]].shape Out[115]: (2, 3, 4,…
5
votes
1 answer

Sum columns in numpy 2D array

I have a 2D NumPy array V: import numpy as np np.random.seed(10) V = np.random.randint(-10, 10, size=(6,8)) This gives V as: [[ -1 -6 5 -10 7 6 7 -2] [ -1 -10 0 -2 -6 9 6 -6] [ 5 1 1 -9 -2 -6 4 7] [ 9 3 -5 3…
EducateMe
  • 103
  • 6
5
votes
1 answer

What does a numpy array slice with complex step size mean?

I came across an example in the numpy docs where there is an example with a complex step size inside a slice (see the 2nd example). From experiments, I can see what it is doing; it is similar to np.linspace. In [42]: np.r_[-1:1:1j] Out[42]:…
suvayu
  • 4,271
  • 2
  • 29
  • 35
5
votes
3 answers

Index multidimensional torch tensor by another multidimensional tensor

I have a tensor x in pytorch let's say of shape (5,3,2,6) and another tensor idx of shape (5,3,2,1) which contain indices for every element in first tensor. I want a slicing of the first tensor with the indices of the second tensor. I tried x=…
5
votes
2 answers

What is the most pythonic way of generating a boolean mask of an RGB image based on the colour of the pixels?

I have an image with a missing part that I know has been coloured in green (first image). What is the most pythonic way of generating another "boolean" image that shows white for the missing parts and black for the non-missing parts (second…
nim.py
  • 467
  • 1
  • 6
  • 19
4
votes
3 answers

Construct array by sampling over every n'th element along last axis

Let a be some (not necessarily one-dimensional) NumPy array with n * m elements along its last axis. I wish to "split" this array along its last axis so that I take every n'th element starting from 0 up until n. To be explicit let a have shape (k, n…
4
votes
2 answers

Numpy: What is the difference between slicing with brackets and with comma?

What is the difference between [x][y][:] and [x, y, :] in numpy? In my example I want to assign a np.ndarray into another and one way works while the other does not. With the same indices, so i really wonder why. Thanks. >>>…
4
votes
2 answers

Select all rows from Numpy array where each column satisfies some condition

I have an array x of the form, x = [[1,2,3,...,7,8,9], [1,2,3,...,7,9,8], ..., [9,8,7,...,3,1,2], [9,8,7,...,3,2,1]] I also have an array of non-allowed numbers for each column. I want to select all of the rows which only have allowed characters in…
Vedvart1
  • 302
  • 4
  • 21
4
votes
1 answer

Why is performance of in-place modification to a numpy array related to the order of dimension being modified?

import numpy as np a = np.random.random((500, 500, 500)) b = np.random.random((500, 500)) %timeit a[250, :, :] = b %timeit a[:, 250, :] = b %timeit a[:, :, 250] = b 107 µs ± 2.76 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) 52 µs ±…
JShi
  • 41
  • 2
1
2 3
34 35