Questions tagged [numpy-slicing]

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

Read more:

518 questions
4
votes
1 answer

Why is numpy's behavior different if I slice out only 1 column vs. if I slice out multiple columns?

If I have a numpy array called data, for example: [[ 3.6216 8.6661 -2.8073 -0.44699 0. ] [ 4.5459 8.1674 -2.4586 -1.4621 0. ] [ 3.866 -2.6383 1.9242 0.10645 0. ]] and I want to get the last column, I can…
Fuad
  • 1,419
  • 1
  • 16
  • 31
4
votes
1 answer

Find Top10(n) RGB colors in Numpy

I have a function that eat a couple of seconds. The function should return the Top(n) colors in a given Image. The return must be sorted, so i could work with the rgb values from the first, second, third top most color. On the fist hand i had an…
Incur
  • 75
  • 7
4
votes
2 answers

How to sum elements of the rows of a lattice periodically

Suppose I have a lattice a = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]) I'd like to make a function func(lattice, start, end) that takes in 3 inputs, where start and end are the…
user3613025
  • 373
  • 3
  • 10
4
votes
1 answer

Slicing the last 2 columns of a numpy array

How can I slice the last 2 columns of a numpy array? for example: A = np.array([[1, 2, 3], [4, 5, 6]]) and I want to get B as the last 2 columns of A which is [[2, 3], [5, 6]] I know that I can index it from start of the array such as B = A[:, 1:3].…
pymn
  • 171
  • 2
  • 10
4
votes
2 answers

Is indexing with a bool array or index array faster in numpy/pytorch?

I can index my numpy array / pytorch tensor with a boolean array/tensor of the same shape or an array/tensor containing integer indexes of the elements I'm after. Which is faster?
drevicko
  • 14,382
  • 15
  • 75
  • 97
4
votes
1 answer

Why using an array as an index changes the shape of a multidimensional ndarray?

I have a 4-D NumPy array, with axis say x,y,z,t. I want to take slice corresponding to t=0 and to permute the order in the y axis. I have the following import numpy as np a = np.arange(120).reshape(4,5,3,2) b = a[:,[1,2,3,4,0],:,0] b.shape I get…
3
votes
2 answers

Slice a multidimensional pytorch tensor based on values in other tensors

I have 4 PyTorch tensors: data of shape (l, m, n) a of shape (k,) and datatype long b of shape (k,) and datatype long c of shape (k,) and datatype long I want to slice the tensor data such that it picks the element addressed by a in 0th…
Nagabhushan S N
  • 6,407
  • 8
  • 44
  • 87
3
votes
2 answers

Numpy array slicing to return sliced array and corresponding array indices

I'm trying to generate two numpy arrays from one. One which is a slice slice of an original array, and another which represents the indexes which can be used to look up the values produced. The best way I can explain this is by example: import numpy…
3
votes
2 answers

Extracting multiple sets of rows/ columns from a 2D numpy array

I have a 2D numpy array from which I want to extract multiple sets of rows/ columns. # img is 2D array img = np.arange(25).reshape(5,5) array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18,…
3
votes
2 answers

NumPy - Updating all values in array, except for slice

I have the following array: arr = np.array([[1, 1, 1, 1, 1, 1], [2, 0, 0, 0, 0, 2], [3, 0, 0, 0, 0, 3], [4, 4, 4, 4, 4, 4]]) inverse_slice = arr[1:3, 1:5] I want to update the values of all values in the array,…
Rudy
  • 437
  • 2
  • 7
3
votes
2 answers

How to get field of nested numpy structured array (advanced indexing)

I have a complex nested structured array (often used as a recarray). Its simplified for this example, but in the real case there are multiple levels. c = [('x','f8'),('y','f8')] A = [('data_string','|S20'),('data_val', c, 2)] zeros = np.zeros(1,…
001001
  • 530
  • 1
  • 4
  • 13
3
votes
3 answers

How to convert and reshape MultiIndex to 3D Numpy array?

I have 4D data in a data frame. I need to convert it to 3D Numpy array. I can do it with for-loops, but is there more efficient way? # Data: df = pd.DataFrame() df['variable'] = ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D', 'A', …
hermo
  • 95
  • 4
3
votes
2 answers

Replacing numpy array elements with chained masks

Consider some array arr and advanced indexing mask mask: import numpy as np arr = np.arange(4).reshape(2, 2) mask = A < 2 Using advanced indexing creates a new copy of an array. Accordingly, one cannot "chain" a mask with an an additional mask or…
Bob
  • 428
  • 3
  • 11
3
votes
3 answers

Append dimensional numpy array

I have two numpy array matrix A and B of length 32 and shape (32, 32, 3) each . I want to combine them into a new array so that the dimensions of my new array is (2, 32, 32,3). Using np. concatenate is throwing an error.
Manasi
  • 179
  • 1
  • 8
3
votes
2 answers

Unable to slice an array correctly

I am having the following array import numpy as np T = np.array(([1,-1,0,0,0,0], [3,0,5,0,0,0], [0,-1,0,9,0,0], [0,0,0,0,-2,1], [3,1,0,0,2,0], [1,-2,1,0,2,2])) I want to remove…
Jan
  • 47
  • 7
1
2
3
34 35