numpy.linalg.norm 返回奇怪的结果

4 投票
1 回答
12157 浏览
提问于 2025-04-18 15:42

下面的代码会出错:

print numpy.linalg.norm(2) # returns 2
print numpy.linalg.norm(2, np.inf) # returns error,
print numpy.linalg.norm(2, np.inf) # returns the same error:

ValueError: Improper number of dimensions to norm.

我该如何在不使用numpy数组的情况下,使用上面的规范呢?

1 个回答

7

正如文档说明的内容所说:

In [165]: np.linalg.norm?
Definition: np.linalg.norm(x, ord=None, axis=None)   
...    
Parameters
----------
x : array_like
    Input array.  If `axis` is None, `x` must be 1-D or 2-D.

传给norm的第一个参数应该是一个类似数组的对象。所以要使用

In [167]: np.linalg.norm([2], np.inf)
Out[167]: 2

撰写回答