What is the numpy.ma.ones() function in NumPy?
Overview
The ma.ones() function in NumPy is used to return a new array with the same shape and type as the input array, but filled with ones.
Syntax
The ma.ones() function takes the syntax below:
ma.ones(shape, dtype=None, order='C')
Syntax for the ma.ones() funciton in NumPy
Parameters
The ma.ones() function takes the following parameter values:
shape: This represents the shape of the new array.dtype: This represents the data type of array you want. This is an optional parameter.order: This represents whether to store multi-dimensional data inCorFstyle. The default isCstyle. This is an optional parameters.
Return value
The ma.ones() function returns an array with the same shape , dtype, and filled with ones.
Example
Let's view the code example.
Note: In the code below, we can also write
np.onesinstead ofnp.ma.ones.
import numpy as npmyarray = np.ma.ones(5) #executing ma.ones() with value 5print (myarray) #printing myarray
Explanation
- Line 1: We import the
numpymodule. - Line 3: We use the
ma.ones()function to create an array with5ones. The data type of the array elements is integer. The output is stored in a variablemyarray. - Line 4: We print the array
myarray.