One of the rudimentary arithmetic operations is to subtract a given entity from another. Though this might be a no-brainer that doesn’t involve the computing capabilities of a programming language such as Python, one might just underestimate this operation when it comes to handling lumpsum data.
This article sets out to explore the different variances to carry out subtraction using the subtract( ) function from the numpy library, listed as follows.
- Subtracting two scalars
- Subtracting two arrays
- Subtracting one array & a scalar
- Subtracting arrays of different sizes
- Selective subtraction
Syntax of numpy.subtract( )
Before getting on with solving examples, let us first understand the syntax of this function.
numpy.subtract(x1, x2, where=True, dtype=None)
- x1, x2 – are the scalars or one-dimensional array or two-dimensional array upon which the subtraction is to be carried out
- where – used to specify the location within the array which is only to be subjected to subtraction by typing TRUE leaving the other entities exempted from being subtracted
- dtype – used to specify the type of data that is to be returned as the result
Use the following code to import the numpy library before getting started with subtracting the entities.
import numpy as np
Using numpy.subtract() for subtracting two scalars
Scalar quantities are those which don’t have any direction (Does your school physics ring a bell?) means they are only a number, unlike an array which possesses a collection of numbers. Let us now use the subtract( ) function to subtract two numbers.
a = 10 b = 5 np.subtract(a,b)
Using numpy.subtract() for subtracting two arrays
Now we shall get on with subtracting a pair of one-dimensional arrays of the same size. Yep! It is imperative that the arrays subjected to subtraction be of the same size for the subtract( ) function to perform as intended. The arrays can be fed using the array( ) function as shown below.
ar_1 = np.array([2,1,0]) ar_2 = np.array([4,3,5])
Now array 2 is to be subtracted from array 1 using the following code.
The subtraction can also be carried out using the minus operator (-) using the below code.
Using numpy.subtract() for subtracting one array & a scalar:
While it is important for one to check whether the two arrays that ought to be subtracted are of the same size, Python provides us with the flexibility to subtract a scalar quantity from an array. To comprehend what happens here, one ought to know how this is being executed.
The scalar quantity provided will be removed from each element of the array and the result will be provided. Let’s get started by creating an array, this time it’s two-dimensional.
ar_3 = np.array([[2,1,0],[3,4,5],[6,7,8]])
Once done the subtract( ) function is to be deployed to remove ’10’ from each element in the above array.
Using numpy.subtract() for subtracting arrays of different sizes
This might be contradicting the statement given earlier in this article that two arrays ought to be of the same size for using subtract( ) function. Well, it’s still true but there’s an exception!
Python allows subtracting two arrays of different sizes provided that the number of columns in both arrays is equal. So, let’s create two arrays that comply with just that.
ar_2 = np.array([4,3,5]) ar_3 = np.array([[2,1,0],[3,4,5],[6,7,8]])
Now the subtract( ) function can be summoned to do its thing.
Using numpy.subtract() for selective subtraction
In this technique, one shall use the where component of the subtract( ) function. As explained earlier this would selectively subtract only at the places where the function is given as TRUE and replace the other places with the entities of the array which is being subtracted.
ar_3 = np.array([[2,1,0],[3,4,5],[6,7,8]]) ar_4 = np.array([[0,9,10],[11,3,9],[7,4,7]]) R = np.subtract(ar_3,ar_4,where=[False, True, False])
It’s evident that the elements of the first & third position of the resultant array are replaced with those of ar_4 whilst the subtraction is carried out in the second position as instructed.
Now that we have reached the end of this article, hope it has elaborated on how to subtract entities using Python programming. Here’s another article that details how to multiply entities in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, hasta luego!