Questions tagged [numpy-slicing]

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

Read more:

518 questions
2
votes
1 answer

How to vectorize a moving Numpy slice window

I have two numpy ndarrays, array1 and array 2, with array1.shape = array2.shape = (n, l, m). A 3rd ndarray is initialized as array3 = np.nan * np.zeros((n-1, l, m + 1)) and is then computed using the following for loop: for i in range(m): …
2
votes
0 answers

Problems with sequentially merge 2 numpy(.npy) files and slice the 2D Array/Matrix in a for loop?

I've multiple .npy files in a directory which contain 2D array of the same shape (row=500,column=55). Due to some constraints, every time I can load 2 files, stack them vertically and slice to make a new array of shape (432,55). I want to slice…
2
votes
1 answer

Getting unexpected shape while slicing a numpy array

I have a 4D numpy array. While slicing for multiple indices in a single dimension, my axis get interchanged. Am I missing something trivial here. import numpy as np from smartprint import smartprint as prints a = np.random.rand(50, 60, 70, 80) b =…
lifezbeautiful
  • 1,160
  • 8
  • 13
2
votes
1 answer

Is there a way to variable slicing in numpy?

I'm trying to slice an array in a variable way. For example, from row 0 up to row 10, slice the array using a step of 2. From row 10 to row 30 slice it using a step of 3. Also, it is worth mentioning that I'm trying to do so without writing any…
rober_dinero
  • 311
  • 2
  • 7
2
votes
4 answers

Why does a[1:-1:-1] with a=[1,2,3] return []?

I am observing that if a is a list (or a numpy array) with elements [1,2,3] and I ask for a[1:-1:-1], then I get the empty list. I would expect to get [2,1] assuming that the slicing spans the indexes obtainable decrementing from 1 to -1 excluding…
callegar
  • 223
  • 1
  • 2
  • 7
2
votes
1 answer

Packing problem : Fit cuboids into a larger cuboid with constraints

I have a 3D numpy array which represents a mask. The "1" elements of this mask are areas where no calculation is done (blue areas in the figure below). The "0" elements of the mask are areas for which a calculation is made. Each calculation is…
Ipse Lium
  • 970
  • 1
  • 6
  • 19
2
votes
1 answer

The elementwise square of subset of numpy array

I have the following numpy array np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) I want to compute the elementwise square of second column and third column only by retaining the first column as it is, yielding the…
2
votes
1 answer

Numpy where using a list of values

if I wanted to create a filter for a numpy array for several values how would I write that. For instance say I want to do this... clusters = np.array([4, 570, 56, 2, 5, 1, 1, 570, 32, 1]) fiveseventy_idx = np.where((clusters == 1) | (clusters == 570…
Angus Campbell
  • 563
  • 4
  • 19
2
votes
2 answers

How to use numpy randint function with broadcasting?

I recently came across the following use of numpy from this link numpy randint manual. The last example in this page shows this command with no explanation. import numpy x=numpy.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8) Can you…
Aleph
  • 224
  • 1
  • 11
2
votes
1 answer

Numpy chained index "force" to be view rather than copy

Basically I want this chained slicing to overwrite the value (show the first element as a 2 instead of 1) tst = np.array([1,2,3,4]) msk1 = [True, False, True, False] msk2 = [True, False] tst[msk1][msk2] = 2 tst > array([1, 2, 3, 4])
qwertylpc
  • 2,016
  • 7
  • 24
  • 34
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
2 answers

Maxpooling 2x2 array only using numpy

I want help in maxpooling using numpy. I am learning Python for data science, here I have to do maxpooling and average pooling for 2x2 matrix, the input can be 8x8 or more but I have to do maxpool for every 2x2 matrix. I have created an matrix by…
2
votes
5 answers

Slice all except first element, unless single element

I'd like to slice a numpy array to get all but the first item, unless there's only one element, in which case, I only want to select that element (i.e. don't slice). Is there a way to do this without using an if-statement? x =…
vsocrates
  • 172
  • 13
2
votes
3 answers

Get 1d numpy array from 2d array using list of indices

I have a 2d array (n x m) from which I would like to produce a 1d array (length n) using a list of row-indices of length n. For instance: 2d = ([a,b,c],[d,e,f],[g,h,i]) # input array 1d = ([0,2,1]) # row numbers result = ([a,e,h]) # array of the…
Robbie Mallett
  • 131
  • 1
  • 11
2
votes
2 answers

Can torch.where() used in a equivalent broadcsating form?

I have the following segment of for loop in my code. The nested loop is slowing down my complete execution. for q in range(batchSize): temp=torch.where((composition_matrix == pred[q]).all(dim=1))[0] if len(temp)==0: output[q]=0 …