Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
A masked array in NumPy is an array that has an associated boolean mask to indicate missing or invalid data. The numpy.ma
module provides a MaskedArray
class which is a subclass of numpy.ndarray
, and it incorporates all standard array methods and attributes, plus some extras to handle the associated mask.
Let's dive into reshaping a masked array without altering its data.
When working with datasets that have missing or invalid data, you might use NumPy's masked array functionality. Reshaping these arrays is just as straightforward as reshaping regular arrays.
To work with masked arrays, you'll need the numpy.ma
module:
import numpy as np import numpy.ma as ma
Let's create a masked array where values that are -1
are considered invalid:
data = np.array([1, 2, -1, 3, 4, -1, 5, 6, 7]) masked_data = ma.masked_equal(data, -1) print(masked_data)
Output:
[1 2 -- 3 4 -- 5 6 7]
The --
denotes the masked (or invalid) entries in the masked array.
Just like with numpy.ndarray
, you can use the reshape
method to give a new shape to the masked array:
reshaped_masked_data = masked_data.reshape(3, 3) print(reshaped_masked_data)
Output:
[[1 2 --] [3 4 --] [5 6 7]]
Notice that the mask is also reshaped correctly alongside the data.
To ensure that reshaping the masked array hasn't altered its underlying data, you can check the original data before and after reshaping:
print(masked_data.data) # [ 1 2 -1 3 4 -1 5 6 7] print(reshaped_masked_data.data) # [[ 1 2 -1] [ 3 4 -1] [ 5 6 7]]
Both representations show the same data, and only the shape has changed.
Reshaping a masked array in NumPy is similar to reshaping a regular ndarray. The associated mask is also reshaped appropriately, ensuring that the mask remains aligned with the corresponding data. This capability makes it easy to manipulate datasets with missing or invalid entries without losing the context of where those entries are.
import numpy as np # Create a masked array data = np.array([1, 2, -1, 4, -5]) mask = np.array([False, False, True, False, True]) masked_array = np.ma.masked_array(data, mask) # Reshape the masked array reshaped_array = masked_array.reshape((2, 2)) print("Original masked array:") print(masked_array) print("\nReshaped array:") print(reshaped_array)
# Assuming 'masked_array' is already defined # Change shape without altering data reshaped_array = np.ma.reshape(masked_array, (2, 2)) print("Original masked array:") print(masked_array) print("\nReshaped array without altering data:") print(reshaped_array)
# Assuming 'masked_array' is already defined # Reshape without modifying data reshaped_array = np.ma.reshape(masked_array, (2, -1)) print("Original masked array:") print(masked_array) print("\nReshaped array without modifying data:") print(reshaped_array)
# Assuming 'masked_array' is already defined # Manipulate shape manipulated_array = np.ma.resize(masked_array, (3, 2)) print("Original masked array:") print(masked_array) print("\nManipulated array:") print(manipulated_array)
# Assuming 'masked_array' is already defined # Adjust dimensions adjusted_array = np.ma.atleast_2d(masked_array) print("Original masked array:") print(masked_array) print("\nAdjusted array dimensions:") print(adjusted_array)
# Assuming 'masked_array' is already defined # Flatten the masked array flattened_array = masked_array.flatten() print("Original masked array:") print(masked_array) print("\nFlattened array:") print(flattened_array)
# Assuming 'masked_array' is already defined # Reshape without changing values reshaped_array = np.ma.reshape(masked_array, (2, -1), order='F') print("Original masked array:") print(masked_array) print("\nReshaped array without changing values:") print(reshaped_array)
# Assuming 'masked_array' is already defined # Adjust shape adjusted_array = np.ma.array(masked_array, shape=(2, 3)) print("Original masked array:") print(masked_array) print("\nAdjusted array shape:") print(adjusted_array)
# Assuming 'masked_array' is already defined # Ravel the masked array raveled_array = np.ma.ravel(masked_array) print("Original masked array:") print(masked_array) print("\nRaveled array:") print(raveled_array)