numpy.linalg.norm给出了奇怪的结果

时间:2014-08-01 21:49:31

标签: python numpy

以下是错误:

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 个答案:

答案 0 :(得分:4)

正如the doc string所述:

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的第一个参数应该是一个array_like对象。因此使用

In [167]: np.linalg.norm([2], np.inf)
Out[167]: 2
相关问题