當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。