Return a 1D version of self as a view with C-like order in Numpy

NumpyServer Side ProgrammingProgramming

To return a 1D version of self as a view, use the ma.MaskedArray.ravel() method in Numpy. The order is set using the "order" parameter, to read the elements using the index order. The‘C’ means to index the elements in C-like order, with the last axis index changing fastest, back to the first axis index changing slowest.

The elements of a are read using this index order. ‘C’ means to index the elements in C-like order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to index the elements in Fortran-like index order, with the first index changing fastest, and the last index changing slowest.

The ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of axis indexing. ‘A’ means to read the elements in Fortran-like index order if m is Fortran contiguous in memory, C-like order otherwise. ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.

Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array −

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

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

Get the shape of the Masked Array −

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

Get the number of elements of the Masked Array −

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

Return a 1D version of self as a view, use the ma.MaskedArray.ravel() method in Numpy; The order is set using the "order" parameter, to read the elements using the index order. The ‘C’ means to index the elements in C-like order, with the last axis index changing fastest, back to the first axis index changing slowest −

print("\nResult...\n",maskArr.ravel(order='C'))

Example

# Python ma.MaskedArray - Returns a 1D version of self as a view with C-like order

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[78, 85, 51], [56, 33, 97]])
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)

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

# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[0, 1, 0], [ 0, 0, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)

# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

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

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

# To return a 1D version of self as a view, use the ma.MaskedArray.ravel() method in Numpy
# The order is set using the "order" parameter, to read the elements using the index order.
# The ‘C’ means to index the elements in C-like order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘
print("\nResult...\n",maskArr.ravel(order='C'))

Output

Array...
[[78 85 51]
[56 33 97]]

Array type...
int64

Array Dimensions...
2

Our Masked Array
[[78 -- 51]
[56 33 97]]

Our Masked Array type...
int64

Our Masked Array Dimensions...
2

Our Masked Array Shape...
(2, 3)

Elements in the Masked Array...
6

Result...
[78 -- 51 56 33 97]
raja
Updated on 02-Feb-2022 08:50:58

Advertisements