你能解释一下为什么在 Numpy 中计算 1 维数组和相同 2 维数组的 L-inf 范数的结果不同吗?

问题描述 投票:0回答:1

请看下面的代码。

import numpy as np

a = np.array([-1, -3, -1, -9, 2])
b = np.array([[-1, -3, -1, -9, 2]])

print("Infinity norm of a:", np.linalg.norm(a, ord=np.inf))
print("Infinity norm of b:", np.linalg.norm(b, ord=np.inf))

输出(Google Colab)是:

Infinity norm of a: 9.0
Infinity norm of b: 16.0

Numpy 版本是 1.25.2

Google Colab Python 版本是 3.10.12

请参阅上面的描述。我期待同样的结果。但一维和二维 arryas 的 L-inf 范数显然不同。

python arrays numpy dimensions norm
1个回答
0
投票

文档中的表格直接解释了差异!
https://numpy.org/doc/stable/reference/ generated/numpy.linalg.norm.html

命令 矩阵范数 向量范数
inf
max(sum(abs(x), axis=1))
max(abs(x))
© www.soinside.com 2019 - 2024. All rights reserved.