说我有一个(3,3,3)这样的数组。如何获得numpy的3x3x3阵列上的欧几里得距离
array([[[1, 1, 1],
[1, 1, 1],
[0, 0, 0]],
[[2, 2, 2],
[2, 2, 2],
[2, 2, 2]],
[[3, 3, 3],
[3, 3, 3],
[1, 1, 1]]])
我如何获得对应欧氏距离9个值3个值的每个向量与零值之间?
如做numpy.linalg.norm([1,1,1] - [1,1,1])
2次,然后做norm([0,0,0] - [0,0,0])
,然后norm([2,2,2] - [1,1,1])
2次,norm([2,2,2] - [0,0,0])
,然后norm([3,3,3] - [1,1,1])
2次,最后norm([1,1,1] - [0,0,0])
。
任何好的方法来这个矢量化?我想在(3,3,1)矩阵中存储距离。
其结果将是:
array([[[0. ],
[0. ],
[0. ]],
[[1.73],
[1.73],
[3.46]]
[[3.46],
[3.46],
[1.73]]])
是的,不幸的是,'norm'不允许'axis' arg。我不知道为什么。你可能会在这个[类似的问题]中找到你正在寻找的答案(http://stackoverflow.com/questions/7741878/how-to-apply-numpy-linalg-norm-to-each-row-of-a -matrix) – shx2 2013-05-10 04:17:11