在 Python 中返回线性代数中矩阵的核范数
要在线性代数中返回矩阵或向量的范数,请使用Python Numpy 中的方法。第一个参数,x 是一个输入数组。如果axis是None,x必须是1-D或2-D,除非ord是None。如果axis和ord都为None,则返回2范数。LA.norm()x.ravel
第二个参数 ord 是范数的阶数。inf 表示 numpy 的 inf 对象。默认值为无。作为参数设置的“nuc”是核规范。Frobenius 和核范数阶都只为矩阵定义
脚步
首先,导入所需的库 -
import numpy as npfrom numpy import linalg as LA
创建一个数组 -
arr = np.array([[ -4, -3, -2], [-1, 0, 1], [2, 3, 4] ])
显示数组 -
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)
要在线性代数中返回矩阵或向量的范数,请使用以下方法 -LA.norm()
print("\nResult...\n",LA.norm(arr, 'nuc'))
示例
import numpy as np输出结果from numpy import linalg as LA
# Create an array
arr = np.array([[ -4, -3, -2],[-1, 0, 1],[2, 3, 4] ])
# 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 the Norm of the matrix or vector in Linear Algebra, use the LA.norm() method in Python Numpy
print("\nResult...\n",LA.norm(arr, 'nuc'))
Our Array...[[-4 -3 -2]
[-1 0 1]
[ 2 3 4]]
Dimensions of our Array...
2
Datatype of our Array object...
int64
Shape of our Array object...
(3, 3)
Result...
9.797958971132713
以上是 在 Python 中返回线性代数中矩阵的核范数 的全部内容, 来源链接: utcz.com/z/297305.html