首页 > 解决方案 > 输出矩阵用点更新,我想删除数组中的点

问题描述

 import numpy as np
 x = np.ones((5,5))
 print(x)
 x[1:-1,1:-1] = 0
 print(x)

我得到如下所示的输出:

[[1. 1. 1. 1. 1.]
 [1. 0. 0. 0. 1.]
 [1. 0. 0. 0. 1.]
 [1. 0. 0. 0. 1.]
 [1. 1. 1. 1. 1.]]

标签: pythonnumpy

解决方案


您可以使用astype,将其设置为int

print(x.astype(int))

结果:

[[1 1 1 1 1]
 [1 0 0 0 1]
 [1 0 0 0 1]
 [1 0 0 0 1]
 [1 1 1 1 1]]

推荐阅读