Heim  >  Artikel  >  Backend-Entwicklung  >  Matrix- und lineare Algebra-Berechnungen in Python

Matrix- und lineare Algebra-Berechnungen in Python

PHPz
PHPznach vorne
2023-08-20 17:41:301374Durchsuche

Matrix- und lineare Algebra-Berechnungen in Python

In diesem Artikel erfahren Sie, wie Sie mit Python Matrix- und lineare Algebraberechnungen durchführen, z. B. Matrixmultiplikation, Determinanten finden, lineare Gleichungen lösen usw.

Dies kann mit einem Matrixobjekt aus der NumPy-Bibliothek erreicht werden. Bei Berechnungen sind Matrizen relativ vergleichbar mit Array-Objekten.

Lineare Algebra ist ein umfangreiches Thema und würde den Rahmen dieses Artikels sprengen.

Wenn Sie jedoch Matrizen und Vektoren manipulieren müssen, ist NumPy ein guter Ausgangspunkt.

Methode zur Verwendung

  • Finden Sie die Transponierte einer Matrix mit Numpy

  • Finden Sie die Umkehrung einer Matrix mit Numpy

  • Matrix- und Vektormultiplikation

  • Verwenden Sie das Unterpaket numpy.linalg, um die Determinante einer Matrix zu erhalten

  • Finden Sie Eigenwerte mit numpy.linalg

  • Verwenden Sie numpy.linalg, um Gleichungen zu lösen

Methode 1: Finden Sie die Transponierte einer Matrix mit Numpy

numpy.matrix.T-Eigenschaft − Gibt die Transponierte der angegebenen Matrix zurück.

Die chinesische Übersetzung von

Beispiel

lautet:

Beispiel

Das folgende Programm verwendet die Eigenschaft numpy.matrix.T, um die Transponierte der Matrix zurückzugeben −

# importing NumPy module
import numpy as np

# input matrix
inputMatrix = np.matrix([[6, 1, 5], [2, 0, 8], [1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# printing the transpose of an input matrix
# by applying the .T attribute of the NumPy matrix of the numpy Module
print("Transpose of an input matrix\n", inputMatrix.T)

Ausgabe

Bei der Ausführung generiert das obige Programm die folgende Ausgabe:

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Transpose of an input matrix
 [[6 2 1]
 [1 0 4]
 [5 8 3]]

Methode 2: Finden Sie die Umkehrung einer Matrix mit Numpy

numpy.matrix.I-Eigenschaft – Gibt die Umkehrung der angegebenen Matrix zurück.

Die chinesische Übersetzung von

Beispiel

lautet:

Beispiel

Das folgende Programm verwendet die Eigenschaft numpy.matrix.I, um die Umkehrung einer Matrix zurückzugeben −

# importing NumPy module 
import numpy as np

# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# printing the inverse of an input matrix 
# by applying the .I attribute of the NumPy matrix of the numpy Module
print("Inverse of an input matrix:\n", inputMatrix.I)

Ausgabe

Bei der Ausführung generiert das obige Programm die folgende Ausgabe:

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Inverse of an input matrix:
 [[ 0.21333333 -0.11333333 -0.05333333]
 [-0.01333333 -0.08666667  0.25333333]
 [-0.05333333  0.15333333  0.01333333]]

Methode 3: Matrizen und Vektoren multiplizieren

Die chinesische Übersetzung von

Beispiel

lautet:

Beispiel

Das folgende Programm gibt das Produkt aus Eingabematrix und Vektor unter Verwendung des *-Operators -

zurück
# importing numpy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# creating a vector using numpy.matrix() function 
inputVector = np.matrix([[1],[3],[5]])

# printing the multiplication of the input matrix and vector 
print("Multiplication of input matrix and vector:\n", inputMatrix*inputVector)

Ausgabe

Bei der Ausführung generiert das obige Programm die folgende Ausgabe:

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Multiplication of input matrix and vector:
 [[34]
 [42]
 [28]]

Methode 4: Verwenden Sie das Unterpaket numpy.linalg, um die Determinante der Matrix zu erhalten

numpy.linalg.det()-Funktion − Berechnet die Determinante einer quadratischen Matrix.

Die chinesische Übersetzung von

Beispiel

lautet:

Beispiel

Das folgende Programm verwendet die Funktion numpy.linalg.det(), um die Determinante der Matrix −

zurückzugeben
# importing numpy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)

# getting the determinant of an input matrix 
outputDet = np.linalg.det(inputMatrix)

# printing the determinant of an input matrix 
print("Determinant of an input matrix:\n", outputDet)

Ausgabe

Bei der Ausführung generiert das obige Programm die folgende Ausgabe:

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Determinant of an input matrix:
 -149.99999999999997

Fünfte Möglichkeit, Eigenwerte mit numpy.linalg zu finden

numpy.linalg.eigvals()-Funktion − Berechnet die Eigenwerte und rechten Eigenvektoren der angegebenen quadratischen Matrix/Matrix.

Die chinesische Übersetzung von

Beispiel

lautet:

Beispiel

Das folgende Programm gibt die Eigenwerte einer Eingabematrix mithilfe der Funktion numpy.linalg.eigvals() −

zurück
# importing NumPy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
 
# getting Eigenvalues of an input matrix 
eigenValues = np.linalg.eigvals(inputMatrix)
 
# printing Eigenvalues of an input matrix 
print("Eigenvalues of an input matrix:\n", eigenValues)

Ausgabe

Bei der Ausführung generiert das obige Programm die folgende Ausgabe:

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
Eigenvalues of an input matrix:
 [ 9.55480959  3.69447805 -4.24928765]

Methode 6: Verwenden Sie numpy.linalg, um Gleichungen zu lösen

Wir können ein Problem lösen, das dem Ermitteln des Werts von X für A*X = B ähnelt,

Wobei A eine Matrix und B ein Vektor ist.

Die chinesische Übersetzung von

Beispiel

lautet:

Beispiel

Das Folgende ist ein Programm, das die Funktion „solve()“ verwendet, um den x-Wert zurückzugeben-

# importing NumPy module 
import numpy as np
 
# input matrix 
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
 
# creating a vector using np.matrix() function 
inputVector = np.matrix([[1],[3],[5]])
 
# getting the value of x in an equation inputMatrix * x = inputVector
x_value = np.linalg.solve(inputMatrix, inputVector)
 
# printing x value
print("x value:\n", x_value)
 
# multiplying input matrix with x values 
print("Multiplication of input matrix with x values:\n", inputMatrix * x_value)

Ausgabe

Bei der Ausführung generiert das obige Programm die folgende Ausgabe:

Input Matrix:
 [[6 1 5]
 [2 0 8]
 [1 4 3]]
x value:
 [[-0.39333333]
 [ 0.99333333]
 [ 0.47333333]]
Multiplication of input matrix with x values:
 [[1.]
 [3.]
 [5.]]

Fazit

In diesem Artikel haben wir gelernt, wie man Matrix- und lineare Algebraoperationen mit dem NumPy-Modul in Python durchführt. Wir haben gelernt, wie man die Transponierte, Inverse und Determinante einer Matrix berechnet. Wir haben auch gelernt, wie man einige Berechnungen in der linearen Algebra durchführt, wie zum Beispiel das Lösen von Gleichungen und das Bestimmen von Eigenwerten.

Das obige ist der detaillierte Inhalt vonMatrix- und lineare Algebra-Berechnungen in Python. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen