- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Return a 1D version of self as a view with elements in the order they occur in memory 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 ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative.

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 −

print("

Result...

",maskArr.ravel(order='K'))

## Example

# Python ma.MaskedArray - Returns a 1D version of self as a view with elements in the order they occur in memory 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 ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative print("

Result...

",maskArr.ravel(order='K'))

## Output

Array... [[78 85 51] [56 33 97]] Array type... int32 Array Dimensions... 2 Our Masked Array [[78 -- 51] [56 33 97]] Our Masked Array type... int32 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 3) Elements in the Masked Array... 6 Result... [78 -- 51 56 33 97]

- Related Articles
- Return a 1D version of self as a view with C-like order in Numpy
- Return a 1D version of self as a view in Numpy
- Return a 1D version of self as a view with Fortran-like index order in Numpy
- Return a copy of the masked array collapsed into one dimension in the order the elements occur in memory in Numpy
- Return the underlying data as a view of the masked array in Numpy
- Return a view of the MaskedArray data in Numpy
- Return a view of the masked array with axes transposed in NumPy
- Return a copy of self, with masked values filled with a given value in Numpy
- Return a view of the masked array with axis1 and axis2 interchanged in Numpy
- Return a view of the masked array with axes transposed along given axis in NumPy
- Return a new array with the same shape and type as a given array and change the order in Numpy
- Return a masked array containing the same data but with a new shape viewed as row-major order in Numpy
- Return a masked array containing the same data but with a new shape viewed as column-major order in Numpy
- Python Pandas - Return a numpy timedelta64 array scalar view in nanoseconds
- Copy and return all the elements of a masked array in Numpy