How to remove an 2d array subset?

I have a 800×800 array and I want to analize just the elements in the outter part of it.
I need a new array without the elements of the slice [5:-5,5:-5].
It doesn’t necessarily have to return a 2d array, a flat array or a list will do as well.
Example:

import numpy

>>> a = numpy.arange(1,10)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

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

I need to discard the core elements, something like:

del a[1:2,1:2]

I expect to have:

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

I tried to use numpy.delete() but it seems to work for one axis at a time.
I wonder if there is a more straight forward way to do this.

Best answer

You can use a boolean array to index your array any way you like. That way you don’t have to change any values in your original array if you don’t want to. Here is a simple example:

>>> import numpy as np
>>> a = np.arange(1,10).reshape(3,3)
>>> b = a.astype(bool)
>>> b[1:2,1:2] = False
>>> b
array([[ True,  True,  True],
       [ True, False,  True],
       [ True,  True,  True]], dtype=bool)
>>> a[b]
array([1, 2, 3, 4, 6, 7, 8, 9])