Python线性代数中矩阵的核范数计算


在Python NumPy中,使用`LA.norm()`方法可以返回线性代数中矩阵或向量的范数。第一个参数`x`是输入数组。如果`axis`为`None`,则`x`必须是一维或二维数组,除非`ord`为`None`。如果`axis`和`ord`都为`None`,则返回`x.ravel`的2-范数。

第二个参数`ord`是范数的阶数。`inf`表示NumPy的`inf`对象。默认值为`None`。“nuc”作为参数设置的是核范数。Frobenius范数和核范数的阶数仅对矩阵定义。

步骤

首先,导入所需的库:

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)

要返回线性代数中矩阵或向量的范数,请使用`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

更新于:2022年3月9日

377 次查看

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.