我正在使用numpy
数组存储某个地区的气候数据。该数组具有以下大小:
data.shape[0]=365, data.shape[1]=466 #y,x
我想说我想通过剪切data
的外框来创建一个新的数组,例如消除width=5
外框中的值。新数组将具有以下维度:
new.shape[0]=355, new.shape[1]=456 #y,x
我当前的数组格式为:
array([[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ],
[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ],
[ 0. , 0. , 0. , ..., 0. ,
0. , 0. ],
...,
[ 17.00830078, 0. , 0. , ..., 28.21435547,
28.28242111, 28.33056641],
[ 0. , 0. , 0. , ..., 28.25419998,
28.32392502, 28.34052658],
[ 0. , 0. , 0. , ..., 28.23759842,
28.31396484, 28.36874962]], dtype=float32)
如何在Python中实现?
答案 0 :(得分:3)
只需slice
-
c = 5 # No. of elems to be cropped on either sides
cropped_out = a[c:-c,c:-c]
示例运行 -
In [212]: a
Out[212]:
array([[2, 8, 4, 1, 4, 2, 0, 1, 6, 1],
[4, 0, 2, 8, 0, 4, 4, 2, 6, 5],
[5, 7, 6, 6, 6, 4, 6, 4, 1, 7],
[6, 8, 2, 4, 3, 0, 3, 0, 2, 2],
[6, 2, 5, 1, 1, 3, 7, 3, 3, 5],
[4, 8, 4, 5, 6, 8, 6, 1, 0, 7],
[7, 2, 8, 8, 6, 7, 3, 1, 7, 2]])
In [213]: c = 2 # No. of elems to be cropped on either sides
In [214]: a[c:-c,c:-c]
Out[214]:
array([[6, 6, 6, 4, 6, 4],
[2, 4, 3, 0, 3, 0],
[5, 1, 1, 3, 7, 3]])