numpy.zeros()
Python numpy.zeros()函数的作用是:返回一个给定形状和类型的新数组,其中包含0。
语法:
numpy.zeros(shape, dtype = None, order = 'C')
参数:
shape :整数或整数的序列
order : C_contiguous或F_contiguous
C-连续顺序在内存中(最后一个索引的变化速度最快)
C顺序意味着在数组上操作行升会稍微快一些
FORTRAN-在内存中的连续顺序(第一个索引变化最快)。
F顺序意味着对列的操作会更快。
dtype : [optional, float(byDeafult)] 返回阵列的数据类型。
返回值:
ndarray of zeros having given shape, order and datatype.
示例1
# Python Program illustrating
# numpy.zeros method
import numpy as geek
b = geek.zeros(2, dtype = int)
print("Matrix b : \n", b)
a = geek.zeros([2, 2], dtype = int)
print("\nMatrix a : \n", a)
c = geek.zeros([3, 3])
print("\nMatrix c : \n", c)
输出:
Matrix b :
[0 0]
Matrix a :
[[0 0]
[0 0]]
Matrix c :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
示例2
操作数据类型
# Python Program illustrating
# numpy.zeros method
import numpy as geek
# manipulation with data-types
b = geek.zeros((2,), dtype=[('x', 'float'), ('y', 'int')])
print(b)
输出:
[(0.0, 0) (0.0, 0)]