NumPy - Arithmetic Operations

Learn via video courses
Topics Covered

Overview

The Python module NumPy provides a wide range of arithmetic operations like addition, subtraction, module, aggregate or statistical functions, etc., which can only be possible if arrays have the same dimensions or follow the rules of broadcasting.

Introduction

NumPy is an open-source Python library. It provides a wide range of arithmetic operations like addition, multiplication, subtraction, and division, which can be performed on the NumPy arrays.

NumPy: Arithmetic Operations

NumPy arithmetic operations are only possible if the arrays should be of the same shape or if they satisfy the rules of broadcasting.

Broadcasting is a technique that helps to perform arithmetic operations on NumPy arrays of different shapes under some rules.

We follow the array manipulation rules when performing these operations. To perform these operations, we have both functions and operators.

Addition

The NumPy 'add' function is used for performing the addition of two arrays. It performs element-wise addition.

Syntax: numpy.add()
Code:

Output:

Explanation:
Two arrays, arr1 and arr2, are added by applying the above-mentioned addition function. The addition is performed element-wise as the 1st element of 1st the array is added to the 1st element of the 2nd array, and so on.

This '+' is the NumPy arithmetic operator, which can also be used for addition.

Let's look at the example below to understand:

Code:

Output:

Explanation:
Two arrays, arr1 and arr2, are added by applying the "+" addition operator as mentioned above. The addition is performed element-wise as the 1st element of the 1st array is added to the 1st element of the 2nd array, and so on.


Subtraction

The Python NumPy 'subtract' function is used to perform a subtraction between two arrays. It subtracts arguments element-wise.

Syntax: numpy.subtract()
Code:

Output:

Explanation :
arr2 is subtracted from arr1 by using subtract function. Element-wise subtraction is performed as the 1st element of the 2nd array is subtracted from the 1st element of the 1st array, and so on.

Subtraction between arrays can also be done using the NumPy arithmetic operator "-" for subtraction.

Let's look at the example below to understand:

Code:

Output:

Explanation:
arr2 is subtracted from arr1 by using the "-" operator for subtraction. Element-wise subtraction is performed as the 1st element of the 2nd array is subtracted from the 1st element of the 1st array, and so on.


Multiplication

The NumPy 'multiply' function is used to multiply two arrays. It performs multiplication in an element-by-element manner.

Syntax: numpy.multiply()

Code:

Output:

Explanation :
Two arrays arr1 and arr2 are multiplied using the 'np.multiply' function, the 1st element of the 1st array is multiplied by the 1st element of the 2nd array, and so on.

We can also use this "\*" NumPy arithmetic operator for multiplication.

Let's look at the example below to understand:

Code:

Output:

Explanation :
Two arrays arr1 and arr2 are multiplied using the multiply operator "*", the 1st element of the 1st array is multiplied by the 1st element of the 2nd array, and so on.


Division

The NumPy 'divide' function is used to perform division between two arrays. Elements of the first array are divided by elements of the other array. It all happens element-wise.

Syntax: numpy.divide()
Code:

Output:

Explanation :
arr1 is divided by arr2 using the 'np.divide' function, the 1st element of the 1st array is divided by the 1st element of the 2nd array, and so on.

We can also use the NumPy arithmetic operator "/" for division.

Let's look at the example below to understand:

Code:

Output:

Explanation :
arr1 is divided by arr2 using the divide operator "/", the 1st element of the 1st array is divided by the 1st element of the 2nd array, and so on.

Other Functions Numpy Arithmetic Operations

mod() and power() Function

1. numpy.mod()

This function returns the remainder of the element-wise division between two arrays.

Code:

Output:

Explanation :
When the 1st element of arr1 is divided by the 1st element of arr2, i.e. 2/1, it leaves the remainder 0, in this element-wise fashion, the mod function is applied to arrays that give remainder as output.


2. numpy.power()

This function uses the first array as a base and raises the elements of the second array to the power of the first array's elements.

Code:

Output:

Explanation :
The elements of arr1 are considered as a base, and the elements of arr2 are treated as powers to the elements of arr1, like 2 raised to the power of 1 is 2, 4 raised to the power of 2 is 16, and so on.

Aggregation Functions

The Python NumPy module provides many aggregate or statistical functions like sum, min, max, mean, average, product, median, standard deviation, argmax, percentile, pump rod, cumsum, etc., for working with a single-dimensional or multi-dimensional array.

Let's look at some NumPy aggregate functions:

1. numpy.sum()

Code:

Output:

