numpy.ones使用方法

https://docs.scipy.org/doc/numpy-1.16.1/reference/generated/numpy.ones.html?highlight=ones

numpy.ones(shape, dtype=None, order=’C’)

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

例子

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