Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คำนวณ condition number ของเมทริกซ์ด้วย Frobenius norm ใน Python


ภาพรวม

ใช้ numpy.linalg.cond() คำนวณ condition number รองรับ 7 norms พารามิเตอร์ p = 'fro' หมายถึง Frobenius norm (รากที่สองของผลรวมกำลังสองของทุก element)

ขั้นตอนการทำงาน

1. นำเข้าไลบรารี

import numpy as np
from numpy import linalg as LA

2. สร้างเมทริกซ์ 3x3

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

3. แสดงข้อมูลเมทริกซ์

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)

4. คำนวณ condition number ด้วย Frobenius norm

print("\nResult...\n", LA.cond(arr, 'fro'))

ตัวอย่างโค้ดเต็ม

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)
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

หมายเหตุ: Frobenius norm condition number = 5.0 (ประมาณ) ใช้สำหรับวัด sensitivity ของเมทริกซ์ต่อการเปลี่ยนแปลงอินพุท