

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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...\n", arr) print("\nArray type...\n", arr.dtype) print("\nArray itemsize...\n", arr.itemsize)
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], [ 0, 1]]) print("\nOur Masked Array\n", maskArr) print("\nOur Masked Array type...\n", maskArr.dtype)
Get the itemsize of the Masked Array −
print("\nOur Masked Array itemsize...\n", maskArr.itemsize)
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)
Get the Tuple of bytes to step in each dimension when traversing an array, use the ma.MaskedArray.strides attribute in Numpy
print("\nStrides...\n",maskArr.strides)
Example
import numpy as np import numpy.ma as ma arr = np.array([[35, 85], [67, 33]]) print("Array...\n", arr) print("\nArray type...\n", arr.dtype) print("\nArray itemsize...\n", arr.itemsize) # 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], [ 0, 1]]) print("\nOur Masked Array\n", maskArr) print("\nOur Masked Array type...\n", maskArr.dtype) # Get the itemsize of the Masked Array print("\nOur Masked Array itemsize...\n", maskArr.itemsize) # 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 get the Tuple of bytes to step in each dimension when traversing an array, use the ma.MaskedArray.strides attribute in Numpy print("\nStrides...\n",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)
- Related Questions & Answers
- Get the total bytes consumed by the elements in Numpy
- Swap the bytes of the masked array data in Numpy
- Compute the Heaviside step function in Numpy
- Swap the bytes of the masked array data inplace in Numpy
- Get the number of bytes in a file in C#
- Python Pandas - Get a tuple with the length of each level from MultiIndex
- Return a copy of the masked array collapsed into one dimension in Numpy
- How to get the HashCode of the tuple in C#?
- Reduce array's dimension by one in Numpy
- Convert inputs to arrays with at least one dimension in Numpy
- Get the size of the LabelValue Tuple in Java
- Reduce array's dimension by adding all the elements in Numpy
- Reduce array’s dimension by multiplying all the elements in Numpy
- How to get Sixth Element of the Tuple in C#?
- How to get Third Element of the Tuple in C#?