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

หารังก์ของเมทริกซ์ด้วย SVD ใน Python ด้วย numpy.linalg.matrix_rank()

เมธอด numpy.linalg.matrix_rank() คำนวณรังก์เมทริกซ์ด้วย SVD นับจำนวน singular values ที่ > tol ค่า tol เริ่มต้นคำนวณจาก epsilon ของ dtype และขนาดเมทริกซ์

ขั้นตอนการใช้งาน

import numpy as np
from numpy.linalg import matrix_rank

สร้างเมทริกซ์ identity 5x5 และหาค่ารังก์

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

โค้ดตัวอย่างสมบูรณ์

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