np.zeros用法举例
时间: 2023-11-08 10:52:42 浏览: 86
python中numpy.zeros(np.zeros)的使用方法
5星 · 资源好评率100%
np.zeros()函数的作用是创建一个指定形状和数据类型的全0数组。下面是np.zeros()函数的几个用法举例:
1. 创建一维数组:
```
import numpy as np
array_1 = np.zeros(5)
print(array_1)
```
输出:
```
[0. 0. 0. 0. 0.]
```
2. 创建多维数组:
```
import numpy as np
array_2 = np.zeros((5, 2))
print(array_2)
```
输出:
```
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
```
3. 创建int类型的数组:
```
import numpy as np
array_3 = np.zeros(5, dtype=int)
print(array_3)
```
输出:
```
[0 0 0 0 0]
```
4. 创建tuple(元组)类型的数组:
```
import numpy as np
array_4 = np.zeros((2, 3), dtype=tuple)
print(array_4)
```
输出:
```
[[() () ()]
[() () ()]]
```
阅读全文