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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。