NumPy subtract()
The numpy.subtract()
function performs element-wise subtraction of two input arrays.
If the shapes of the input arrays differ, NumPy attempts to broadcast them to a common shape.
Syntax
</>
Copy
numpy.subtract(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1, x2 | array_like | Input arrays to be subtracted element-wise. They must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the subtraction. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array containing the element-wise difference between x1
and x2
.
If both inputs are scalars, a scalar is returned.
Examples
1. Subtracting Two Arrays Element-wise
Here, we subtract two NumPy arrays element by element.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([10, 20, 30])
x2 = np.array([1, 5, 10])
# Perform element-wise subtraction
result = np.subtract(x1, x2)
# Print the result
print("Subtraction result:", result)
Output:
Subtraction result: [ 9 15 20]

2. Broadcasting in NumPy Subtraction
NumPy automatically broadcasts smaller arrays when their shape differs.
</>
Copy
import numpy as np
# Define a 2D array and a 1D array
x1 = np.array([[10, 20, 30], [40, 50, 60]])
x2 = np.array([1, 2, 3]) # Will be broadcasted
# Perform element-wise subtraction with broadcasting
result = np.subtract(x1, x2)
# Print the result
print("Subtraction result:\n", result)
Output:
Subtraction result:
[[ 9 18 27]
[39 48 57]]

3. Using the out
Parameter
Using an output array to store the results instead of creating a new array.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([10, 20, 30])
x2 = np.array([2, 4, 6])
# Create an output array
output_array = np.empty_like(x1)
# Perform subtraction and store the result in output_array
np.subtract(x1, x2, out=output_array)
# Print the results
print("Output array after subtraction:", output_array)
Output:
Output array after subtraction: [ 8 16 24]

4. Using the where
Parameter
The where
parameter allows selective subtraction based on a condition.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([10, 20, 30])
x2 = np.array([2, 4, 6])
# Define a mask (perform subtraction only where mask is True)
mask = np.array([True, False, True])
# Perform subtraction using the mask
result = np.subtract(x1, x2, where=mask)
# Print the results
print("Subtraction result with mask:", result)
Output:
Subtraction result with mask: [ 8 4603214750636966148 24]

The subtraction is performed only for elements where mask=True
, while the others remain unchanged.