Return a new array of given shape and type without initializing entries in Numpy

NumpyServer Side ProgrammingProgramming

To return a new array of given shape and type, without initializing entries, use the ma.empty() method in Python Numpy. The 1st parameter sets the shape of the empty array. The dtype parameter sets the desired output data-type for the array. The method returns an array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None.

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, without initializing entries using the ma.empty() method in Python Numpy −

arr = ma.empty((5, 5),dtype = np.int32)

Displaying our array −

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

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 and type without initializing entries

import numpy as np
import numpy.ma as ma

# To return a new array of given shape and type, without initializing entries, use the ma.empty() method in Python Numpy
# The 1st parameter sets the shape of the empty array
# The dtype parameter sets the desired output data-type for the array
arr = ma.empty((5, 5),dtype = np.int32)

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

# 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...
[[ 0 0 0 0 117]
[ 99 104 32 102 105]
[108 101 32 111 114]
[ 32 100 105 114 101]
[ 99 116 111 114 121]]

Array Dimensions...
2

Our Array Shape...
(5, 5)

Elements in the Array...
25
raja
Updated on 03-Feb-2022 11:06:50

Advertisements