Questions tagged [numpy-slicing]

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

Read more:

518 questions
1
vote
2 answers

Numpy slicing with batch size

I have a numpy array A of shape (550,10). I have batch size of 100 i.e how much data rows I want from A. In each iteration I want to extract 100 rows from A. But when I reach last 50 rows, I want last 50 and first 50 rows from A. I have a function…
Pawandeep Singh
  • 830
  • 1
  • 13
  • 25
1
vote
1 answer

Numpy sclicing multiple dimensions simulteanously

Given a 2D array and two pairs of indices, defining the upper left and lower right corner of a sub-matrix respectively: a = np.arange(25).reshape(5,5) # array([[ 0, 1, 2, 3, 4], # [ 5, 6, 7, 8, 9], # [10, 11, 12, 13, 14], # …
scleronomic
  • 4,392
  • 1
  • 13
  • 43
1
vote
0 answers

NumPy indexing ambiguity in 3D arrays

I have the following 3D array of shape In [159]: arr = np.arange(60).reshape(3, 4, 5) And I'm trying to do advanced indexing to extract a sub-array like: # behaves as expected In [160]: arr[[1, 2], :, 1].shape Out[160]: (2, 4) In the following…
0
votes
0 answers

Making copy of a numpy array within a function, then replacing a value and returing that new array(within the same function)

I am writing a function that takes three inputs: a numpy array, a number, and another number. I want to take a slice of the array, and replace the first value with another value. I do not want to modify the original array passed into the function as…
Brailey
  • 1
  • 1
0
votes
1 answer

How do I sparsely add the rows of a numpy array to another numpy array?

I have a large 2D array (A) and a smaller 2D array of the same number of columns but fewer number of rows (B). I want to add the rows of my smaller array to rows of my larger array, but there are rows in between that I don't want to affect. I have…
0
votes
0 answers

Numpy way of building an irregular array of arrays

Let's assume I have a huge (actually, it would have around a ~1_000_000 values) numpy array of floats. data = np.arange(100).astype(np.float64) In parallel I created two numpy arrays of integers (that would be indices later on) a =…
A. G
  • 187
  • 7
0
votes
2 answers

How can I use slicing in numpy arrays correctly?

I have a piece of code that has to generate a gaussian distribution over a few pixels of an image based on a set of coordinates and a confidence. import numpy as np def gen_single_joint_heatmap(x, y, conf, img_size, sigma=0.6, eps=1e-6): heatmap =…
Neskelogth
  • 91
  • 3
  • 12
0
votes
1 answer

Generalising to arbitrarily sized arrays in numpy

I currently have an input grid and function which i'd like to pass onto every element of the grid (dependent on the number of dimensions of the original grid). Issue is, I currently have several different implementations of passing the function…
0
votes
3 answers

How return all positions of a given subarray of an array

Consider a Numpy array C of shape (s_1,...,s_k) and another array A of shape (s_j,...,s_k) where j > 1. Is there a function in Python to return the list [p_1,...,p_l] of positions of the form p_r == [x_1,...,x_{j-1}] such that C[x_1,...,x_{j-1}] ==…
Physor
  • 101
  • 3
0
votes
2 answers

Retain original array structure in np.where

Take the following example: x = np.transpose(np.array([np.arange(10), np.zeros(10, dtype=int)])) x = np.array([x, x, x]) print("orig: \n", x) print("") print("indexed: \n", x[np.where(np.logical_and(x[..., 0] > 3, x[..., 0] < 7))]) This…
MattHusz
  • 452
  • 4
  • 15
0
votes
4 answers

Multi-dimensional indexing of numpy arrays along inner axis

I have a numpy array x with shape [4, 5, 3] I have a 2D array of indices i with shape [4, 3], referring to indices along dimension 1 (of length 5) in x I'd like to extract a sub-array y from x, with shape [4, 3], such that y[j, k] == x[j, i[j, k],…
Jake Levi
  • 1,329
  • 11
  • 16
0
votes
1 answer

How can I apply a function to each channel of a three-channel image numpy array without using a for-loop?

Suppose that I have an image of three channels, and its corresponding numpy array is of the shape (H,W,3). Now I create a function which does something to a 2D numpy array. How can I apply the funtion to each channel of the image without using a…
LuigiNixy
  • 1
  • 1
0
votes
1 answer

How to implement __getitem__ such that it can process the input args and then pass them into the underlying numpy array?

Let A be a simple python class that has one member self._mat which is a numpy array. The constructor of A gets an integer n and creates an inner n by n zeros numpy array and saves it as the private member self._mat. How can I implement __getitem__…
Tal Afek
  • 111
  • 5
0
votes
1 answer

Copy data from one numpy array to an other with possibly missmatched sizes

So my issue is that I'm trying to paste a small image into a larger image with the typical numpy syntax : large_im = np.linspace(0,1,20)[:,None]*np.ones([1,20]) small_im = np.linspace(1,0,10)[None,:]*np.ones([10,1]) a0,b0 =…
jadsq
  • 3,033
  • 3
  • 20
  • 32
0
votes
1 answer

Splitting numpy arrays & rearranging them

I have 10 seconds of data from 7 devices each with 2 channels in a 1D array, sampled at 51.2KHz. I split the array into to 10 time_split = np.array_split(data, 10) I then split each element of time_split into the samples for each…
DrBwts
  • 3,470
  • 6
  • 38
  • 62