- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 n-th discrete difference after setting the number of times values are differenced in Numpy

To calculate the n-th discrete difference along the given axis, use the **MaskedArray.diff()** method in Python Numpy. The "n" parameter is used to set the number of times values are differenced. If zero, the input is returned as-is.

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 the input 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 the input 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...

", 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("

Our Masked Array...

", maskArr)

Get the type of the masked array −

print("

Our Masked Array type...

", maskArr.dtype)

Get the dimensions of the Masked Array −

print("

Our Masked Array Dimensions...

",maskArr.ndim)

Get the shape of the Masked Array −

print("

Our Masked Array Shape...

",maskArr.shape)

Get the number of elements of the Masked Array −

print("

Number of elements in the Masked Array...

",maskArr.size)

To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method in Python Numpy. The "n" parameter is used to set the number of times values are differenced. If zero, the input is returned as-is:

print("

Result..

.", np.diff(maskArr, n = 2))

## 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...

", 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("

Our Masked Array...

", maskArr) # Get the type of the masked array print("

Our Masked Array type...

", maskArr.dtype) # Get the dimensions of the Masked Array print("

Our Masked Array Dimensions...

",maskArr.ndim) # Get the shape of the Masked Array print("

Our Masked Array Shape...

",maskArr.shape) # Get the number of elements of the Masked Array print("

Number of elements in the Masked Array...

",maskArr.size) # To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method in Python Nump # The "n" parameter is used to set the number of times values are differenced. # If zero, the input is returned as-is. print("

Result..

.", np.diff(maskArr, n = 2))

## 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.. . [[--] [103] [--] [39]]

- Related Articles
- Calculate the n-th discrete difference in Numpy
- Calculate the n-th discrete difference along axis 1 in Numpy
- Calculate the n-th discrete difference along specific axis in Numpy
- Calculate the n-th discrete difference in Python
- Calculate the n-th discrete difference over given axis in Python
- Calculate the n-th discrete difference over axis 1 in Python
- Calculate the n-th discrete difference over axis 0 in Python
- Calculate the n-th discrete difference for unsigned integer arrays in Python
- If $m$ times the $m^{th}$ term of an AP is equal to $n$ times its $n^{th}$ term. find the $( m+n)^{th}$ term of the AP.
- Calculate the absolute value of float values in Numpy
- How to calculate the absolute difference between two values/times in Excel?
- For an AP, if \( m \) times the mth term equals \( \mathrm{n} \) times the \( n \) th term, prove that \( (m+n) \) th term of the AP is zero. \( (m ≠ n) \).
- Maximum number of dots after throwing a dice N times in C++
- N-th root of a number in C++
- N-th Tribonacci Number in C++