Creating a numpy array norm

I have this numpy array

X = [[ -9.03525007   7.45325017  33.34074879][ -6.63700008   5.13299996  31.66075039][ -5.12724996   8.25149989  30.92599964][ -5.12724996   8.25149989  30.92599964]]

I want to get the norm of this array using numpy. How can i do this?

for each array inside, I need sqrt (x2 + y2 + z2), so my wull output will be an array of 4 values ​​(since there are 4 internal arrays)

+4
source share
4 answers

To get what you request (2-norm of each row in your array), you can use the argument axisfor numpy.linalg.norm:

import numpy
x = numpy.array([[ -9.03525007,   7.45325017,  33.34074879],
                 [ -6.63700008,   5.13299996,  31.66075039],
                 [ -5.12724996,   8.25149989,  30.92599964],
                 [ -5.12724996,   8.25149989,  30.92599964]])
print numpy.linalg.norm(x, axis=1)

=> 

array([ 35.33825423,  32.75363451,  32.41594355,  32.41594355])
+2
source

Why not use numpy.linalg.norm

import numpy

x = [[ -9.03525007, 7.45325017 , 33.34074879], [ -6.63700008  , 5.13299996  , 31.66075039], [ -5.12724996 , 8.25149989 , 30.92599964], [ -5.12724996   , 8.25149989  , 30.92599964]]

print numpy.linalg.norm(x)

Output:

66.5069889437
+2
source

()? :

import numpy as np
>>> xs = [[ -9.03525007, 7.45325017, 33.34074879], [-6.63700008, 5.13299996, 31.66075039], [-5.12724996, 8.25149989, 30.92599964], [-5.12724996, 8.25149989, 30.92599964]]
>>> np.linalg.norm(xs)
66.506988943656381

: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html

+1

norm(). , map() norm() .

:

from numpy.linalg import norm
norms = map(norm, x)
0

All Articles