Python 之 numpy.zeros() 的使用方法

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')]

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值