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


Python numpy.ones()用法及代碼示例

numpy.ones(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 ones having given shape, order and datatype.
# Python Program illustrating 
# numpy.ones method 
  
import numpy as geek 
  
b = geek.ones(2, dtype = int) 
print("Matrix b : \n", b) 
  
a = geek.ones([2, 2], dtype = int) 
print("\nMatrix a : \n", a) 
  
c = geek.ones([3, 3]) 
print("\nMatrix c : \n", c)

輸出:


Matrix b : 
 [1 1]

Matrix a : 
 [[1 1]
 [1 1]]

Matrix c : 
 [[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]

參考:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.ones.html
注意:與零和空不同,數字1不會分別將數組值設置為零或隨機值。此外,這些代碼也無法在online-ID上運行。請在您的係統上運行它們以探索其工作原理。



相關用法


注:本文由純淨天空篩選整理自 numpy.ones() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。