- 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 n-th discrete difference over axis 1 in Python

To calculate the n-th discrete difference, use the numpy.diff() method. 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 diff() method 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 1st parameter is the input array. The 2nd parameter is n, i.e. the number of times values are differenced. If zero, the input is returned as-is. The 3rd parameter is the axis along which the difference is taken, default is the last axis. The 4th parameter is the values to prepend or append to the input array 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.

## Steps

At first, import the required libraries −

import numpy as np

Creating a numpy array using the array() method. We have added elements of int type with nan −

arr = np.array([[10, 15, 30, 65], [80, 87, np.nan, 120]])

Display the array −

print("Our Array...

",arr)

Check the Dimensions −

print("

Dimensions of our Array...

",arr.ndim)

Get the Datatype −

print("

Datatype of our Array object...

",arr.dtype)

To calculate the n-th discrete difference, use the numpy.diff() method. 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 −

print("

Discrete difference..

",np.diff(arr, axis = 1))

## Example

import numpy as np # Creating a numpy array using the array() method # We have added elements of int type with nan arr = np.array([[10, 15, 30, 65], [80, 87, np.nan, 120]]) # Display the array print("Our Array...

",arr) # Check the Dimensions print("

Dimensions of our Array...

",arr.ndim) # Get the Datatype print("

Datatype of our Array object...

",arr.dtype) # To calculate the n-th discrete difference, use the numpy.diff() method # 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. print("

Discrete difference..

",np.diff(arr, axis = 1))

## Output

Our Array... [[ 10. 15. 30. 65.] [ 80. 87. nan 120.]] Dimensions of our Array... 2 Datatype of our Array object... float64 Discrete difference.. [[ 5. 15. 35.] [ 7. nan nan]]

- Related Questions & Answers
- Calculate the n-th discrete difference over axis 0 in Python
- Calculate the n-th discrete difference over given axis in Python
- Calculate the n-th discrete difference along axis 1 in Numpy
- Calculate the n-th discrete difference in Python
- Calculate the n-th discrete difference along specific axis in Numpy
- Calculate the n-th discrete difference in Numpy
- Calculate the n-th discrete difference for unsigned integer arrays in Python
- Calculate the n-th discrete difference after setting the number of times values are differenced in Numpy
- Return the gradient of an N-dimensional array over axis 1 in Python
- Integrate a Chebyshev series over axis 1 in Python
- Integrate a Laguerre series over axis 1 in Python
- Integrate a Hermite series over axis 1 in Python
- Integrate a Legendre series over axis 1 in Python
- Integrate a Hermite_e series over axis 1 in Python
- Calculate the nthdiscrete difference along axis 0 in Python ma.MaskedArray