Numpy subtract – Python Numpy subtract() Function

Numpy subtract() Function:

Numpy subtract: The difference between the two NumPy arrays is calculated using the NumPy subtract function. It calculates the element-by-element difference between two arrays, say l1 and l2. Numpy. subtract() is a universal function, which means it has numerous options that can be used to optimize its performance based on the algorithm’s characteristics.

Syntax:

numpy.subtract(l1, l2, out=None)

Parameters:

l1 and l2: (Required)

np.subtract: Required. The arrays that will be subtracted must be specified. They must be broadcastable to a common shape if l1.shape!= l2.shape

out:

This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

NP subtract: The difference between the two NumPy arrays is calculated using the NumPy subtract function. It calculates the element-by-element difference between two arrays, say x1 and x2. Numpy. subtract() is a universal function, which means it has numerous options that can be used to optimize its performance based on the algorithm’s characteristics.

NumPy subtract() Function in Python

Approach:

  • Import NumPy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Create some sample arrays of different shapes to test the subtract() function.
  • Pass the first array and some random number to subtract() function of NumPy module and print the result.
  • Here it subtracts the second argument for each element of the array.
  • Print the result array.
  • Pass the first array and second array to subtract() function of the NumPy module and print the result.
  • Here it subtracts the second array elements for the first array element.
  • Print the result array.
  • Similarly, test it with other arrays of different shapes.
  • The Exit of the Program.

Below is the implementation:

# Import NumPy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the array() function to create an array.
# Store it in a variable.
arry1 = np.array([[30, 60], [90, 120], [150, 180]])
# Create some sample arrays of different shapes to test the subtract() function.
arry2 = np.array([[10, 20]])
arry3 = np.array([[100], [200], [300]])
arry4 = np.array([[55, 65], [75, 85], [95, 105]])
# Pass the first array and some random number to subtract() function of NumPy module and print the result.
# Here it subtracts the second argument for each element of the array.
print('Subtracting random number say 10 to the first array : ')
print('Subtracting 10 to first array gives :\n', np.subtract(arry1, 10))
# Print the result array.
# Pass the first array and second array to subtract() function of NumPy module and print the result.
# Here it subtracts the second array elements for the first array element.
# Print the result array.
print('Subtracting first array and second array : ')
print(np.subtract(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('Subtracting first array and third array : ')
print(np.subtract(arry1, arry3))
print('Subtracting first array and fourth array : ')
print(np.subtract(arry1, arry4))

Output:

Subtracting random number say 3 to the first array : 
Subtracting 10 to first array gives :
[[ 20 50]
[ 80 110]
[140 170]]
Subtracting first array and second array : 
[[ 20 40]
[ 80 100]
[140 160]]
Subtracting first array and third array : 
[[ -70 -40]
[-110 -80]
[-150 -120]]
Subtracting first array and fourth array : 
[[-25 -5]
[ 15 35]
[ 55 75]]