Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split array at value in numpy

Tags:

python

numpy

I have a file containing data in the format:

0.0 x1
0.1 x2
0.2 x3
0.0 x4
0.1 x5
0.2 x6
0.3 x7
...

The data consists of multiple datasets, each starting with 0 in the first column (so x1,x2,x3 would be one set and x4,x5,x6,x7 another one). I need to plot each dataset separately so I need to somehow split the data. What would be the easiest way to accomplish this?

I realize I could go through the data line-by-line and split the data every time I encounter a 0 in the first column but this seems very inefficient.

like image 663
pafcu Avatar asked Mar 11 '11 14:03

pafcu


People also ask

How do I split an array into equal parts in NumPy?

Splitting NumPy Arrays Joining merges multiple arrays into one and Splitting breaks one array into multiple. We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

Can you divide NumPy array?

You can also use the function to divide a Numpy array by a scalar value (i.e., divide a matrix by a scalar).

How do you separate elements in an array?

Example-2: This example uses the splice() method to split the array into chunks of array. This method removes the items from the original array. This method can be used repeatedly to split array of any size. | Split array into chunks.

How do you split an array into two?

To divide an array into two, we need at least three array variables. We shall take an array with continuous numbers and then shall store the values of it into two different variables based on even and odd values.


1 Answers

I actually liked Benjamin's answer, a slightly shorter solution would be:

B= np.split(A, np.where(A[:, 0]== 0.)[0][1:])
like image 184
eat Avatar answered Oct 02 '22 22:10

eat