numpy.ones(shape, dtype=None, order='C')
Return a new array of given shape and type,filled with ones.
Parameters:
shape : int or sequence of ints
Shape of the new array, e.g., (2, 3) or 2.
dtype : data-type, optional
The desired data-type for the array, e.g.,numpy.int8. Default is numpy.float64.
order : {‘C’, ‘F’}, optional
Whether to store multidimensional data inC- or Fortran-contiguous (row- or column-wise) order in memory.
Returns:
out : ndarray
Array of ones with the given shape, dtype,and order.
Example
strong@foreverstrong:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.ones(5)
>>> a
array([ 1., 1., 1., 1., 1.])
>>> a.shape
(5,)
>>> a.dtype
dtype('float64')
>>> a.size
5
>>>
>>> b = np.ones((5,), dtype=np.int)
>>> b
array([1, 1, 1, 1, 1])
>>> b.shape
(5,)
>>> b.dtype
dtype('int64')
>>> b.size
5
>>>
>>> c = np.ones((2, 1))
>>> c
array([[ 1.],
[ 1.]])
>>> c.shape
(2, 1)
>>> c.dtype
dtype('float64')
>>> c.size
2
>>>
>>> s = (3, 4)
>>> d = np.ones(s)
>>> d
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
>>> d.shape
(3, 4)
>>> d.dtype
dtype('float64')
>>> d.size
12
>>> e = np.ones((4, 5), dtype=np.float)
>>> e
array([[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.]])
>>> e.shape
(4, 5)
>>> e.dtype
dtype('float64')
>>> e.size
20
>>>
>>> exit()
strong@foreverstrong:~$