numpy 构建一个全为零的数组 zeros()方法
·
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')])
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)