-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-array-manipulation.py
More file actions
39 lines (32 loc) · 1.08 KB
/
Copy path02-array-manipulation.py
File metadata and controls
39 lines (32 loc) · 1.08 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
import numpy as np
print("=== Array Manipulation: Reshape, Stack, Split, Transpose ===\n")
# Reshape vs resize
a = np.arange(6).reshape(2, 3)
print("Original (2,3):\n", a)
print("Ravel (flatten):", a.ravel())
print("Flatten:", a.flatten())
# Concatenation
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
print("\n--- Concatenation ---")
print("Vstack:\n", np.vstack((x, y)))
print("Hstack:\n", np.hstack((x, y.T)))
z = np.array([[[1]], [[2]]])
print("\nStack along new axis:\n", np.stack((x, x), axis=2))
# Splitting
arr = np.arange(12).reshape(4, 3)
print("\n--- Splitting ---")
print("Original:\n", arr)
print("Hsplit:", np.hsplit(arr, 3))
print("Vsplit:", np.vsplit(arr, 2))
# Transpose & swap axes
print("\n--- Transpose ---")
print("Transpose:\n", arr.T)
print("Swap axes (0,1):\n", np.swapaxes(arr, 0, 1))
# Adding/removing dimensions
vec = np.array([1, 2, 3])
print("\n--- Expanding/Reducing dims ---")
print("Original shape:", vec.shape)
print("Column (2D):\n", vec[:, np.newaxis])
print("Row (2D):\n", vec[np.newaxis, :])
print("Squeeze:", np.squeeze(np.expand_dims(vec, axis=0)))