create account

Arithmetic and Mathematical Operations in NumPy by leoumesh

View this thread on: hive.blogpeakd.comecency.com
· @leoumesh ·
$5.72
Arithmetic and Mathematical Operations in NumPy
<div class="text-justify">

![image.png](https://images.hive.blog/DQmQ8xSCctiWNvuk3LWdNZ66azeD2mniJTU1kbZmHyYsuPa/image.png)
<center>[Image Source](https://medium.com/predict/numpy-arithmetic-arrays-operations-e1c20be41c5a)</center>

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 5*3 and 3*5 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
```
<br>
The output is:

![image.png](https://images.hive.blog/DQmR4LBB4rdcY54sRgip3qnJqTSqCnkwwG2kCDwFKAv284h/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](https://images.hive.blog/DQmWhtvkuBnGYTjmAvRg27hcxm1DGkepvvRwErCoBpQJd6F/image.png)

Now lets try adding `array_2` and `array_3`. Remember the shape of first one is 5*3 and the second one is 3*5. 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](https://images.hive.blog/DQmRawTBzTkFac7fmENZCULRzVRN5uCc8pLZQtXNZ53UKyL/image.png)

Lets try adding `array_3` with `array_1`.

```
array_1

array_3

array_3 + array_1
```
<br>
Now we can see the output as:

![image.png](https://images.hive.blog/DQmYJfcNfXx7rJfi75xDUx9wXzARey5Y7ZdDnC2BEHaAKoM/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
```
<br>

![image.png](https://images.hive.blog/DQmPcWiAL96jkgusHVTWvQ3ku2nS2w1Vqg8zeHYHmsh6GBb/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)
```
<br>

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](https://images.hive.blog/DQmSeLfKjaQTCtsG2a5Pf682s4ASVm7Yrt88F3w48dbyH1F/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)
```
<br>

The output is:

![image.png](https://images.hive.blog/DQme2Dtu9DcHUFpbzFJJDV8GQ7JZd4HJTkfeyAKBAiiUP3L/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.
<br>


![image.png](https://images.hive.blog/DQmbfCV8Nv88p2BUDHE7nVYqiEKFhcNjnLBHqmufnrAVDNn/image.png)

Link to previous NumPy Post:

1. [Intro To NumPy Library](https://hive.blog/hive-196387/@leoumesh/intro-to-numpy-library)
2. [Working With Arrays in NumPy](https://hive.blog/hive-196387/@leoumesh/working-with-arrays-in-numpy)
3. [Indexing and Slicing an Array in NumPy](https://hive.blog/hive-196387/@leoumesh/indexing-and-slicing-an-array-in-numpy)
4. [Array Manipulation Using NumPy](https://hive.blog/hive-196387/@leoumesh/array-manipulation-using-numpy)

</div>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 270 others
properties (23)
authorleoumesh
permlinkarithmetic-and-mathematical-operations-in-numpy
categoryhive-196387
json_metadata{"tags":["hive-196387","python","numpy","code","coding","programming","math","mathematics"],"image":["https://images.hive.blog/DQmQ8xSCctiWNvuk3LWdNZ66azeD2mniJTU1kbZmHyYsuPa/image.png","https://images.hive.blog/DQmR4LBB4rdcY54sRgip3qnJqTSqCnkwwG2kCDwFKAv284h/image.png","https://images.hive.blog/DQmWhtvkuBnGYTjmAvRg27hcxm1DGkepvvRwErCoBpQJd6F/image.png","https://images.hive.blog/DQmRawTBzTkFac7fmENZCULRzVRN5uCc8pLZQtXNZ53UKyL/image.png","https://images.hive.blog/DQmYJfcNfXx7rJfi75xDUx9wXzARey5Y7ZdDnC2BEHaAKoM/image.png","https://images.hive.blog/DQmPcWiAL96jkgusHVTWvQ3ku2nS2w1Vqg8zeHYHmsh6GBb/image.png","https://images.hive.blog/DQmSeLfKjaQTCtsG2a5Pf682s4ASVm7Yrt88F3w48dbyH1F/image.png","https://images.hive.blog/DQme2Dtu9DcHUFpbzFJJDV8GQ7JZd4HJTkfeyAKBAiiUP3L/image.png","https://images.hive.blog/DQmbfCV8Nv88p2BUDHE7nVYqiEKFhcNjnLBHqmufnrAVDNn/image.png"],"links":["https://medium.com/predict/numpy-arithmetic-arrays-operations-e1c20be41c5a"],"app":"hiveblog/0.1","format":"markdown"}
created2024-04-26 10:54:06
last_update2024-04-26 10:54:06
depth0
children1
last_payout2024-05-03 10:54:06
cashout_time1969-12-31 23:59:59
total_payout_value2.796 HBD
curator_payout_value2.922 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,192
author_reputation212,340,493,251,438
root_title"Arithmetic and Mathematical Operations in NumPy"
beneficiaries
0.
accountstemsocial
weight600
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id133,136,643
net_rshares14,681,015,411,087
author_curate_reward""
vote details (334)
@stemsocial ·
re-leoumesh-arithmetic-and-mathematical-operations-in-numpy-20240426t182832227z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> 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.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-leoumesh-arithmetic-and-mathematical-operations-in-numpy-20240426t182832227z
categoryhive-196387
json_metadata{"app":"STEMsocial"}
created2024-04-26 18:28:33
last_update2024-04-26 18:28:33
depth1
children0
last_payout2024-05-03 18:28:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length545
author_reputation22,911,435,184,347
root_title"Arithmetic and Mathematical Operations in NumPy"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id133,146,051
net_rshares0