Get the Tuple of bytes to step in each dimension when traversing in Numpy


To get the Tuple of bytes to step in each dimension when traversing an array, use the ma.MaskedArray.strides attribute in Numpy. The byte offset of element (i[0], i[1], ..., i[n]) in an array a is −

offset = sum(np.array(i) * a.strides)

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

Create an array using the numpy.array() method −

arr = np.array([[35, 85], [67, 33]])
print("Array...
", arr) print("
Array type...
", arr.dtype) print("
Array itemsize...
", arr.itemsize)

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

Get the itemsize of the Masked Array −

print("
Our Masked Array itemsize...
", maskArr.itemsize)

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)

Get the Tuple of bytes to step in each dimension when traversing an array, use the ma.MaskedArray.strides attribute in Numpy

print("
Strides...
",maskArr.strides)

Example

import numpy as np
import numpy.ma as ma

arr = np.array([[35, 85], [67, 33]])
print("Array...
", arr) print("
Array type...
", arr.dtype) print("
Array itemsize...
", arr.itemsize) # 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], [ 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the itemsize of the Masked Array print("
Our Masked Array itemsize...
", maskArr.itemsize) # 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 get the Tuple of bytes to step in each dimension when traversing an array, use the ma.MaskedArray.strides attribute in Numpy print("
Strides...
",maskArr.strides)

Output

Array...
[[35 85]
[67 33]]

Array type...
int64

Array itemsize...
8
Array Dimensions...
2

Our Masked Array
[[35 85]
[67 --]]

Our Masked Array type...
int64

Our Masked Array itemsize...
8

Our Masked Array Dimensions...
2

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

Elements in the Masked Array...
4

Strides...
(16, 8)

Updated on: 22-Feb-2022

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements