在Python线性代数中返回矩阵的弗罗贝尼乌斯范数
要返回线性代数中矩阵或向量的范数,请使用Python NumPy中的LA.norm()方法。第一个参数x是输入数组。如果axis为None,则x必须是1-D或2-D的,除非ord为None。如果axis和ord都为None,则将返回x.ravel的2范数。
第二个参数ord是范数的顺序。inf表示Python NumPy的inf对象。默认值为None。将”齐”设置为参数表示弗罗贝尼乌斯范数。弗罗贝尼乌斯范数和核范数仅对矩阵定义。
步骤
首先,导入所需的库 –
import numpy as np
from 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)
要返回线性代数中矩阵或向量的范数,请在Python中使用LA.norm()方法 Numpy −
print("\nResult...\n",LA.norm(arr, 'fro'))
示例
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, 'fro'))
输出
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...
7.745966692414834