create account

NumPy Broadcasting by leoumesh

View this thread on: hive.blogpeakd.comecency.com
· @leoumesh ·
$3.17
NumPy Broadcasting
<div class="text-justify">

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

<center>[Image Source](https://www.educba.com/numpy-broadcasting/)</center>
Today we will look into broadcasting in NumPy. Broadcasting is a powerful feature that allows NumPy to perform arithmetic operations on arrays of different shapes and sizes, without explicitly reshaping them. This is particularly useful when working with arrays of different dimensions but performing similar operations.

Let’s look at an example where broadcasting helps us add a scalar to a 2D array.

Here’s the code:

```
import numpy as np

# Creating a 2D array of size 3x3
array_1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Adding a scalar (constant) to every element of the array
result_array = array_1 + 10

print("Original Array:")
print(array_1)

print("\nArray After Addition:")
print(result_array)
```
<br> In the code above, we created a 3x3 array, and then added a scalar value 10 to each element of the array. Due to broadcasting, NumPy automatically applies the scalar value to each element without needing any loop or explicit reshaping.

The output will look like this:

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

Now let’s see another common use of broadcasting—adding a 1D array to a 2D array. We’ll add a 1D array to each row of the 2D array.

```
# Creating a 1D array
array_2 = np.array([1, 2, 3])

# Adding the 1D array to each row of the 2D array
broadcasted_result = array_1 + array_2

print("\nArray After Broadcasting Addition:")
print(broadcasted_result)
```
<br> In this case, NumPy automatically broadcasts the 1D array across each row of the 2D array, adding the corresponding elements. 

This is what the output will look like:


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


As you can see, NumPy handled the broadcasting operation automatically, adding each element of the 1D array to the corresponding row of the 2D array. This makes the process much more efficient than manually looping through the rows and columns.

Broadcasting is very powerful because it will save you a lot of time and memory when dealing with arrays of complex shapes and sizes. It's a feature you’ll use often when working with large datasets or mathematical operations in NumPy.

</div>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 228 others
properties (23)
authorleoumesh
permlinknumpy-broadcasting
categoryhive-196387
json_metadata{"app":"peakd/2025.1.3","format":"markdown","tags":["python","programming","code","coding","tutorial","numpy","education","science","stem"],"users":[],"image":["https://images.hive.blog/DQmZu9cTsdvF477PrvtLeeNXHLadCRSoNwEAwaikRp9ebcx/image.png","https://images.hive.blog/DQmQ4ir2KVFBBTnYK7LMsNxzDTBz4Uj3yjqjmy4eK8bH8fC/image.png","https://images.hive.blog/DQmbsQLA337ZM2q8UNicivCmAZLTBsewiM1GTCkan1QLwwQ/image.png"]}
created2025-02-10 22:41:21
last_update2025-02-10 22:41:21
depth0
children1
last_payout2025-02-17 22:41:21
cashout_time1969-12-31 23:59:59
total_payout_value1.564 HBD
curator_payout_value1.601 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,415
author_reputation212,340,493,251,438
root_title"NumPy Broadcasting"
beneficiaries
0.
accountstemsocial
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,703,209
net_rshares7,753,402,608,623
author_curate_reward""
vote details (292)
@stemsocial ·
re-leoumesh-numpy-broadcasting-20250212t041543379z
<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-numpy-broadcasting-20250212t041543379z
categoryhive-196387
json_metadata{"app":"STEMsocial"}
created2025-02-12 04:15:42
last_update2025-02-12 04:15:42
depth1
children0
last_payout2025-02-19 04:15:42
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,927,767,309,334
root_title"NumPy Broadcasting"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,732,788
net_rshares0