๐ข Einstieg in Arrays#
๐ NumPy importieren#
import numpy as np
โ Best Practice: np als Alias verwenden
๐ข NumPy Arrays erstellen#
a = np.array([1, 2, 3])
print(a)
โก๏ธ a ist jetzt ein 1D NumPy Array mit den Werten 1, 2, 3
๐งฑ Mehrdimensionale Arrays:#
b = np.array([[1, 2, 3],
[4, 5, 6]])
โก๏ธ b ist ein 2D Array (Matrix)
๐ Unterschied zu Python-Listen#
Python-Liste |
NumPy Array |
---|---|
|
|
beliebige Datentypen |
homogener Datentyp (z.โฏB. int) |
langsam bei Schleifen |
schnell durch Vektorisierung |
โ ๏ธ Beispiel: mathematischer Unterschied#
liste = [1, 2, 3]
array = np.array([1, 2, 3])
print(liste * 2) # โ [1, 2, 3, 1, 2, 3]
print(array * 2) # โ [2, 4, 6]
๐ 3. Eigenschaften von Arrays#
Sei
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3],
[4, 5, 6]])
๐ shape โ Form (z.โฏB. Zeilen ร Spalten)#
print(a.shape) # (3,)
print(b.shape) # (2, 3)
๐ ndim โ Anzahl Dimensionen#
print(a.ndim) # 1
print(b.ndim) # 2
๐ dtype โ Datentyp der Elemente#
print(a.dtype) # int64 oder int32 (je nach System)
๐ข size โ Anzahl aller Elemente#
print(a.size) # 3
print(b.size) # 6
โ Zusammenfassung#
Attribut |
Bedeutung |
---|---|
|
Form des Arrays (z.โฏB. (3, 2)) |
|
Dimensionen (1D, 2D, 3D, โฆ) |
|
Datentyp der Elemente (int, floatโฆ) |
|
Gesamtzahl der Elemente |