In this article, we’ll make use of the operator(-) and the Python Numpy subtract function to subtract single/multi-dimensional arrays. We’ll learn subtraction on 1D, 2D arrays, broadcasting, etc. Practical code examples with step-by-step explanations will be provided with easy code examples so you can grasp the implementation quickly.
How to Subtract Numpy Arrays (2 Methods)
The two methods that can be used to subtract numpy arrays are the subtraction operator(-) and subtract() Numpy function. Let’s now practice some code examples to better understand their work.
4 Examples of Subtraction Operator(-) and numpy.subtract() Function to Subtract Arrays
Example 1: Subtract 1D Arrays using the subtraction(-) operator and numpy.subtract function
import numpy as np ar1 = np.array([7, 8, 6]) ar2 = np.array([2,4,5]) resultSubtractionOperator = ar1 - ar2 resultSubtractMethod= np.subtract(ar1,ar2) print('Result of Subtraction operator: ',resultSubtractionOperator) print('Result of numpy.subtract method: ',resultSubtractMethod)
Result of Subtraction operator: [5 4 1] Result of numpy.subtract method: [5 4 1]
Explaining the code
- The numpy library is crucial to make arrays, so we imported it.
- Two numpy 1D arrays are created.
- These two 1D arrays are specified as operands to the subtraction(-) operator. The result is stored inside a variable and displayed using the Python print function.
- Similarly, the two arrays are passed to the numpy subtract function and the result is stored and displayed.
- The result displays that the subtraction between the first item of the first array and the first item of the second array has been done. Similarly, the second item of the first array and the second item of the second array has been subtracted from each other, and so on.
- Both the outputs from the subtraction operator and numpy.subtract() function displays the same result.
Example 2: These 2 methods applied on 2D Numpy Arrays
twoDAr1 = np.array([[3,5],[7,8]]) twoDAr2 = np.array([[1,2],[5,6]]) resSubOper = twoDAr1 - twoDAr2 resSubMet= np.subtract(twoDAr1,twoDAr2) print('Subtract operator:') print(resSubOper) print('numpy.subtract:') print(resSubMet)
Subtract operator: [[2 3] [2 2]] numpy.subtract: [[2 3] [2 2]]
Explanation of Code example
- First, we created a couple of two-dimensional (2D) arrays.
- Then we performed the subtraction on both these arrays through the 2 methods(subtraction operator and numpy subtract function).
- After, printing the result, we can see that the output generated from both these methods are same.
Element-wise subtraction is done when both the arrays are of the same shape and broadcasting(explained in example 3) will be implemented when one of the operands is scalar.
Example 3: Subtract all the items of the Array from one item (Subtraction Broadcasting)
oneDArray = np.array([22,54,34,21]) scalItem=10 resultSubOper = oneDArray - scalItem resultSubMet= np.subtract(oneDArray,scalItem) print('Subtract operator:') print(resultSubOper) print('numpy.subtract:') print(resultSubMet)
Subtract operator: [12 44 24 11] numpy.subtract: [12 44 24 11]
In this code, we created a simple 1D array. Also, we created a scalar value that will be used for subtraction with all the items of the 1D array.
Then we applied the Python Numpy subtract function and the subtraction(-) operator on the array and the scalar value. The scalar value has been broadcast to each array element and then the element-wise subtraction has been executed.
The result shows that the subtraction has been done successfully.
Example 4: Broadcasting with 2D Array
twoDArray = np.array([[10,20,40],[90,50,989]]) scalarItem=15 resOper = twoDArray - scalarItem resMet= np.subtract(twoDArray,scalarItem) print(resOper) print(resMet)
[[ -5 5 25] [ 75 35 974]] [[ -5 5 25] [ 75 35 974]]
We’ve implemented broadcasting the scalar value with the 2D array. The result shows that the output from both methods is the same. All the items of the 2D array have been subtracted with the scalar value.
In conclusion, the concept we’ve learned from this article is that how to properly subtract arrays using two methods. These methods are Python Numpy subtract function and the subtraction(-) operator. Multiple Python coding examples were used with proper and detailed explanations in order for detailed understanding.
Do visit our other article on Python Numpy functions as well. In those articles, the specified concepts are explained practically with code examples and easy explanations.
Thank you very much for reading this article.