-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_broadcasting.py
More file actions
32 lines (23 loc) · 884 Bytes
/
Copy pathnumpy_broadcasting.py
File metadata and controls
32 lines (23 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# numpy_broadcasting.py
# ===============================
# This file explains how broadcasting works in NumPy.
import numpy as np
# ---------------------------
# 1. Adding a scalar to an array (broadcasting the scalar)
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("Original Array:\n", arr)
print("\nAdd 10 to each element:\n", arr + 10)
# ---------------------------
# 2. Broadcasting a 1D array to a 2D array
arr2 = np.array([[10, 20, 30],
[40, 50, 60]])
row = np.array([1, 2, 3]) # Shape (3,)
# NumPy will automatically stretch the 1D row across the rows of arr2
result = arr2 + row
print("\nBroadcasting Row Addition:\n", result)
# ---------------------------
# 3. Broadcasting column-wise (reshape needed)
col = np.array([10, 20]).reshape((2, 1)) # Shape (2, 1)
result2 = arr2 + col
print("\nBroadcasting Column Addition:\n", result2)