-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreshape_broadcast.py
More file actions
97 lines (67 loc) · 2.04 KB
/
Copy pathreshape_broadcast.py
File metadata and controls
97 lines (67 loc) · 2.04 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
def create_data():
"""
Create base dataset for reshaping and broadcasting.
"""
data = np.arange(1, 13) # 1 to 12
return data
def reshaping_examples(data):
"""
Demonstrates reshaping techniques.
"""
# Convert 1D → 2D
reshaped_2d = data.reshape(3, 4)
# Convert 1D → 3D
reshaped_3d = data.reshape(2, 2, 3)
# Flatten back to 1D
flattened = reshaped_2d.flatten()
# Reshape using -1 (auto dimension)
auto_reshape = data.reshape(2, -1)
return reshaped_2d, reshaped_3d, flattened, auto_reshape
def broadcasting_examples():
"""
Demonstrates broadcasting rules.
"""
# 2D array
matrix = np.array([[1, 2, 3],
[4, 5, 6]])
# 1D array
vector = np.array([10, 20, 30])
# Broadcasting: vector added to each row
broadcast_add = matrix + vector
# Scalar broadcasting
scalar_add = matrix + 5
return matrix, vector, broadcast_add, scalar_add
def advanced_broadcasting():
"""
Column-wise broadcasting example.
"""
matrix = np.array([[1, 2, 3],
[4, 5, 6]])
column_vector = np.array([[10],
[20]])
# Broadcasting column-wise
result = matrix + column_vector
return matrix, column_vector, result
def main():
data = create_data()
print("Original Data:", data)
print("\n--- Reshaping ---")
r2d, r3d, flat, auto = reshaping_examples(data)
print("2D:\n", r2d)
print("3D:\n", r3d)
print("Flatten:", flat)
print("Auto reshape:\n", auto)
print("\n--- Broadcasting (Row-wise) ---")
matrix, vector, b_add, s_add = broadcasting_examples()
print("Matrix:\n", matrix)
print("Vector:", vector)
print("Broadcast Add:\n", b_add)
print("Scalar Add:\n", s_add)
print("\n--- Broadcasting (Column-wise) ---")
mat, col_vec, result = advanced_broadcasting()
print("Matrix:\n", mat)
print("Column Vector:\n", col_vec)
print("Result:\n", result)
if __name__ == "__main__":
main()