Questions tagged [numpy-slicing]

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

Read more:

518 questions
1
vote
0 answers

NumPy: how to check leading dimension of non-owned array?

In NumPy, one can have arrays which are just pointing towards data that was allocated in a different array in order to avoid deep copies. For example: import numpy as np a = np.ones((3,4)) b = a[:2] Now b just contains a pointer to the array in a,…
anymous.asker
  • 1,179
  • 9
  • 14
1
vote
1 answer

Python Pandas select rows in numpy array on first columns

I have a dataframe like this: df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6],'C':[7,8,9],'D':[10,11,12]}) and a list, here arr, that may vary in length like this: arr = np.array([[1,4],[2,6]]) arr = np.array([[2,5,8], [1,5,8]]) And I would like get…
Nicolas Rey
  • 431
  • 2
  • 6
  • 19
1
vote
1 answer

How to efficiently shuffle some values of a numpy array while keeping their relative order?

I have a numpy array and a mask specifying which entries from that array to shuffle while keeping their relative order. Let's have an example: In [2]: arr = np.array([5, 3, 9, 0, 4, 1]) In [4]: mask = np.array([True, False, False, False, True,…
1
vote
1 answer

How to change the order numpy stores the data?

I have a numpy array that I need to change the order of the axis. To do that I am using moveaxis() method, which only returns a view of the input array, by changing only the strides of the array. However, this does not change the order that the data…
BayesianMonk
  • 540
  • 7
  • 23
1
vote
1 answer

Why slicing in boolean filtered array is not happening..?

When I try to slice the boolean filtered numpy array it's not changing its value. please look a array = np.array([1,2,3,4,5,6,7,8]) array[array>=2] array([2, 3, 4, 5, 6, 7, 8]) array = np.array([1,2,3,4,5,6,7,8]) array[array>=2][1:5] array([3, 4,…
1
vote
2 answers

How do I enumerate a 2-dimensional NumPy array excluding the first and last rows and columns?

I would like to enumerate the elements of a 2-dimensional NumPy array excluding the first and last row and column (i.e. the ones in the matrix below). import numpy as np q = np.zeros((4, 4)) q[1, 1] = 1 q[1, 2] = 1 q[2, 1] = 1 q[2, 2] = 1 # [[0.…
Frank
  • 3,029
  • 5
  • 34
  • 43
1
vote
3 answers

slicing periodically for a numpy array

I have a numpy array a = np.arange(100) including 100 numbers. I wondered to know is there a way to slice it periodically rather than using the conditional statements. for example, if I want to slice the 1st four numbers + 5th four numbers + 9th…
user14655550
1
vote
1 answer

I'm multiplying a numpy matrix column by a float and getting a strange rounding. What is causing this?

I trying to update the column of a matrix by multiplying it with a float. See the code below. H = np.array([[1, 2, 3], [4, 5, 6]]) print(H[:, 0] * 0.1) H[:, 0] = H[:, 0] * 0.1 print(H[:, 0]) Which gives the output: [0.1 0.4] [0 0] Numpy seems to…
ViktorEM
  • 13
  • 2
1
vote
1 answer

"IndexError: too many indices for array" when trying to count unique items in numpy array column

I have a numpy array called data which has 8 columns and gets recursively manipulated in the rows so it has a variable number of rows each time it passes through the function I need to apply. Inside that function I have the following line of code…
1
vote
2 answers

Confused on Index Slicing in python

I am slicing my hour column and when I use slice(1, 3) and slice(0, 3) I get the same results. What am i missing? See in the image below.
Meet Shah
  • 73
  • 1
  • 8
1
vote
0 answers

Combination of slicing and array index in numpy

Looking at the answers to this question: How to understand numpy's combined slicing and indexing example I'm still unable to understand the result of indexing with a combination of a slice and two 1d arrays, like this: >>> m =…
shenun
  • 85
  • 5
1
vote
1 answer

Numpy get values at indices given in array form

I want to get the values at indices of my_array. indices = np.array([[[0], [1], [0]]]) my_array = np.array([[[1.1587323 , 1.75406635], [1.05464125, 1.29215026], [0.9784655 , 1.16957462]]]) I should get the…
1
vote
3 answers

finding all the points that belong to a plane using python

I have an mx3 array that is used to create a 3d model. Is there is a fast way to extract all the points that belong to a given plane using numpy or other python functions? The plane will take the Ax+By+Cz+D=0 form. I'm currently looping through all…
yihao ren
  • 369
  • 1
  • 4
  • 15
1
vote
4 answers

How to concatenate sliced array to a list in Python

I am trying to merge a sliced array to a list in Python but i get an error: ValueError: operands could not be broadcast together with shapes `(4,)` `(2,)` . This is my code: y = np.array([5,3,2,4,6,1]) row = y[2:6] + np.array([0,0]) I am…
1
vote
0 answers

NumPy ndarray slicing and indexing using comma

What basically the commas do in slicing nd array in NumPy? E.g. for a 3d array, what do these mean? a[1,2::] a[:2,3:] a[1:2,:] I means commas in general, how do these thing work while slicing? I know about a[start:stop:step] but having hard time…
cyclonejet
  • 41
  • 1
  • 6