What is the numpy.ones() function in Python?
Overview
The numpy.ones() function in Python is used to return an array filled with ones (1).
Syntax
numpy.ones(shape, dtype=None, order='C', *, like=None)
Parameters
The numpy.ones() method takes the following parameters:
shape: This represents the shape of the new array.dtype: This represents the data type of array you want. This is optional.order: This represents whether to store multi-dimensional data inCorFstyle. The default isCstyle. This is optional.like: This represents thearray_likeobject or prototype.
Return value
The numpy.ones() function returns an array with the given shape, dtype, and filled with ones.
Example
import numpy as np# an aray with 5 ones and integer data typemyarray = np.ones((5), dtype = int)print(myarray)
Explanation
- Line 1: We import the
numpymodel. - Line 4: We use the
numpy.ones()function to create an array with5ones. The data type of the array elements is integer. The output is stored in a variablemyarray. - Line 6: We print the array
myarray.