Przykłady dodawania nowych wymiarów w bibliotece NumPy:
- Dodawanie nowego wymiaru – tablica 1D:
import numpy as np
# Tworzenie tablicy 1D
a = np.array([1, 2, 3])
print(a.shape) # (3,)
b = np.expand_dims(a, axis=0)
print(b.shape) # (1, 3)
import numpy as np
# Tworzenie tablicy 1D
a = np.array([1, 2, 3])
print(a.shape) # (3,)
b = a[:, np.newaxis]
print(b.shape) # (3, 1)
2. Dodawanie nowego wymiaru – tablica 2D:
import numpy as np
# Tworzenie tablicy 2D
a = np.array([[1, 2],
[3, 4]])
print(a.shape) # (2, 2)
b = np.expand_dims(a, axis=2)
print(b.shape) # (2, 2, 1)
import numpy as np
# Tworzenie tablicy 2D
a = np.array([[1, 2],
[3, 4]])
print(a.shape) # (2, 2)
b = a[:, :, np.newaxis]
print(b.shape) # (2, 2, 1)
3. Dodawanie nowego wymiaru – macierz 3D:
import numpy as np
# Tworzenie tablicy 3D
a = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
print(a.shape) # (2, 2, 2)
b = np.expand_dims(a, axis=3)
print(b.shape) # (2, 2, 2, 1)
import numpy as np
# Tworzenie tablicy 3D
a = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
print(a.shape) # (2, 2, 2)
b = a[:, :, :, np.newaxis]
print(b.shape) # (2, 2, 2, 1)