Questions tagged [numpy-slicing]

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

Read more:

518 questions
1
vote
0 answers

Contracting a tensor with a selection rule

Imagine that you have a tensor $T_{ijkl}$ (0<=i,j,k,l< N) that has the following property T_{ijkl} is nonzero if i+j=k+l which acts as a "selection rule". You want to contract the tensor T with another tensor u to make a third tensor v as…
1
vote
0 answers

Populate already split numpy array

I am trying to avoid using list of lists and appending during my loop, so I've figured I can create a large np.zeros array, then use np.split to split it in batches, and populate the batches. The code looks like this: N = inputData.size -…
deblue
  • 277
  • 4
  • 18
1
vote
0 answers

Index numpy array with triplets from list

I have a 3d numpy array A representing trigram language model. So A[i, i-1, i-2] is the probability $P(w_i|w_{i-1},w_{i-2})$, where $w$ are consecutive words. I want to extract all probabilities for a sequence of words. Now I am using the…
JAV
  • 279
  • 2
  • 9
1
vote
1 answer

selecting from a multi-dimesional array with a list of indices

Let's say I have an array with size batch x max_len x output_size, where batch, max_len, and output_size all correspond to positive natural numbers. I have a list of indices which correspond to individual items in dimension 1 (i.e. max_len). How can…
Clement Attlee
  • 723
  • 3
  • 8
  • 16
1
vote
2 answers

Indexing of structured numpy arrays

I have a np.ndarray (call it arr) that looks something like this: # python 3.7 import numpy as np my_dtype = [("x", "float32"), ("y", "float32"), ("some_more", "int32"), ("and_more_stuff", "uint8")] # part1 = np.zeros(5,…
Adomas Baliuka
  • 1,384
  • 2
  • 14
  • 29
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
2 answers

Slice a submatrix with center element indices

Given a matrix A, and a list of row indices, and a list of column indices, how to efficiently extract the squared submatrices with size k centered by the row and column indices? For example: A = array([[12, 6, 14, 8, 4, 1], [18, 13, 8,…
tczj
  • 438
  • 4
  • 17
1
vote
1 answer

Rolling an array axis by varying amounts in ndimensions (generalizing strided indexing roll)

I have an array of arbitrary shape, but let's say, (A, B, C), and I'd like to roll the last axis by a different amount for each element (i.e. for each (A, B)). I'm trying to generalize @Divakar's beautiful solution here for 2D arrays, but I don't…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
1
vote
1 answer

Finding fixed-length contiguous regions of an nan-filled array (no overlap)

I've found similar questions posted here but none which apply to row-defined time series data. I'm anticipating the solution might be found via numpy or scipi. Because I have so much data, I'd prefer not to use pandas dataframes. I have many runs…
1
vote
1 answer

I don't know the meaning of the code "[:, row:row]"

I have the code: g, g_err = data[:, 4:6].T I don't know the meaning of [:, 4:6] especially the first : and does .T mean transpose?
CapKoko
  • 49
  • 1
  • 5
1
vote
1 answer

Python/Numpy: How to extract interior of any dimension of numpy array?

Suppose I have a numpy array A which can be of any dimensions len(A.shape) can be 1,2,3,..etc. and a corresponding array, crop which len(crop) = len(A.shape) and I want to extract the interior values of A using crop. Here is an example for 2D…
Ong Beng Seong
  • 196
  • 1
  • 11
1
vote
1 answer

Efficiently using 1-D pyfftw on small slices of a 3-D numpy array

I have a 3D data cube of values of size on the order of 10,000x512x512. I want to parse a window of vectors (say 6) along dim[0] repeatedly and generate the fourier transforms efficiently. I think I'm doing an array copy into the pyfftw package…
1
vote
0 answers

Boolean slicing swaps dimensions of numpy array

for some nd-array a = np.zeros((2,5,4)) I want to slice the last dimension based on some boolean statement. a[1,:, (True,True,True,True)].shape however, returns (4,5) while I would expect a shape of (5,4) like from a[1,:, :].shape So, what's…
forch
  • 11
  • 2
1
vote
2 answers

Backwards Slicing

I have a numpy array from 0 to 999 and I would like to make a slice that runs from the last element in the list (999) to the one in the middle (500). test[500:][::-1] works but if I have a two dimensional array and I only want to do so along the…
Pickniclas
  • 349
  • 1
  • 8
1
vote
0 answers

Performance decreases with increasing nesting of array elements

A short note: This question relates to another I asked previously, but since asking multiple questions within a single Q&A is concidered bad SO-style I splitted it up. Setup I have the following two implementations of a matrix-calculation: The…
Markus
  • 2,265
  • 5
  • 28
  • 54