- 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

# Count the number of masked elements along axis 1 in Numpy

To count the number of masked elements along axis 1, use the **ma.MaskedArray.count_masked()** method. The axis is set using the "axis" parameter. The method returns the total number of masked elements (axis=None) or the number of masked elements along each slice of the given axis.

The axis parameter is the axis along which to count. If None (default), a flattened version of the array is used.

## Steps

At first, import the required library −

import numpy as np import numpy.ma as ma

Creating a 4x4 array with int elements using the numpy.arange() method −

arr = np.arange(16).reshape((4,4)) print("Array...\n", arr) print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim) print("\nOur Array type...\n", arr.dtype)

Get the shape of the Array −

print("\nOur Masked Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Masked Array...\n",arr.size)

Create a masked array −

arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked

To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked() method. The axis is set using the "axis" parameter −

print("\nResult (number of masked elements)...\n",ma.count_masked(arr, axis = 1))

## Example

import numpy as np import numpy.ma as ma # Creating a 4x4 array with int elements using the numpy.arange() method arr = np.arange(16).reshape((4,4)) print("Array...\n", arr) print("\nArray type...\n", arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) print("\nOur Array type...\n", arr.dtype) # Get the shape of the Array print("\nOur Masked Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Masked Array...\n",arr.size) # Create a masked array arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked # To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked() method # The axis is set using the "axis" parameter print("\nResult (number of masked elements)...\n",ma.count_masked(arr, axis = 1))

## Output

Array... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Array type... int64 Array Dimensions... 2 Our Array type... int64 Our Masked Array Shape... (4, 4) Elements in the Masked Array... 16 Result (number of masked elements)... [1 1 2 3]

- Related Questions & Answers
- Count the non-masked elements of the masked array along axis 1 in Numpy
- Count the number of masked elements along axis 0 to count in Numpy
- Count the number of masked elements along specific axis
- Repeat elements of a masked array along axis 1 in NumPy
- Count the non-masked elements of the masked array along axis 0 in Numpy
- Count the non-masked elements of the masked array along the given axis in Numpy
- Append masked arrays along axis 1 in Numpy
- Sort the masked array in-place along axis 1 in NumPy
- Join a sequence of masked arrays along axis 1 in Numpy
- Count the number of masked elements in Numpy
- Repeat elements of a masked array along given axis in NumPy
- Repeat elements of a masked array along axis 0 in NumPy
- Return the average of the masked array elements along specific axis in Numpy
- Compute the median of the masked array elements along specified axis in Numpy
- Compute the median of the masked array elements along axis 0 in Numpy