Stack masked arrays in sequence depth wise (along third axis) in Numpy


To stack masked arrays in sequence depth wise (along third axis), use the ma.dstack() method in Python Numpy. This is equivalent to concatenation along the third axis after 2-D arrays of shape (M,N) have been reshaped to (M,N,1) and 1-D arrays of shape (N,) have been reshaped to (1,N,1). Rebuilds arrays divided by dsplit.

This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions concatenate, stack and block provide more general stacking and concatenation operations.

The parameters are the arrays that must have the same shape along all but the third axis. 1-D or 2-D arrays must have the same shape. The function returns the array formed by stacking the given arrays, will be at least 3-D.

Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create Array 1, a 3x3 array with int elements using the numpy.arange() method −

arr1 = np.arange(9).reshape((3,3))
print("Array1...
", arr1) print("
Array type...
", arr1.dtype)

Create a masked array 1 −

arr1 = ma.array(arr1)

Mask Array1 −

arr1[0, 1] = ma.masked
arr1[1, 1] = ma.masked

Display Masked Array 1 −

print("
Masked Array1...
",arr1)

Create Array 2, another 3x3 array with int elements using the numpy.arange() method −

arr2 = np.arange(9).reshape((3,3))
print("
Array2...
", arr2) print("
Array type...
", arr2.dtype)

Create masked array 2 −

arr2 = ma.array(arr2)

Mask Array2 −

arr2[2, 1] = ma.masked
arr2[2, 2] = ma.masked

Display Masked Array 2 −

print("
Masked Array2...
",arr2)

To stack masked arrays in sequence depth wise (along third axis), use the ma.dstack() method −

print("
Result of stacking arrays depth wise...
",ma.dstack((arr1, arr2)))

Example

# Python ma.MaskedArray - Stack masked arrays in sequence depth wise (along third axis)

import numpy as np
import numpy.ma as ma

# Array 1
# Creating a 3x3 array with int elements using the numpy.arange() method
arr1 = np.arange(9).reshape((3,3))
print("Array1...
", arr1) print("
Array type...
", arr1.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr1.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr1.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr1.size) # Create a masked array arr1 = ma.array(arr1) # Mask Array1 arr1[0, 1] = ma.masked arr1[1, 1] = ma.masked # Display Masked Array 1 print("
Masked Array1...
",arr1) # Array 2 # Creating another 3x3 array with int elements using the numpy.arange() method arr2 = np.arange(9).reshape((3,3)) print("
Array2...
", arr2) print("
Array type...
", arr2.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr2.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr2.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr2.size) # Create a masked array arr2 = ma.array(arr2) # Mask Array2 arr2[2, 1] = ma.masked arr2[2, 2] = ma.masked # Display Masked Array 2 print("
Masked Array2...
",arr2) # To stack masked arrays in sequence depth wise (along third axis), use the ma.dstack() method in Python Numpy print("
Result of stacking arrays depth wise...
",ma.dstack((arr1, arr2)))

Output

Array1...
[[0 1 2]
[3 4 5]
[6 7 8]]

Array type...
int64

Array Dimensions...
2

Our Array Shape...
(3, 3)

Elements in the Array...
9

Masked Array1...
[[0 -- 2]
[3 -- 5]
[6 7 8]]

Array2...
[[0 1 2]
[3 4 5]
[6 7 8]]

Array type...
int64

Array Dimensions...
2

Our Array Shape...
(3, 3)

Elements in the Array...
9

Masked Array2...
[[0 1 2]
[3 4 5]
[6 -- --]]

Result of stacking arrays depth wise...
[[[0 0]
[-- 1]
[2 2]]
[[3 3]
[-- 4]
[5 5]]
[[6 6]
[7 --]
[8 --]]]

Updated on: 03-Feb-2022

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements