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 in C or F style. The default is C style. 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.ones instead of np.ma.ones.

import numpy as np
myarray = np.ma.ones(5) #executing ma.ones() with value 5
print (myarray) #printing myarray

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We use the ma.ones() function to create an array with 5 ones. The data type of the array elements is integer. The output is stored in a variable myarray.
  • Line 4: We print the array myarray.

Free Resources