当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.zeros()用法及代码示例


numpy.zeros(shape,dtype = None,order ='C'):返回具有给定形状和类型(带有零)的新数组。
参数:

shape : integer or sequence of integers
order  : C_contiguous or F_contiguous
         C-contiguous order in memory(last index varies the fastest)
         C order means that operating row-rise on the array will be slightly quicker
         FORTRAN-contiguous order in memory (first index varies the fastest).
         F order means that column-wise operations will be faster. 
dtype : [optional, float(byDeafult)] Data type of returned array.  

返回值:

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

参考:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.zeros.html#numpy.zeros
注意:与零和空不同,零不会将数组值分别设置为零或随机值。此外,这些代码也无法在online-ID上运行。请在您的系统上运行它们以探索其工作原理。



相关用法


注:本文由纯净天空筛选整理自 numpy.zeros() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。