NumPy - Difference Universal Function (ufunc)



Difference Universal Function (ufunc)

A difference universal function (ufunc) in NumPy is a function used to calculate the difference between elements in an array.

This operation can be applied element-wise between two arrays, or to compute the discrete difference along a specific axis of a single array. The primary function for computing differences in NumPy is numpy.diff().

The NumPy diff() Function

The numpy.diff() function computes the difference between consecutive elements in an array, effectively calculating the first order discrete difference. It can also compute higher-order differences by specifying the n parameter.

Example

In the following example, we use the numpy.diff() function to calculate the difference between consecutive elements of a 1D array −

import numpy as np

# Define an array
a = np.array([1, 3, 6, 10, 15])

# Compute the first-order differences
diff = np.diff(a)

print("First-order differences:", diff)

Following is the output obtained −

First-order differences: [2 3 4 5]

NumPy Higher-Order Differences

The numpy.diff() function can also compute higher-order differences. By specifying the n parameter, we can calculate the difference between elements multiple times.

Example

In the following example, we calculate the second-order difference of an array using numpy.diff() function with n=2

import numpy as np

# Define an array
a = np.array([1, 3, 6, 10, 15])

# Compute the second-order differences
second_diff = np.diff(a, n=2)

print("Second-order differences:", second_diff)

This will produce the following result −

Second-order differences: [1 1 1]

NumPy diff Function for 2D Arrays

The numpy.diff() function can also be used with 2D arrays. By specifying the axis parameter, we can compute differences along a specific axis, such as row-wise or column-wise.

Example

In the following example, we use numpy.diff() function on a 2D array to compute the differences along the rows and columns −

import numpy as np

# Define a 2D array
a = np.array([[1, 2, 3], [4, 5, 6]])

# Compute the differences along the rows (axis=1)
row_diff = np.diff(a, axis=1)

# Compute the differences along the columns (axis=0)
col_diff = np.diff(a, axis=0)

print("Row-wise differences:", row_diff)
print("Column-wise differences:", col_diff)

Following is the output obtained −

Row-wise differences: 
[[1 1]
 [1 1]]
Column-wise differences: [[3 3 3]]

NumPy diff with Periodic Boundary

By default, numpy.diff() function treats the array as open-ended, meaning it calculates differences between adjacent elements. However, we can specify the prepend or append arguments to include boundary elements for periodic data.

Example

In the following example, we use numpy.diff() function with the prepend argument to treat the array as a periodic sequence −

import numpy as np

# Define an array
a = np.array([1, 3, 6, 10, 15])

# Compute the first-order differences with a periodic boundary
periodic_diff = np.diff(a, append=a[0])

print("Periodic differences:", periodic_diff)

The result will be as follows −

Periodic differences: [  2   3   4   5 -14]

The numpy.gradient() Function

The numpy.gradient() function computes the gradient of an array. The gradient is a multi-dimensional generalization of the derivative.

For a 1D array, it calculates the differences between consecutive elements and takes into account edge effects. For multi-dimensional arrays, it computes the gradient along each axis.

Example

In this example, the gradient() function calculates the gradient of an array −

import numpy as np

# Create a NumPy array
array = np.array([1, 2, 4, 7, 11])

# Compute the gradient
gradient = np.gradient(array)

print(gradient)

The output represents the rate of change between each element, with special handling at the boundaries −

[1.  1.5 2.5 3.5 4. ]

The numpy.ediff1d() Function

The numpy.ediff1d() function computes the differences between consecutive elements of an array in a flattened form. It is a simpler and faster alternative to numpy.diff() function for 1D arrays.

Example

In the following example, the ediff1d() function calculates the difference between each consecutive element of the array −

import numpy as np

# Create a NumPy array
array = np.array([1, 2, 4, 7, 11])

# Compute the element-wise differences
ediff1d_result = np.ediff1d(array)

print(ediff1d_result)

This will produce the following result −

[1 2 3 4]
Advertisements