numpy.ones(shape, dtype=None, order=‘C’, *, like=None)
Return a new array of given shape and type, filled with ones.
参数:
shape:int or sequence of ints
dtype:data-type, optional
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
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.]])
>>> a = np.arange(1,9).reshape(2,4)
>>> np.ones_like(a)
array([[1, 1, 1, 1],
[1, 1, 1, 1]])
>>> np.empty((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
np.full((2,4),4)
array([[4, 4, 4, 4],
[4, 4, 4, 4]])