使用Python中的奇异值分解法返回数组的矩阵秩
要使用奇异值分解法返回数组的矩阵秩,请在Python中使用numpy.linalg.matrix_rank()方法。数组的秩是数组中大于tol的奇异值的个数。第一个参数A是输入向量或矩阵堆栈。
第二个参数tol是将SVD值视为零的阈值。如果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)
要使用奇异值分解法返回数组的矩阵秩,请在Python中使用numpy.linalg.matrix_rank()方法:
print("\nResult (rank)...\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("\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
广告