In simple terms, the function performs an element-wise subtraction between two input arrays.

Let us explore.

## Function Syntax

The function syntax is as shown in the code snippet below:

## Essential Function Parameter

The function accepts various parameters, as shown in the above syntax. However, the following are the essential ones:

- x1 and x2 – refer to the arrays or array_like objects whose difference needs to be calculated.
- out – the output arrays to store the resulting value.
- where – specifies the condition that is broadcasted over the input.
- kwargs – other keyword-only arguments. Check the docs.

## Function Return Value

The function will then return the element-wise difference of the input arrays. If both input values are scalars, the function will also return a scalar value.

## Example #1

The code below shows how to use the NumPy subtract() function with two scalar values.

import numpy as np

print(f"difference: {np.subtract(10,4)}")

The code above should return the scalar difference as shown in the output below:

## Example #2

In the example below, we use the subtract() function with 2d arrays as shown:

x2 = np.array([[1,2,3], [4,5,6]])

print(f"difference:\n {np.subtract(x1, x2)}")

The code above returns the element-wise difference between the two arrays as shown:

[[ 9 18 27]

[36 45 54]]

## Example #3

You can also replace the subtract function with the – operator. An example is as demonstrated below:

x2 = np.array([[1,2,3], [4,5,6]])

print(f"difference:\n {x1 - x2}")

The resulting output is similar to using the subtract function as:

[[ 9 18 27]

[36 45 54]]

## Conclusion

This article explored how to determine the element-wise difference between two arrays using the NumPy subtract() function.

Thanks for reading & Happy coding!!