NumPy subtract()

The subtract() function performs element-wise subtraction of two arrays.

Example

import numpy as np

# create two arrays
array1 = np.array([4, 5, 6])  
array2 = np.array([2, 1, 3])  

# perform element-wise subtraction of the two arrays result = np.subtract(array1, array2)
print(result) # Output: [2 4 3]

subtract() Syntax

The syntax of subtract() is:

numpy.subtract(x1, x2, out = None, where = True, dtype = None)

subtract() Arguments

The subtract() function takes following arguments:

  • x1 and x2 - two input arrays or scalars to be subtracted
  • out (optional) - the output array where the result will be stored
  • where (optional) - a boolean array or condition specifying which elements to subtract
  • dtype (optional) - data type of the output array

subtract() Return Value

The np.subtract() function returns an array containing the result of element-wise subtraction between two arrays or between an array and a scalar value.


Example 1: Subtract a Scalar Value From a NumPy Array

import numpy as np

# create an array
arr = np.array([10, 20, 30])

# subtract a scalar value from the array result = np.subtract(arr, 5)
print(result)

Output

[ 5 15 25]

Here, the np.subtract() function is used to subtract a scalar value of 5 from each element of the arr array.


Example 2: Use of out and where in subtract()

import numpy as np

# create two input arrays
array1 = np.array([10, 20, 30, 50])
array2 = np.array([1, 2, 3, 5])

# create a Boolean array as a condition for subtraction
condition = np.array([True, False, True, True])

# create an empty array to store the subtracted values
result = np.empty_like(array1)

# perform element-wise subtraction between array1 and array2, # only where the condition is True and store output in result array np.subtract(array1, array2, where=condition, out=result)
print(result)

Output

[ 9  0 27 45]

The output shows the result of the subtraction operation, where the elements from array1 and array2 are subtracted together only where the corresponding condition is True.

The second element in result is 0 because the corresponding condition value is False, and therefore, the subtraction does not take place for that element.

Here, out=result specifies that the output of np.subtract() should be stored in the result array


Example 3: Use of dtype Argument in subtract()

import numpy as np

# create two arrays
array1 = np.array([14, 25, 46])
array2 = np.array([7, 12, 23])

# subtract array2 from array1 with a floating-point data type resultFloat = np.subtract(array1, array2, dtype=np.float64)
# subtract array2 from array1 with a integer data type resultInt = np.subtract(array1, array2, dtype=np.int32)
print("Floating-point result:") print(resultFloat) print("\nInteger result:") print(resultInt)

Output

Floating-point result:
[ 7. 13. 23.]

Integer result:
[ 7 13 23]

Here, by specifying the desired dtype, we can control the data type of the output array according to our requirements.

Note: To learn more about the dtype argument, please visit NumPy Data Types.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges