python中numpy.zeros()函数

1.概述

numpy 中一个用于创建 0 数组的函数。

2. zeros()

2.1 语法
numpy.zeros(shape, dtype=float, order=‘C’)
2.2 参数
shape — 整型或者整数序列如 (2, 3) ,指定新数组的大小,创建多维数组;
dtype — 数据类型,可选参数,默认float,当数据类型要为整型时dtype=int;
order —是否在内存中以 C- 或 Fortran-contiguous(行或列)顺序存储多维数据。可选参数
2.3 返回值
返回来一个给定形状和类型的用0填充的数组;
2.4示例
创建一个一维数组,指定shape为一个整数,代表数组长度:

import numpy as np
a = np.zeros(4)
print(a)

结果:在默认的情况下,zeros创建的数组元素类型是浮点型的

[0. 0. 0. 0.]

指定dtype整型:

import numpy as np
a = np.zeros(4, dtype=int)
print(a)

结果:

[0 0 0 0]

创建多维数组:

import numpy as np
a = np.zeros((3, 4))
print(a)

结果:

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

猜你喜欢

转载自blog.csdn.net/qq_36455412/article/details/117731719
今日推荐