我有这个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]]
我想使用numpy来获得这个数组的规范。我怎么能这样做?
对于里面的每个数组,我需要sqrt(x2 + y2 + z2),所以我的输出wull是4个值的数组(因为有4个内部数组)
答案 0 :(得分:2)
为什么不使用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)
<强>输出:强>
66.5069889437
答案 1 :(得分:2)
要获得您要求的内容(数组中每行的2范数),您可以使用axis
参数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 :(得分:1)
您的意思是矩阵规范吗?如果是这样的话:
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
答案 3 :(得分:0)
其他人已经为您提供了norm()
功能。您可能希望map()
数组中的norm()
函数。
只是做:
from numpy.linalg import norm
norms = map(norm, x)