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


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...
", arr) print("
Array type...
", arr.dtype)

Get the dimensions of the Array −

print("Array Dimensions...
",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("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)

Get the dimensions of the Masked Array −

print("
Our Masked Array Dimensions...
",maskArr.ndim)

Get the shape of the Masked Array −

print("
Our Masked Array Shape...
",maskArr.shape)

Get the number of elements of the Masked Array −

print("
Elements in the Masked Array...
",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("
Result...
",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...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",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("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Elements in the Masked Array...
",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("
Result...
",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]

Updated on: 02-Feb-2022

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements