43

I'd like to extract a numpy array with a specified size from a numpy 2d array--essentially I want to crop the array. For example, if have a numpy array like this:

([1,2,3],
 [4,5,6],
 [7,8,9])

I'd like to extract a 2x2 from it and the result should be:

([1,2],
 [4,5])

How can I do that?

1
  • You can use numpy indexing array[:2,:2] Commented Feb 28, 2016 at 10:00

1 Answer 1

57

Given this array:

>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

You can slice it along both dimensions:

>>> a[:2,:2]
array([[1, 2],
       [4, 5]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.