| by Arround The Web | No comments

NumPy Subtract

Subtraction means difference or finding the difference between same-sized values. The subtract() is a universal function of the NumPy library. This function is utilized to eradicate two or more arrays or matrices in Python. The (-) operator is used to subtract matrices or arrays. Hence, in this guide, we will go through how and when to apply the np. subtract() method in programs.

When Subtraction of Arrays in NumPy is Possible?

In mathematics, the subtraction of two arrays or matrices is possible only when both arrays are of the same dimensions. It means that both arrays have the same number of columns and rows. But in NumPy subtract() method, the NumPy library allows the subtraction among arrays that are not scalar or do not have the same dimensions.

Usage of np. subtract() Method Instead of (-) Operator

In most programs and examples, you would have seen that both the (-) operator and the subtract() method are utilized to perform subtractions. Both are used interchangeably when you want. But in most cases we consider NumPy subtract() method instead of using the (-) operator. Eventually, the (-) operator is an abstraction of np. subtract() method. If you intend to modify the specified default behavior of the (-) operator, you can use the np. subtract() method.


The NumPy subtract() method is declared as follows:

Numpy.subtract(array1, array2, /, out=none, where=true, d type=None)


In this section, we will discuss different required and optional parameters that are provided to np. subtract() method and these are the following:

Numpy.subtract(): This one is a required parameter. It is a datatype and mandatory argument to be used. It will work on lists and tuples in Python.

Array 1: It is also a required parameter. It represents the first defined array, whose size is identical to that of the second given array and can be updated to the same matrix as the second array.

Array 2: It represents the second input array and is a required argument. It has to be a similar size as the first input array and can be updated to the same matrix as the first array.

Out: It is an optional argument. It is used when we want a specific location to store a result. A new object is constructed to retain the outcome if the location is not provided.

Where: It is also an optional argument. Whenever we would like to identify specific array values to which the unfunc (universal function) won’t be executed, we use this argument.

dtype: An optional argument is used to give the result matrix a specific data type. It is identical to the type of input arrays.

To learn more about the np. subtract() method in depth, let’s look at a few illustrations:

Example no 1:

Subtraction of one-dimensional array.

In this case, we will discuss how to perform the subtraction of a one-dimensional array by using the subtract() method of the NumPy library. The one-dimensional means the array has only 1 column or 1 number of rows on which subtraction or another function of Numpy can be performed.

In the execution of this program, the NumPy library as np is imported. Then, we have the ‘m’ variable which has assigned the first array by initializing np. array() function. The values we have assigned to this array are [7, 8, 6]. Next, we have a second input array that is saved in the ‘n’ variable and the values that are assigned to the second array are [9, 10, 5]. Now, we have declared a new variable ‘o’. Then, we have to call an np. subtract() function to perform subtraction on both arrays to get our result. This function contains the two required arrays as the arguments. In the end, the print() function represents the output after performing the subtraction. The resultant array will be saved in the ‘o’ variable.

The outcome that we get after the successful implementation of the subtract() method to the one-dimensional array is:

Example no 2:

Subtraction of two-dimensional array.

In the above-illustrated code, we have cleared our concepts about subtraction of one-dimensional array on how np. subtract() function works on it. Now, in the second instance, we will see the performance of np. subtract() method on the two-dimensional array. A two-dimensional array represents the 2 number of rows or columns on which the subtraction can be done.

Then, we have to import the NumPy library as np in the program which is a required step. In the second step, we have initialized variable ‘x’. This variable retains the values of the first defined array. Next, we have to call the np. array() function which is used to acquire the components of the array. The values of the first 2D array are [30, 40] [10, 20]. Then, we have to declare the ‘y’ variable. Here we would assign second array elements by using the np. array() method. The second defined array has [10, 20] [30, 40] values. To save the output by performing the subtraction, a new variable ‘z’ would be initialized in the next statement. The np. subtract() function is called to perform subtraction on the required two-dimensional arrays. We have to pass ‘y’ and ‘x’ as the parameters of the np. subtract() method. In the last step, the print() method will show the outcome.

After the successful execution of np. subtract() method to the 2D array we have the following output:


In this guide, we talked about the NumPy subtract() method, how it works, and when it is used. We also covered the syntax, and parameters of the subtract() function. We implemented different examples with a detailed explanation of that codes. On both 1D and 2D arrays, subtraction has been done by using the subtract() method. Moreover, we also mentioned the differences and similarities between the use of the (-) operator and np. subtract() function.

Share Button

Source: linuxhint.com

Leave a Reply