- 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

# Calculate the nthdiscrete difference along axis 0 in Python ma.MaskedArray

To calculate the n-th discrete difference along the given axis, use the **MaskedArray.diff()** method in Python Numpy. The first difference is given by **out[i] = a[i+1] - a[i]** along the given axis, higher differences are calculated by using diff recursively −

The axis is set using the "

**axis**" parameterThe axis is the axis along which the difference is taken, default is the last axis.

The function returns the n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases. A notable exception is datetime64, which results in a timedelta64 output array.

The prepend, append parameter are the values to prepend or append to a along axis prior to performing the difference. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must match a except along axis.

## Steps

At first, import the required library −

import numpy as np import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[65, 68, 81], [93, 33, 76], [73, 88, 51], [62, 45, 67]]) print("Array...\n", arr)

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[1, 0, 0], [ 0, 0, 0], [0, 1, 0], [0, 0, 0]]) print("\nOur Masked Array...\n", maskArr)

Get the type of the masked array −

print("\nOur Masked Array type...\n", maskArr.dtype)

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("\nNumber of elements in the Masked Array...\n",maskArr.size)

To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method in Python. The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively. The axis is set using the "axis" parameter. The axis is the axis along which the difference is taken, default is the last axis −

print("\nResult..\n.", np.diff(maskArr, axis = 0))

## Example

import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([[65, 68, 81], [93, 33, 76], [73, 88, 51], [62, 45, 67]]) print("Array...\n", arr) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[1, 0, 0], [ 0, 0, 0], [0, 1, 0], [0, 0, 0]]) print("\nOur Masked Array...\n", maskArr) # Get the type of the masked array print("\nOur Masked Array type...\n", maskArr.dtype) # 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("\nNumber of elements in the Masked Array...\n",maskArr.size) # To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method in Python Numpy # The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively. # The axis is set using the "axis" parameter # The axis is the axis along which the difference is taken, default is the last axis. print("\nResult..\n.", np.diff(maskArr, axis = 0))

## Output

Array... [[65 68 81] [93 33 76] [73 88 51] [62 45 67]] Our Masked Array... [[-- 68 81] [93 33 76] [73 -- 51] [62 45 67]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (4, 3) Number of elements in the Masked Array... 12 Result.. . [[-- -35 -5] [-20 -- -25] [-11 -- 16]]

- Related Questions & Answers
- Calculate the n-th discrete difference over axis 0 in Python
- Calculate the n-th discrete difference along axis 1 in Numpy
- Calculate the n-th discrete difference along specific axis in Numpy
- Integrate along axis 0 using the composite trapezoidal rule in Python
- Sort the masked array in-place along axis 0 in NumPy
- Calculate the n-th discrete difference over given axis in Python
- Calculate the n-th discrete difference over axis 1 in Python
- Compute the median of the masked array elements along axis 0 in Numpy
- Concatenate a sequence of masked arrays along axis 0 in Numpy
- Repeat elements of a masked array along axis 0 in NumPy
- Join a sequence of masked arrays along axis 0 in Numpy
- Count the number of masked elements along axis 0 to count in Numpy
- Return the minimum of an array along axis 0 or minimum ignoring any NaNs in Python
- Return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python
- Count the non-masked elements of the masked array along axis 0 in Numpy