Numpy Subtract | How to Use Numpy.subtract() Function in Python

The Numpy subtract function is a part of numpy arithmetic operations. There are basic arithmetic operators available in the numpy module, which are add, subtract, multiply, and divide. The significance of python subtract is equivalent to the subtraction operation in mathematics.

What does Numpy Subtract Function do?

The numpy subtract function calculates the difference between the two numpy arrays. It calculates the difference between the two arrays, say x1 and x2, element-wise. The numpy.subtract() is a universal function, i.e., supports several parameters that allow you to optimize its work depending on the specifics of the algorithm.

Syntax of Numpy Subtract

numpy.subtract(a1, a2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘subtract’)

Parameters of Numpy Subtract

ParameterMandatory or Not
a1Mandatory
a2Mandatory
/Not-Mandatory
outNot-Mandatory
*Not-Mandatory
whereNot-Mandatory
castingNot-Mandatory
orderNot-Mandatory
dtypeNot-Mandatory
subokNot-Mandatory
unfuncNot-Mandatory
  • a1: [arrayLike]
    1st Input array for calculating the difference.
  • a2[arrayLike]
    2nd input array for calculating the difference.
  • out: [ndarray, None, or tuple of ndarray and None, optional]
    out will be the location where the result is to be stored. have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where: [array_like, optional]
    If the value of where is true it indicate to calculate the unfunc at that position, whereas if the value is false than it denotes to leave the value in output only.

Return Value of Numpy Subtract

The Numpy subtract function returns the difference between a1 and a2. The subtract() function can be scalar of nd-array. It depends on the a1 and a2. If a1 and a2 are scalar, than numpy.subtract() will return a scalar value. Else it will return an nd-array.

Note: The input a1 and a2 must be broadcastable to a common shape (which becomes the shape of the output).

Examples of Numpy Subtract Function

Let’s go through the examples of Numpy subtract() function and see how it works.

Example 1: Using Np.subtract() Function To subtract two numbers

import numpy as np
a1 = 24
a2 = 13

print ("1st Input number : ", a1) 
print ("2nd Input number : ", a2) 
	
dif = np.subtract(a1, a2) 
print ("Difference of two input number : ", dif) 

Output:

1st Input number :  24
2nd Input number :  13
Difference of two input number :  11

Explanation

In this simple first example, we just subtracted two numbers and get the result. Let’s take a look at each step and know what happens in each stage. First of all, we imported the numpy module as np it’s obvious because we are working on the numpy library. After that, we have taken two pre-defined inputs ’24’, ’13’, and stored them in variables ‘a1’, ‘a2’ respectively. We printed our inputs to check whether they are specified properly or not. Then the main part comes where we will find the difference between the two numbers.

Herewith the help of the np.subtract() function, we will calculate the difference between a1 and a2. This subtraction operation is identical to what we do in mathematics.

So, we will get the difference between the number 24 and 13 which is 11.

Example 2: Using Np.Subtract() Function to find the difference between two input arrays

import numpy as np
a1 = [20, 21, 5, 9]
a2 = [13, 17, 6, 11]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
dif = np.subtract(a1, a2) 
print ("Difference of two input arrays : ", dif) 

Output:

1st Input array :  [20, 21, 5, 9]
2nd Input array :  [13, 17, 6, 11]
Difference of two input arrays :  [ 7  4 -1 -2]

Explanation

From this example, things get Lil bit tricky; instead of numbers, we have used arrays as our input value.
We can now see we have two input arrays a1 & a2 with array inputs [20, 21, 5, 9] and [13, 17, 6, 11], respectively. The numpy.subtract() function will find the difference between a1 & a2 array arguments, element-wise.

So, the solution will be an array with the shape equal to input arrays a1 and a2. The difference between a1 and a2 will be calculated parallelly, and the result will be stored in the dif variable.

Example 3: Using Np.Subtract() Function To Find Difference Between Two Multi-Dimensional Arrays

import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
dif = np.subtract(a1, a2) 
print ("Difference of two input arrays : ", dif) 

Output:

1st Input array :  [[20, 21, 5], [-9, 11, 1]]
2nd Input array :  [[13, 17, 6], [1, -8, 7]]
Difference of two input arrays :  [[  7   4  -1]
 [-10  19  -6]]

Explanation

The third example in this numpy subtract() tutorial is slightly similar to the second example which we have already gone through. What we have done here in this example is instead of simple numpy array we have used a multi-dimensional array in both of our input values a1 and a2.

Make sure both the input arrays should be of the same dimension and same shapes. The numpy.subtract() function will find the difference between array arguments, element-wise.

Can We Find Difference Between Two Numpy Arrays With Different Shapes?

In simple words, No, we can’t find differences or use the numpy subtract function in two numpy arrays that have different shapes.

Let’s look it through one example,

import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[7, 13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
dif = np.subtract(a1, a2) 
print ("Difference of two input arrays : ", dif) 

Output:

ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Explanation

If the shape of two numpy arrays will be different than we will get a value error. The value error will say something like for example.

ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Here in this example we get a value error because the a2 input array has different shape than a1 input array. In order to get the difference without any value error, make sure to check the shape of arrays.

What’s Next?

NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.

Conclusion

The numpy subtract() is a compelling and essential function available in the numpy module, which can be very useful and highly recommended by many experts while finding the difference between very large data sets.

If you still have any questions regarding NumPy subtract function. Leave your question in the comments below.

Happy Pythonning!

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments