NumPy subtract: A step-by-step Guide with Examples

 

NumPy subtract


NumPy subtract()



One of the essential operations in NumPy is subtraction, which allows you to subtract elements from one array to another or a constant value from an array. In this blog post, we'll explore NumPy's subtract function through step-by-step examples.


Getting Started with NumPy



Before we dive into the examples, you need to ensure that you have NumPy installed.



pip install numpy



import numpy as np


Now, let's start exploring NumPy's subtract function with step-by-step examples.
 
$ads={1}


Example 1: Subtracting Two Arrays



The most straightforward use of the np.subtract function is to subtract one array from another element-wise. Here's how you can do it:



import numpy as np

# Create two NumPy arrays
array1 = np.array([10, 21, 30, 40])
array2 = np.array([5, 16, 17, 18])

# Subtract array2 from array1
result = np.subtract(array1, array2)

print(result)


In this example, NumPy subtracts the corresponding elements of array1 from array2, resulting in a new array result. The output will be [ 5 5 13 22]

 

You can also use the np.subtract function to subtract a constant value from all elements of an array. Here's an example:



import numpy as np

array = np.array([20, 40, 30, 40])

# Subtract a constant value (e.g., 5) from all elements
result = np.subtract(array, 5)

print(result)


In this example, we subtract 5 from all elements of the array, resulting in a new array result with values [15 35 25 35]


Example 3: In-Place Subtraction



NumPy also allows you to perform in-place subtraction, which means modifying an existing array with the subtraction results. You can do this using the '-=' operator:



import numpy as np

# Create a NumPy array
array = np.array([20, 40, 30, 40])

# In-place subtraction of 5 from all elements
array -= 5

print(array)


In this case, the original array is modified, and the output will be [15 35 25 35].

$ads={2}
 
NumPy supports broadcasting, which means you can perform subtraction operations between arrays of different shapes as long as they are compatible. For example, you can subtract a one-dimensional array from a two-dimensional array:



import numpy as np

# Create a two-dimensional array
matrix = np.array([[11, 22, 13], [42, 51, 63], [71, 83, 91]])

# Create a one-dimensional array
row = np.array([1, 2, 3])

# Subtract the one-dimensional array from each row of the two-dimensional array
result = matrix - row

print(result)


The output will be a new two-dimensional array where each row has been subtracted by the row array.



[[10 20 10]
 [41 49 60]
 [70 81 88]]


NumPy's 'np.subtract' function is a versatile tool for performing subtraction operations on arrays, whether it's subtracting two arrays, a constant value, or performing in-place subtraction. With broadcasting, you can efficiently work with arrays of different shapes. Understanding these capabilities of NumPy will be valuable for various scientific and data analysis tasks in Python.


Previous Post Next Post