Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python creating a smaller sub-array from a larger 2D NumPy array?

So I have a large NumPy array that takes the following form:

data = [[2456447.64798471, 4, 15.717, 0.007, 5, 17.308, 0.019, 6, 13.965, 0.006],
        [2456447.6482855, 4, 15.768, 0.018, 5, 17.347, 0.024, 6, 14.001, 0.023],
        [2456447.648575, 4, 15.824, 0.02, 5, 17.383, 0.024, 6, 14.055, 0.023]]

I want to create a sub array that looks like this:

[[4, 15.717, 5, 17.308, 6, 13.965], 
 [4, 15.768, 5, 17.347, 6, 14.001],
 [4, 15.824, 5, 17.383, 6, 14.055]]

Basically I want to select out the first column, and then starting at the 4th column I want to select out every 3rd column. I tried to figure this out how to approach this with something like data[1:6:?] but I didn't understand how to step through and only get the columns that I wanted.

Also I need this to be scalable for an array that extends horizontally. So I don't just want to hard code the column values.

like image 311
sTr8_Struggin Avatar asked Jun 28 '13 16:06

sTr8_Struggin


People also ask

How do I split a NumPy array into smaller arrays?

Use the hsplit() method to split the 2-D array into three 2-D arrays along rows. Note: Similar alternates to vstack() and dstack() are available as vsplit() and dsplit() .

How do I reduce the size of a NumPy array?

You can use numpy. squeeze() to remove all dimensions of size 1 from the NumPy array ndarray . squeeze() is also provided as a method of ndarray .

How do you sub an array in Python?

To get the subarray we can use slicing to get the subarray. Step 1: Run a loop till length+1 of the given list. Step 2: Run another loop from 0 to i. Step 3: Slice the subarray from j to i.


2 Answers

You could do this:

>>> data[:, [1, 2, 4, 5, 7, 8]]
array([[  4.   ,  15.717,   5.   ,  17.308,   6.   ,  13.965],
       [  4.   ,  15.768,   5.   ,  17.347,   6.   ,  14.001],
       [  4.   ,  15.824,   5.   ,  17.383,   6.   ,  14.055]])
like image 177
Eric Workman Avatar answered Nov 15 '22 06:11

Eric Workman


This will do the trick, it scales horizontally and vertically and it's easy and works.

subArray = []
newRow = []
for row in data:
    for i in xrange(0,len(row)):
        if (i % 3 == 0):
            continue
        newRow.append(row[i])
    subArray.append(newRow)
    newRow = []
like image 41
Stephan Avatar answered Nov 15 '22 06:11

Stephan