Explanation :
An array is created using the 'np. arrange function of shape (4,3) and the sum of all the elements of the array using the 'np.sum()' function is calculated. Axis = 0 is used for adding the elements column-wise, like 0+3+6+9=180+3+6+9=18, and axis = 1 is used for adding the elements row-wise, like 0+1+2=30+1+2=3.


2. numpy.average()

Code:

Output:

Explanation :
An array is created using an arange function of shape (4,3), and the average of all elements of an array using the 'np.average' function is calculated. Axis = 0 is used for finding the average of the elements column-wise like (0+3+6+9)/4=4.5(0+3+6+9)/4=4.5, and axis = 1 is used for finding the average of the elements row-wise like (0+1+2)/3=1(0+1+2)/3=1.


3. numpy.prod()

Code:

Output:

Explanation :
To calculate the product of the elements of an array with the shape (4,3), use the 'np.prod()' function. Axis = 0 is used to multiply the elements column-wise, such as 0x3x6x9=00x3x6x9=0 while axis = 1 is used to multiply the elements row-wise, such as 3x4x5=603x4x5=60.


4. np.min() Code:

Output:

Explanation :
The 'np.min()' function is used to find the smallest element of an array with the shape (4,3). Axis = 0 is used to find the smallest element along the column, i.e., in the 1st column, 0 is the smallest element, and axis = 1 is used to find the smallest elements along the row, such as 3 is the smallest element in the 2nd row.


5. numpy.max()

Code:

Output:

Explanation :
The 'np.max()' function is used to determine the element with the maximum value in an array. The element with the maximum value along a column is found using axis = 0, so the biggest element in the first column is 9. Similarly, the maximum value element along a row is found using axis = 1, so the maximum value element in the second row is 2.


6. numpy.maximum()

Code:

Output:

Explanation :
The 'np.maximum' function compares the elements of two arrays and returns the element-wise maximum elements in the new array.


7. numpy.argmin()

Code:

Output:

Explanation :
The index of the smallest element in an array is returned by the 'np.argmin()' function. Indexing starts from 0. Axis = 0 is used for performing functions column-wise, like the smallest element in the 1st column is 0 at index 0. Similarly, Axis = 1 is used for performing functions row-wise, like the smallest element in the 2nd row is 3at index0`.


8. numpy.mean()

Code:

Output:

Explanation :
The 'np.mean()' function is used to find the mean of the elements of an array. Axis = 0 is used for finding the mean of the elements along the column, such as the mean of the elements of the 1st column is 4.5, and axis = 1 is used for finding the mean of the elements row-wise, like mean of the 1st row is 1.


9. numpy.median()

Code:

Outpu:

Explanation :
The 'np.median' function is used to find the median of an array. The median is the middle value of the sorted array and can be calculated by using the following formula :

  • N/2 for an odd number of elements.
  • (N-1)/2 for an even number of elements.

N is the total number of elements.

In the above example, the median of the array along rows is found using axis = 1, and the median along columns is found using axis = 1.


10. numpy.var()

Code:

Output:

Explanation :
'np.var()' is used to calculate the variance of an array. Variance is the average of squared deviations from the mean and is calculated using the formula below :

variance-formula

The variance of the array is calculated in the above code. The variance of the elements along the column is calculated using axis = 0 and along rows using axis = 1.


11. numpy.std()

Code:

Output:

Explanation :
Standard deviation is the square root of variance. The following formula is used to calculate the standard deviation :

standard-deviation

In the code above, the standard deviation of the array is calculated. Using axis = 0 for the column and axis = 1 for the rows, the standard deviation of the elements is calculated.


12. numpy.cumsum()

Code:

Output:

Explanation :
The 'np.cumsum' is used to calculate the cumulative sum. The cumulative sum is the sum of all the previous values up to that number, like the cumulative sum for 2 in the first row is 0+1+2=30+1+2=3. It can be calculated along the columns using axis = 0 and along the rows using axis = 1.

NumPy Remainder Function

Like the mod function, this function also returns the element-wise remainder. Let's look at the example below, where two different arrays are providing the remainder of the division.

Syntax : numpy.remainder()
Code:

Output:

NumPy Reciprocal Function

numpy.reciprocal(),This function returns the element-wise reciprocal of an array. The result is always 0 for elements with absolute values greater than 1 because Python handles integers in such a way that an overflow warning is provided for an integer 0.

Let's look at the code example below:

Code:

Output:

Conclusion

  • Python NumPy module provides a lot of Numpy arithmetic operations like addition, subtraction, division, and some other functions like mod, power, some aggregate or statistical functions, etc.
  • All these functions are only possible if the arrays have the same shape or follow the rules of broadcasting.