利用 Python 中的奇異值分解法返回秩虧矩陣的秩
要使用奇異值分解法返回陣列的矩陣秩,請在 Python 中使用 numpy.linalg.matrix_rank() 方法。陣列的秩是陣列中大於 tol 的奇異值數量。第一個引數 A 是輸入向量或矩陣堆疊。
第二個引數 tol 是奇異值被認為是零的閾值。如果 tol 為 None,並且 S 是 M 的奇異值陣列,而 eps 是 S 的資料型別的 epsilon 值,則將 tol 設定為 S.max() * max(M, N) * eps。第三個引數 hermitian,如果為 True,則假定 A 是 Hermitian 的,從而能使用一個更高效的方法來查詢奇異值。預設為 False。
步驟
首先,匯入所需的庫 -
import numpy as np from numpy.linalg import matrix_rank
建立一個數組 -
arr = np.eye(5)
顯示陣列 -
print("Our Array...\n",arr)
檢查維度 -
print("\nDimensions of our Array...\n",arr.ndim)
獲取資料型別 -
print("\nDatatype of our Array object...\n",arr.dtype)
獲取形狀 -
print("\nShape of our Array object...\n",arr.shape)
要使用奇異值分解法返回陣列的矩陣秩,請使用 numpy.linalg.matrix_rank() 方法 -
print("\nRank...\n",matrix_rank(arr)) arr[-1,-1] = 0. print("\nUpdated Rank (Rank-Deficit Matrix)...\n",matrix_rank(arr))
示例
import numpy as np from numpy.linalg import matrix_rank # Create an array arr = np.eye(5) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To Return matrix rank of array using Singular Value Decomposition method, use the numpy.linalg.matrix_rank() method in Python print("\nRank...\n",matrix_rank(arr)) arr[-1,-1] = 0. print("\nUpdated Rank (Rank-Deficit Matrix)...\n",matrix_rank(arr))
輸出
Our Array... [[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.]] Dimensions of our Array... 2 Datatype of our Array object... float64 Shape of our Array object... (5, 5) Rank... 5 Updated Rank (Rank-Deficit Matrix)... 4
廣告