numpy ones 用法及代码示例

用法:
numpy.ones(shape, dtype=None, order='C')
返回给定形状和类型的新数组,并填充为1。

描述
参数 shape: : int 或 sequence of ints

新阵列的形状,例如(2, 3)或者2。
dtype: : data-type, 可选参数

数组所需的数据类型,例如numpy.int8。默认为numpy.float64。
order: : {‘C’, ‘F’}, optional, default:C

是否以行优先(C-style)或列优先(Fortran-style)的顺序将多维数据存储在内存中。

返回值 out: : ndarray

具有给定形状,dtype和顺序的数组。

例子:

>>> np.ones(5)
array([1., 1., 1., 1., 1.])
>>> np.ones((5,), dtype=int)
array([1, 1, 1, 1, 1])
>>> np.ones((2, 1))
array([[1.],
       [1.]])
>>> s = (2,2)
>>> np.ones(s)
array([[1.,  1.],
       [1.,  1.]])

发表回复

登录... 后才能评论