1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see I wasn't using the comma correctly. I assume that this references all of the first axis?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.