# Return a new array of given shape filled with ones and also set the desired data-type in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To return a new array of given shape and type, filled with ones, use the ma.one() method in Python Numpy. The 1st parameter sets the shape of the array. The 2nd parameter is the desired data-type for the array.

The order parameter suggests whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.

A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

## Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Return a new array of given shape and type, filled with ones, using the ma.one() method in Python Numpy. The 2nd parameter is the desired data-type for the array −

arr = ma.ones((5,5), dtype=int)


Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)


Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)


Get the number of elements of the Array −

print("\nElements in the Array...\n",arr.size)

## Example

# Python ma.MaskedArray - Return a new array of given shape filled with ones and also set the desired data-type

import numpy as np
import numpy.ma as ma

# To return a new array of given shape and type, filled with ones, use the ma.one() method in Python Numpy
# The 1st parameter sets the shape of the array
# The 2nd parameter is the desired data-type for the array
arr = ma.ones((5,5), dtype=int)

# Displaying our array
print("Array...\n",arr)

# Get the datatype
print("\nArray datatype...\n",arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nElements in the Array...\n",arr.size)

## Output

Array...
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(5, 5)

Elements in the Array...
25