NumPy ist ein Paket, welches Python um Funktionen für das schnelle Rechnen mit großen Zahlenmengen (Matrizen, Vektoren) erweitert. NumPy ist im Python-Ökosystem der De-Facto-Standard für numerische Berechnungen wie beispielsweise das Lösen großer linearer oder nichtlinearer Gleichungssysteme.
import numpy as npGrundlage für die Arbeit mit NumPy sind die NumPy-Arrays, welche durch Darstellung von Vektoren und Matrizen dienen.
2.4.1NumPy-Arrays erstellen¶
np.array([4, 5, 2, 3]) # aus einer Listearray([4, 5, 2, 3])np.zeros(4) # eindimensionales Array mit Nullenarray([0., 0., 0., 0.])np.ones((3, 2)) # zweidimensionales Array mit Einsenarray([[1., 1.],
[1., 1.],
[1., 1.]])np.eye(5) # Einheitsmatrixarray([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])2.4.2Zugriff auf Array-Elemente¶
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
aarray([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])print(a[0, 0]) # 1. Zeile, 1. Spalte
print(a[1, 0]) # 2. Zeile, 1. Spalte
print(a[-1, -1]) # Element unten rechts1
4
9
a[1:3, :2]array([[4, 5],
[7, 8]])a[0, :] # 1. Zeilearray([1, 2, 3])2.4.3Eigenschaften von Arrays¶
a.shape # Größe (Zeilen, Spalten)(3, 3)a.dtype # Datentyp der Einträgedtype('int64')b = np.array([4.1, 6, 3, 5, 4, 3])
print(b.shape)
print(b.dtype)(6,)
float64
2.4.4Rechnen mit Arrays¶
a = np.array([4.1, 6, 3])
b = np.array([5, 4, 3])
2 * a + barray([13.2, 16. , 9. ])a * barray([20.5, 24. , 9. ])A = np.array([[1, 2], [3, 4]])
x = np.array([[5], [6]]) # Spaltenvektor!
print(A)
print()
print(b)
print()
print(A * x) # elementweise Multiplikation (vorher "Broadcasting")
print()
print(A @ x) # Matrix-Vektor-Multiplikation[[1 2]
[3 4]]
[5 4 3]
[[ 5 10]
[18 24]]
[[17]
[39]]
print(A * A) # elementweises Produkt
print()
print(A @ x) # Matrix-Produkt[[ 1 4]
[ 9 16]]
[[17]
[39]]
A.T # transponierte Matrixarray([[1, 3],
[2, 4]])np.sin(A) # Sinus elementweisearray([[ 0.84147098, 0.90929743],
[ 0.14112001, -0.7568025 ]])2.4.5Arrays vergleichen¶
a = np.array([1, 2, 3])
b = np.array([-1, 3, 3])
print(a > b)[ True False False]
c = a > b
print(c.any())
print(c.all())True
False
2.4.6Weitere Möglichkeiten für den Elementzugriff¶
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
aarray([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])a[a > 3]array([4, 5, 6, 7, 8, 9])a[np.logical_and(a > 3, a < 6)]array([4, 5])a[a > 4] = 20
aarray([[ 1, 2, 3],
[ 4, 20, 20],
[20, 20, 20]])2.4.7Lineare Algebra¶
a = np.array([1, 2, 3])
b = np.array([1, 0, 2])
print(np.inner(a, b)) # Skalarprodukt
print(np.cross(a, b)) # Kreuzprodukt7
[ 4 1 -2]
A = np.array([[1, 2, 3], [4, 5, 5], [7, 7, 9]])
print(np.linalg.det(A)) # Determinante
print(np.linalg.inv(A)) # Inverse-12.999999999999995
[[-0.76923077 -0.23076923 0.38461538]
[ 0.07692308 0.92307692 -0.53846154]
[ 0.53846154 -0.53846154 0.23076923]]
A = np.array([[2, 0],
[1, 1]])
b = np.array([2, 3])
print(np.linalg.solve(A, b)) # LGS lösen[1. 2.]
2.4.8Zufallszahlen¶
Für Tests und Simulationen sind Zufallszahlen sehr nützlich. Um Zufallszahlen zu erzeugen, muss zunächst der Zufallszahlengenerator von NumPy mit einem Startwert initialisiert werden. Der Startwert legt die Folge der später generierten Zufallszahlen fest, sodass bei späterer Wiederholung von Rechnungen sehr leicht wieder die gleichen Zufallszahlen verwendet werden können.
rng = np.random.default_rng(123) # wie der Startwert gewählt wird, ist egalrng.integers(23, 42, (4, 10))array([[23, 35, 34, 24, 40, 27, 27, 26, 29, 26],
[29, 38, 31, 40, 31, 28, 37, 38, 39, 39],
[23, 32, 28, 27, 27, 38, 38, 27, 30, 37],
[25, 34, 31, 40, 37, 27, 38, 38, 27, 32]])rng.uniform(0, 2, (4, 4)) # Gleichverteilung auf [0, 2]array([[0.46311125, 0.33180799, 0.99557794, 1.16544928],
[0.36867597, 0.02978983, 0.94226646, 1.45648666],
[1.83720098, 1.25106801, 1.83424515, 1.7293805 ],
[0.43628575, 1.73225486, 1.46150387, 0.55573058]])rng.normal(0, 1, (4, 4)) # Standardnormalverteilungarray([[ 0.85988118, 1.76166124, 0.99332378, -0.29152143],
[ 0.72812756, -1.26160032, 1.42993853, -0.15647532],
[-0.67375915, -0.6390601 , -0.06136133, -0.39278492],
[ 2.28990995, -0.71818115, 0.03260774, 0.0280499 ]])