将居中切片之外的数组值清零

时间:2016-03-07 23:00:13

标签: python arrays numpy data-structures

我有一个2D阵列(抱歉这个烂摊子):

[[4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]
 [5.91745 13.7384 35.5878 79.1378 94.6858 60.1723 27.6673 12.7537 5.91745]
 [11.1614 29.0228 108.87 175.518 155.52 138.734 96.2145 29.5323 11.1614]
 [14.7855 59.3563 156.22 410.418 669.983 400.613 163.023 58.3647 14.7855]
 [9.87354 87.0854 124.38 775.761 1630.11 744.488 115.188 90.8995 9.87354]
 [8.47966 64.2404 145.325 475.687 931.189 519.85 122.555 69.1578 8.47966]
 [10.0377 27.1189 106.654 112.141 132.298 233.69 136.029 30.2086 10.0377]
 [6.50091 14.2923 24.318 73.8886 128.941 106.06 48.8712 15.8181 6.50091]
 [4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]]

我知道如何获取一个数组的切片,并将该切片的值清零以获得如下所示的内容(我将3 x 3切片归零):

[[4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]
 [5.91745 13.7384 35.5878 79.1378 94.6858 60.1723 27.6673 12.7537 5.91745]
 [11.1614 29.0228 108.87 175.518 155.52 138.734 96.2145 29.5323 11.1614]
 [14.7855 59.3563 156.22 0.0 0.0 0.0 163.023 58.3647 14.7855]
 [9.87354 87.0854 124.38 0.0 0.0 0.0 115.188 90.8995 9.87354]
 [8.47966 64.2404 145.325 0.0 0.0 0.0 122.555 69.1578 8.47966]
 [10.0377 27.1189 106.654 112.141 132.298 233.69 136.029 30.2086 10.0377]
 [6.50091 14.2923 24.318 73.8886 128.941 106.06 48.8712 15.8181 6.50091]
 [4.8718 7.72844 12.4288 11.7124 7.55014 10.2837 8.50997 3.69767 4.8718]]

但是,除了原始中心切片(如下所示)之外,我如何将数组中的所有值归零?

[[0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 410.418 669.983 400.613 0.0 0.0 0.0]
 [0.0 0.0 0.0 775.761 1630.11 744.488 0.0 0.0 0.0]
 [0.0 0.0 0.0 475.687 931.189 519.85 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]]

1 个答案:

答案 0 :(得分:3)

查看numpy indexing页面:

您可以使用布尔掩码(由x!= 0创建)索引到x的副本,将所有适当的值设置为0.以下应该有效:

In [4]: x = np.random.random((5,5))


In [11]: y = x.copy()
In [14]: y[1:3, 1:3] = 0

array([[ 0.84905824,  0.079079  ,  0.71624639,  0.02272327,  0.00628426],
       [ 0.29032824,  0.        ,  0.        ,  0.98850971,  0.52504821],
       [ 0.45770781,  0.        ,  0.        ,  0.41691402,  0.11052185],
       [ 0.06152469,  0.51321134,  0.6347654 ,  0.82463901,  0.78756266],
       [ 0.96716897,  0.79592572,  0.12402872,  0.25271513,  0.2999993 ]])

In [18]: z = x.copy()

In [19]: z[y!=0] = 0

array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.8722439 ,  0.50425235,  0.        ,  0.        ],
       [ 0.        ,  0.05976207,  0.66207464,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])

正如@StefanPochmann指出的那样,一旦你得到带有零的y矩阵。将z计算为:

非常容易
z = x - y
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.8722439 ,  0.50425235,  0.        ,  0.        ],
       [ 0.        ,  0.05976207,  0.66207464,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])

不需要花哨的索引。