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