使用 Python 線上性代數中計算矩陣的條件數(負二範數)


要計算線性代數中矩陣的條件數,可以使用 Python 中的 numpy.linalg.cond() 方法。此方法能夠根據 p 的值返回使用七種不同範數之一計算出的條件數。返回矩陣的條件數。可能是無限的。

x 的條件數定義為 x 的範數乘以 x 的逆的範數;範數可以是通常的 L2 範數或其他多種矩陣範數之一。第一個引數是 x,即需要求條件數的矩陣。第二個引數是 p,即條件數計算中使用的範數的階數。“2”作為引數設定的是負二範數(最小奇異值)。

步驟

首先,匯入所需的庫 -

import numpy as np
from numpy import linalg as LA

建立一個數組 -

arr = np.array([[ 1, 1, 0], [1, 0, 1], [1, 0, 0]])

顯示陣列 -

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.cond() 方法。此方法能夠根據 p 的值返回使用七種不同範數之一計算出的條件數 -

print("\nResult...\n",LA.cond(arr, -2))

示例

import numpy as np
from numpy import linalg as LA

# Create an array
arr = np.array([[ 1, 1, 0], [1, 0, 1], [1, 0, 0]])

# 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 compute the condition number of a matrix in linear algebra, use the numpy.linalg.cond() method in Python
print("\nResult...\n",LA.cond(arr, -2))

輸出

Our Array...
[[1 1 0]
[1 0 1]
[1 0 0]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

Shape of our Array object...
(3, 3)

Result...
0.2679491924311227

更新於: 2022年2月25日

156 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.