如何删除2d数组子集?

时间:2022-05-12 21:33:08

I have a 800x800 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:

我有一个800x800阵列,我想分析它的外部元素。我需要一个没有切片元素的新数组[5:-5,5:-5]。它不一定要返回2d数组,平面数组或列表也可以。例:

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.

我试图使用numpy.delete(),但它似乎一次适用于一个轴。我想知道是否有更直接的方式来做到这一点。

2 个解决方案

#1


6  

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])

#2


2  

You can replace the middle region with some placeholder value (I used -12345, anything that can't occur in your actual data would work), then select everything that is not equal to that value:

您可以使用一些占位符值替换中间区域(我使用-12345,实际数据中不会出现的任何内容都可以使用),然后选择不等于该值的所有内容:

>>> import numpy as np
>>> a = np.arange(1,26)
>>> a.shape = (5,5)
>>> a
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])

>>> a[1:4,1:4] = -12345
>>> a
array([[     1,      2,      3,      4,      5],
       [     6, -12345, -12345, -12345,     10],
       [    11, -12345, -12345, -12345,     15],
       [    16, -12345, -12345, -12345,     20],
       [    21,     22,     23,     24,     25]])
>>> a[a != -12345]
array([ 1,  2,  3,  4,  5,  6, 10, 11, 15, 16, 20, 21, 22, 23, 24, 25])

If you use a float array rather than an integer array, you can do it a little more elegantly by using NaN and isfinite:

如果使用float数组而不是整数数组,则可以使用NaN和isfinite更优雅地执行:

>>> a = np.arange(1,26).astype('float32')
>>> a.shape = (5,5)
>>> a[1:4,1:4] = np.nan
>>> a
array([[  1.,   2.,   3.,   4.,   5.],
       [  6.,  nan,  nan,  nan,  10.],
       [ 11.,  nan,  nan,  nan,  15.],
       [ 16.,  nan,  nan,  nan,  20.],
       [ 21.,  22.,  23.,  24.,  25.]], dtype=float32)
>>> a[np.isfinite(a)]
array([  1.,   2.,   3.,   4.,   5.,   6.,  10.,  11.,  15.,  16.,  20.,
    21.,  22.,  23.,  24.,  25.], dtype=float32)

#1


6  

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])

#2


2  

You can replace the middle region with some placeholder value (I used -12345, anything that can't occur in your actual data would work), then select everything that is not equal to that value:

您可以使用一些占位符值替换中间区域(我使用-12345,实际数据中不会出现的任何内容都可以使用),然后选择不等于该值的所有内容:

>>> import numpy as np
>>> a = np.arange(1,26)
>>> a.shape = (5,5)
>>> a
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20],
       [21, 22, 23, 24, 25]])

>>> a[1:4,1:4] = -12345
>>> a
array([[     1,      2,      3,      4,      5],
       [     6, -12345, -12345, -12345,     10],
       [    11, -12345, -12345, -12345,     15],
       [    16, -12345, -12345, -12345,     20],
       [    21,     22,     23,     24,     25]])
>>> a[a != -12345]
array([ 1,  2,  3,  4,  5,  6, 10, 11, 15, 16, 20, 21, 22, 23, 24, 25])

If you use a float array rather than an integer array, you can do it a little more elegantly by using NaN and isfinite:

如果使用float数组而不是整数数组,则可以使用NaN和isfinite更优雅地执行:

>>> a = np.arange(1,26).astype('float32')
>>> a.shape = (5,5)
>>> a[1:4,1:4] = np.nan
>>> a
array([[  1.,   2.,   3.,   4.,   5.],
       [  6.,  nan,  nan,  nan,  10.],
       [ 11.,  nan,  nan,  nan,  15.],
       [ 16.,  nan,  nan,  nan,  20.],
       [ 21.,  22.,  23.,  24.,  25.]], dtype=float32)
>>> a[np.isfinite(a)]
array([  1.,   2.,   3.,   4.,   5.,   6.,  10.,  11.,  15.,  16.,  20.,
    21.,  22.,  23.,  24.,  25.], dtype=float32)