Tutorials  /  Python

NumPy Matrixmultiplikation

Ccentron Redaktion · März 2024 ·2 Min. Lesezeit ·Python, Tutorial

Die Matrixmultiplikation mit NumPy kann mit den folgenden drei Methoden durchgeführt werden.

  1. multiply(): elementweise Matrixmultiplikation.
  2. matmul(): Matrixprodukt zweier Arrays.
  3. dot(): Skalarprodukt zweier Arrays.

1. NumPy Matrixmultiplikation elementweise

Wenn Sie eine elementweise Matrixmultiplikation durchführen möchten, können Sie die Funktion multiply() verwenden.

Python
import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[5, 6],
                 [7, 8]])

arr_result = np.multiply(arr1, arr2)

print(arr_result)


Output

Code
[[ 5 12]
 [21 32]]

VM

Passende Infrastruktur bei centron

Skripte und Dienste brauchen irgendwann eine Umgebung, die durchläuft: ccloud³ VMs ab 3,12 € im Monat, stundengenau abgerechnet. Cloud Server mieten →

2. Matrixprodukt zweier NumPy Arrays

Wenn Sie das Matrixprodukt zweier Arrays erzielen möchten, verwenden Sie die Funktion matmul().

Python
import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[5, 6],
                 [7, 8]])

arr_result = np.matmul(arr1, arr2)

print(f'Matrix Product of arr1 and arr2 is:\n{arr_result}')

arr_result = np.matmul(arr2, arr1)

print(f'Matrix Product of arr2 and arr1 is:\n{arr_result}')


Output

Code
Matrix Product of arr1 and arr2 is:
[[19 22]
 [43 50]]
Matrix Product of arr2 and arr1 is:
[[23 34]
 [31 46]]


3. Skalarprodukt zweier NumPy Arrays

Die Funktion numpy dot() gibt das Skalarprodukt zweier Arrays zurück. Das Ergebnis ist dasselbe wie bei der Funktion matmul() für ein- und zweidimensionale Arrays.

Python
import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[5, 6],
                 [7, 8]])

arr_result = np.dot(arr1, arr2)

print(f'Dot Product of arr1 and arr2 is:\n{arr_result}')

arr_result = np.dot(arr2, arr1)

print(f'Dot Product of arr2 and arr1 is:\n{arr_result}')

arr_result = np.dot([1, 2], [5, 6])
print(f'Dot Product of two 1-D arrays is:\n{arr_result}')


Output

Code
Dot Product of arr1 and arr2 is:
[[19 22]
 [43 50]]
Dot Product of arr2 and arr1 is:
[[23 34]
 [31 46]]
Dot Product of two 1-D arrays is:
17


Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren