zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python中ones的含义和用法

Python 用法 含义
2023-09-11 14:17:11 时间

  ones是numpy的一个内置函数,作用是生成参数为一的数组。英文解释:

Return a new array of given shape and type, filled with ones.

例子:

>>> np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])
>>> np.ones((5,), dtype=np.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.]])