使用 Python 和 Frobenius 範數計算線性代數中矩陣的條件數
要計算線性代數中矩陣的條件數,請在 Python 中使用 numpy.linalg.cond() 方法。此方法能夠根據 p 值返回使用七種不同範數之一計算出的條件數。返回矩陣的條件數。可能為無窮大。
x 的條件數定義為 x 的範數乘以 x 的逆的範數;範數可以是通常的 L2 範數或許多其他矩陣範數之一。第一個引數是 x,即需要查詢其條件數的矩陣。第二個引數是 p,即條件數計算中使用的範數的階數。“fro”作為引數設定的是 Frobenius 範數。
步驟
首先,匯入所需的庫:
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)
要計算線性代數中矩陣的條件數,請在 Python 中使用 numpy.linalg.cond() 方法。此方法能夠根據 p 值返回使用七種不同範數之一計算出的條件數:
print("\nResult...\n",LA.cond(arr, 'fro'))示例
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, 'fro'))輸出
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... 5.000000000000001
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP