Arithmetic and Mathematical Operations in NumPy

image.png

Image Source

Hello everyone. Today I am again with a new post regarding mathematical and arithmetic operations using NumPy package. The last post deals with manipulating an array in NumPy. The link to the post along with other previous posts can be found at the bottom of this blog.

First of all we will start by creating three arrays: one array is one dimensional and the rest two is two dimensional of shape 53 and 35 respectively. We will then print the content of each of these arrays.

import numpy as np

array_1 = np.array([1,3,6,8,9])

array_2 = np.array([2,3,4,2,1,9,7,5,8,6,1,1,9,2,3]).reshape(5,3)

array_3 = np.array([2,3,4,2,1,9,7,5,8,6,1,1,9,2,3]).reshape(3,5)

array_1

array_2

array_3



The output is:

image.png

First lets see the addition of array. We will try to add array_1 with itself. There are two ways to do it: the first is using + operator and the other way is using add() function.

array_1 + array_1

np.add(array_1, array_1)

You can see the output as:

image.png

Now lets try adding array_2 and array_3. Remember the shape of first one is 53 and the second one is 35. If we try to add these two arrays, we will get following error. The reason is that arrays have different shape along two dimensions that make broadcasting impossible.

image.png

Lets try adding array_3 with array_1.

array_1

array_3

array_3 + array_1



Now we can see the output as:

image.png

In the above program, the smaller array is being broadcasted to the bigger array to make the shape compatible. In the same way broadcasting works for multiplication as well:

array_3 * 2

array_3 * array_1


image.png

In the same way we can divide the array and do other operation like subtraction. Now lets see other things like getting the sum using sum() function.

array_3

np.sum(array_3)

np.sum(array_3, axis=0)

np.sum(array_3, axis=1)


If you see the output, you will notice the sum function returns the total value of all elements inside the array. Specifying the axis will return the value either across rows or columns. axis=0 does operation column-wise and axis=1 does operation along the rows.

image.png

There is another interesting function called cumsum() that helps to find the cumulative sum. Again you can specify the axis of your choice.

array_3

np.cumsum(array_3)

np.cumsum(array_3, axis=0)


The output is:

image.png

Next we will see various functions like mean, minimum and maximum value.

array_2

array_2.mean()

np.mean(array_2, axis=1)

np.mean(array_2, axis=0)

np.amax(array_2)

np.amin(array_2)

In the above code, we tried finding the mean of all element of array_2, along the rows and column, and the maximum and minimum value of element inside it.

image.png

Link to previous NumPy Post:

  1. Intro To NumPy Library
  2. Working With Arrays in NumPy
  3. Indexing and Slicing an Array in NumPy
  4. Array Manipulation Using NumPy


0
0
0.000
1 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000