๐Ÿ”ข 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

[1, 2, 3]

np.array([1, 2, 3])

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

.shape

Form des Arrays (z.โ€ฏB. (3, 2))

.ndim

Dimensionen (1D, 2D, 3D, โ€ฆ)

.dtype

Datentyp der Elemente (int, floatโ€ฆ)

.size

Gesamtzahl der Elemente