Python numpy.zeros() function returns a new array of given shape and type, where the element’s value as 0.
Python numpy.zeros()函数返回给定形状和类型的新数组,其中元素的值为0。
numpy.zeros()函数参数 (numpy.zeros() function arguments)
The numpy.zeros() function syntax is:
numpy.zeros()函数的语法为:
zeros(shape, dtype=None, order='C')
- he shape is an int or tuple of ints to define the size of the array. 形状是一个int或一个int元组,用于定义数组的大小。
- The dtype is an optional parameter with default value as float. It’s used to specify the data type of the array, for example, int. dtype是一个可选参数,默认值为float。 它用于指定数组的数据类型,例如int。
Output:
输出:
[0. 0. 0.]
Notice that the elements are having the default data type as the float. That’s why the zeros are 0.
请注意,元素的默认数据类型为float。 这就是零为0的原因。
2.创建多维数组 (2. Creating Multi-dimensional array)
import numpy as np
array_2d = np.zeros((2, 3))
print(array_2d)
Output:
输出:
[[0. 0. 0.]
[0. 0. 0.]]
3.具有int数据类型的NumPy zeros数组 (3. NumPy zeros array with int data type)
import numpy as np
array_2d_int = np.zeros((2, 3), dtype=int)
print(array_2d_int)
Output:
输出:
[[0 0 0]
[0 0 0]]
4.具有元组数据类型和零的NumPy数组 (4. NumPy Array with Tuple Data Type and Zeroes)
We can specify the array elements as a tuple and specify their data types too.
我们可以将数组元素指定为元组 ,也可以指定其数据类型。
import numpy as np
array_mix_type = np.zeros((2, 2), dtype=[('x', 'int'), ('y', 'float')])
print(array_mix_type)
print(array_mix_type.dtype)
Output:
输出:
[[(0, 0.) (0, 0.)]
[(0, 0.) (0, 0.)]]
[('x', '<i8'), ('y', '<f8')]