使用Python中的奇異值分解法返回陣列的矩陣秩


要使用奇異值分解法返回陣列的矩陣秩,請在Python中使用numpy.linalg.matrix_rank()方法。陣列的秩是陣列中大於tol的奇異值的個數。第一個引數A是輸入向量或矩陣堆疊。

第二個引數tol是將SVD值視為零的閾值。如果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)

要使用奇異值分解法返回陣列的矩陣秩,請在Python中使用numpy.linalg.matrix_rank()方法:

print("\nResult (rank)...\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("\nResult (rank)...\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)

Result (rank)...
5

更新於:2022年2月25日

463 次檢視

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.