Questions tagged [numpy-slicing]

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

Read more:

518 questions
2
votes
1 answer

Is there a way to extract a solid box slice of an arbitrary multidimensional Python array given two sets of corner index coordinates?

Suppose I have a = np.arange(16).reshape(4,4), which is array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) And want to slice a as I could with a[0:3,1:4], which results in array([[ 1, 2, 3], …
2
votes
2 answers

efficient way to get all numpy slices for different ranges

I want to slice the same numpy array (data_arra) multiple times to find each time the values in a different range data_ar shpe: (203,) range_ar shape: (1000,) I implemented it with a for loop, but it takes way to long since I have a lot of…
MadMax
  • 143
  • 4
  • 14
2
votes
2 answers

"only integer scalar arrays can be converted to a scalar index"

I am generating a numpy array with 1000000 random numbers between 1 and 6 and i would like to calculate the mean of the first 10, 100, 1000, ... I also want to plot the means on logarithmic scale. I mustn't use anything but Python with numpy and…
AufsMaulwurf
  • 41
  • 1
  • 6
2
votes
2 answers

Select multiple rows multiple times from a numpy array at once avoiding loops

I am looking for a way to select multiple rows from a numpy array multiple times given an array of indexes. Given M and indexes, I would like to get N avoiding for loop, since it is slow for big dimensions. import numpy as np M = np.array([[1, 0, 1,…
allucas
  • 21
  • 1
2
votes
3 answers

how to assign values to numpy array given the row indices and starting column indices?

a = np.array([2,3,1,4]) b = np.array([2,3,7,1]) c = np.zeros((4, 10)) I wanna assign value 1 to some elements in c. a and b define the positions of such elements. a is the starting column indices of value 1 in each row. And b represents how many…
tczj
  • 438
  • 4
  • 17
2
votes
1 answer

Weird result in setting column values of a numpy array

In the script below, I want to apply a rotation matrix to the first two columns of a (Nx3) array. rotate_mat = lambda theta: np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]]) rot_mat = rotate_mat(np.deg2rad(90)) basis1 =…
meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34
2
votes
0 answers

Pythonic way of slicing array by list indices

I was wondering what is the best way to perform a batched slice (torch/numpy)? I know for constant slicing indices it is possible to perform this operation batch_size = 2 data = torch.zeros((batch_size,1, 256, 256)) x_start = 10 x_stop = 20 y_start…
Alexus
  • 1,282
  • 1
  • 12
  • 20
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
1 answer

Mask 3D-array by 2D-array for slicing without for loop

I have something like this import numpy as np array_3D = np.random.rand(3,3,3) array_2D = np.random.randint(0, 3 , (3,3)) for i in range(3): for j in range(3): array_3D[:, i, j][:array_2D[i, j]]=np.nan Is there a way to do this…
clearseplex
  • 679
  • 1
  • 7
  • 22
2
votes
3 answers

How to make a `numpy` array view that repeats

How can I slice a numpy array beyond its shape such that values in the array are repeated without having to store the entire array in memory? Here's what I'd like to do: x = numpy.array([[1, 2], [3, 4]]) x[0:3, 0:3] -> [[1, 2, 1, 2], [3, 4, 3, 4], …
Rich
  • 12,068
  • 9
  • 62
  • 94
2
votes
4 answers

For loop in numpy 3d arrays

I want to take a basic 3d array like this: b = np.arange(1,101).reshape(4,5,5) b Then I want to take the first index, and work down like a stairs. b1 = [b[0:,0,0],b[0:,1,1],b[0:,2,2],b[0:,3,3],b[0:,4,4]] b1 = np.asarray(b1) b1 =…
DillM94
  • 71
  • 4
2
votes
1 answer

Python advanced Numpy slicing and indexing to vectorize method

I am trying to vectorize this method that I am using for image augmentation in ML: def random_erase_from_image(images, random_erasing, image_size): #could probably be vectorized to speed up to_return = images for t in range(images.shape[0]): if…
2
votes
2 answers

Numpy Advanced Indexing : How the broadcast is happening?

array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) if we run the following statement x[1:, [2,0,1]] we get the following result array([[ 6, 4, 5], [10, 8, 9]]) According to numpy's doc: Advanced indexes always…
2
votes
3 answers

Indexing with Boolean arrays

a = np.arange(12).reshape(3,4) b1 = np.array([False,True,True] b2 = np.array([True,False,True,False]) a[b1,b2] output: array([4,10]) I am not getting how it comes 4 and 10 in a[b1,b2]
Go Beyond
  • 118
  • 12
2
votes
4 answers

Slicing every 5th and 6th element from a numpy row vector

I have an array like this: A = ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) and would like to extract every 5th "and" 6th element from this row vector, leading to B = ([4,5,9,10,14,15]). I know how to extract every 5th element: B =…
Jenni
  • 23
  • 3