numpy 构建一个全为零的数组 zeros()方法

来源:互联网 发布:法国历史 书籍 知乎 编辑:程序博客网 时间:2024/06/05 00:12

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 dtypearray([(0, 0), (0, 0)],      dtype=[('x', '<i4'), ('y', '<i4')])
原创粉丝点击