通过整数值对2D NumPy数组的边界进行切片 [英] Slice border of 2D NumPy array by integer value

查看:85
本文介绍了通过整数值对2D NumPy数组的边界进行切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用整数值对2D NumPy数组进行切片,但是我找不到合适的方法。我需要将矩阵的边界切成一定数量的行/列。

I would like to slice a 2D NumPy array by an integer value, but I cannot find a way to do this properly. I need to slice the "border" of the matrix by a certain number of rows/columns.

说数组是:

a = np.reshape(np.arange(25),(5,5))
print a

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]

slice_val = 1

b = a[:-slice_val,:-slice_val]
print "\n", b

我得到的是:

[[ 0  1  2  3]
 [ 5  6  7  8]
 [10 11 12 13]
 [15 16 17 18]]

,但我想要这样的东西:

, but I want something like this:

[[6  7  8 ]
 [11 12 13]
 [16 17 18]]


推荐答案

使用

b = a [slice_val:-slice_val,slice_val:-slice_val]

通过slice_val分割边界。

to slice the borders by slice_val.

这篇关于通过整数值对2D NumPy数组的边界进行切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