GeoMonkey
GeoMonkey

Reputation: 1671

Subset a 3d numpy array

I have checked the numpy documentation but some of the indexing still eludes me. I have a numpy array such that its shape is (40000, 432) and its looks something like:

arr = [[1,2,3......431,432],
       [1,2,3......431,432],
       [1,2,3......431,432],
       ....................
       [1,2,3......431,432]'
       [1,2,3......431,432]]

I wanted to subset each array over a range (ie. 20-50) so that the shape will be (40000, 30) and it will look like:

subarr = [[20,21,22...48,49,50],
          [20,21,22...48,49,50],
          [20,21,22...48,49,50],
          .....................
          [20,21,22...48,49,50]]

Everything I try either returns me an error or gives me the shape (30, 432) which is not what I need. How do I subset a 2d array along the axis I want to?

Upvotes: 1

Views: 2676

Answers (1)

gtlambert
gtlambert

Reputation: 11971

You want to use numpy slicing:

arr = np.zeros((40000, 432))
subarr = arr[:, 20:50]
print(subarr.shape)

Output

(40000L, 30L)

The L in the shape output indicates that the integer is of Python type long.

Upvotes: 2

Related Questions