Questions tagged [numpy-slicing]

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

Read more:

518 questions
1
vote
0 answers

Numba TypingError when indexing 2D array with 2D array

I'm trying to index a 2D array with another 2D array which works fine in Numpy, but I want to use it in a function decorated with Numba's njit decorator, which gives me this error: TypingError: No implementation of function Function(
kynnemall
  • 855
  • 9
  • 26
1
vote
2 answers

numpy joint two np.arrays in weird manner

I have to np.arrays which I need to join in a really weird manner. Unfortunately the shapes are givn, I can neither change output nor input. frequencies = [100. 200.] (2,) and values = [[1. 2.] [3. 4.] [5. 6.] [7. 8.]] (4, 2) The required…
1
vote
0 answers

Slice array based by providing string ':' as index in Numpy

I am in a position to extract whole data from an array. The simplest method would be simply passing array[:]. However, I want to make it automated as part of the larger project where the index would be varying with the data format. Therefore, is it…
sundar_ima
  • 3,604
  • 8
  • 33
  • 52
1
vote
1 answer

Adding block-constant matrices in numpy

Let's say we have a n*n matrix A m*m matrix B a vector b=[b_1,...,b_m] of block sizes with b_1 + ... + b_m = n and b_i >= 1. Then I define a n*n block matrix B_block whose (i,j)-th block is a b_i*b_j matrix of constant value B_{ij}. How can I…
1
vote
2 answers

Numpy Array: Slice several values at every step

I am trying to extract several values at once from an array but I can't seem to find a way to do it in a one-liner in Numpy. Simply put, considering an array: a = numpy.arange(10) > array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) I would like to be able to…
soubaboy
  • 13
  • 3
1
vote
2 answers

What is the Numpy slicing notation in this code?

# split into inputs and outputs X, y = data[:, :-1], data[:, -1] print(X.shape, y.shape) Can someone explain the second line of code with reference to specific documentation? I know its slicing but the I couldn't find any reference for the…
1
vote
1 answer

Indexing ndarray with unknown number of dimensions with range dynamically

I have data array with unknown shape and array bounds of bounds for slicing data. This code is for 3D data, but is there any way of generalizing this to N-dim? for b in bounds: l0, u0 = b[0] l1, u1 = b[1] l2, u2 = b[2] a =…
1
vote
1 answer

Numpy subarrays and relative indexing

I have been searching if there is an standard mehtod to create a subarray using relative indexes. Take the following array into consideration: >>> m = np.arange(25).reshape([5, 5]) array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], …
BorjaEst
  • 390
  • 2
  • 11
1
vote
2 answers

multiply a vector and a matrix to create a 3D matrix in numpy

I have a matrix A of zise MxN, and vector b of size L. how can I create a matrix C of size MxNxL such that: C[m, n, k] = A[m, n] * b[k] pretty much the same as dot product of two vectors to create 2D matrix, but with one dimention higher. I had…
1
vote
1 answer

Slicing a 3D tensor with a 1D tensor-index in PyTorch

How can I slice a 3D tensor using a 1D tensor? For instance, consider the following 2 tensors: t of size [Batch, Sequence, Dim]; and idx of size [Batch]. The values of idx are restricted to be integers between 0 and Sequence-1. I need tensor idx to…
Oren
  • 171
  • 1
  • 8
1
vote
1 answer

How can I extract a set of 2D slices from a larger 2D numpy array?

If I have a large 2D numpy array and 2 arrays which correspond to the x and y indices I want to extract, It's easy enough: h = np.arange(49).reshape(7,7) # h = [[0, 1, 2, 3, 4, 5, 6], # [7, 8, 9, 10, 11, 12, 13], # [14, 15, 16, 17, 18, 19,…
1
vote
3 answers

Printing columns of a list of arrays

I have the following list import numpy as np Y = [np.array([[1, 4, 7], [2, 5, 8]]), np.array([[10, 14, 18], [11, 15, 19], [12, 16, 20], [13, 17, 21]]), np.array([[22, 26, 31], [24, 28, 33], [26, 30,…
Amelia
  • 97
  • 8
1
vote
1 answer

Slicing every second row and column of a 3d numpy array, making a new array

I have a NumPy array of dimension 698x64x64 (64rows, 64columns) I would like to slice it into a new array of 698x32x32 by taking every second row and column of the original array. How do I do that?
1
vote
0 answers

How to reduce memory consumption while splitting a Pandas DataFrame

I have a pandas dataframe which has more than 5 millions rows. I try to slice it (time series to supervised frame) by using the function below. However it consumes too much ram and collaps during long sequences (look_back, forecast_horizon). As I…
ast
  • 35
  • 1
  • 7
1
vote
2 answers

Slicing along NumPy ndarray axis with varying indices

Given a ndarray ar of shape (n, m) I want to "extract" subsequences along axis 1 of length k with k
derkurt
  • 11
  • 2