Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.linalg.norm gives weird result

Tags:

python

numpy

The following gives error:

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.

How can I use the above norm with non numpy array input?

like image 502
Yair Daon Avatar asked Sep 11 '25 07:09

Yair Daon


1 Answers

As the doc string states:

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.

the first argument to norm should be an array_like object. Therefore use

In [167]: np.linalg.norm([2], np.inf)
Out[167]: 2
like image 120
unutbu Avatar answered Sep 13 '25 21:09

unutbu