python构建指定长度全为零的数组

numpy.zeros( )

语法:

numpy.zeros(shape, dtype=float, order='C')

返回一个指定类型和格式的数组的全为0的数组

示例:

  • 指定长度的一维数组
>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])
  • 指定数据类型,指定长度的一维数组
>>> np.zeros((5), dtype=np.int)
array([0, 0, 0, 0, 0])
  • 二维数组
>>> np.zeros((2, 1))
array([[ 0.],
       [ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0.,  0.],
       [ 0.,  0.]])
  • 指定dtype
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
      dtype=[('x', '<i4'), ('y', '<i4')])
Logo

学大模型,用大模型上飞桨星河社区!每天8点V100G算力免费领!免费领取ERNIE 4.0 100w Token >>>

更多推荐