利用 Python 中的奇异值分解法返回秩亏矩阵的秩
要使用奇异值分解法返回阵列的矩阵秩,请在 Python 中使用 numpy.linalg.matrix_rank() 方法。阵列的秩是阵列中大于 tol 的奇异值数量。第一个参数 A 是输入向量或矩阵堆栈。
第二个参数 tol 是奇异值被认为是零的阈值。如果 tol 为 None,并且 S 是 M 的奇异值数组,而 eps 是 S 的数据类型的 epsilon 值,则将 tol 设置为 S.max() * max(M, N) * eps。第三个参数 hermitian,如果为 True,则假定 A 是 Hermitian 的,从而能使用一个更高效的方法来查找奇异值。默认为 False。
步骤
首先,导入所需的库 -
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)要使用奇异值分解法返回阵列的矩阵秩,请使用 numpy.linalg.matrix_rank() 方法 -
print("\nRank...\n",matrix_rank(arr))
arr[-1,-1] = 0.
print("\nUpdated Rank (Rank-Deficit Matrix)...\n",matrix_rank(arr))示例
import numpy as np
from numpy.linalg import matrix_rank
# Create an array
arr = np.eye(5)
# 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 Return matrix rank of array using Singular Value Decomposition method, use the numpy.linalg.matrix_rank() method in Python
print("\nRank...\n",matrix_rank(arr))
arr[-1,-1] = 0.
print("\nUpdated Rank (Rank-Deficit Matrix)...\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) Rank... 5 Updated Rank (Rank-Deficit Matrix)... 4
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP