Python 计算数组中每个连续元素之间的距离
假设我有以下数字数组:Python 计算数组中每个连续元素之间的距离,python,numpy,Python,Numpy,假设我有以下数字数组: array([[-3 , 3], [ 2, -1], [-4, -4], [-4, -4], [ 0, 3], [-3, -2], [-4, -2]]) 然后我想计算列中每对连续数字之间的距离的范数,即 array([[norm(2--3), norm(-1-3)], [norm(-4-2), norm(-4--1)], [norm(-4--4), norm(-4--4)], [norm(0--4), n
array([[-3 , 3],
[ 2, -1],
[-4, -4],
[-4, -4],
[ 0, 3],
[-3, -2],
[-4, -2]])
然后我想计算列中每对连续数字之间的距离的范数,即
array([[norm(2--3), norm(-1-3)],
[norm(-4-2), norm(-4--1)],
[norm(-4--4), norm(-4--4)],
[norm(0--4), norm(3--4)],
[norm(-3-0), norm(-2-3)],
[norm(-4--3)-3, norm(-2--2)])
然后我想取每列的平均值
在Python中是否有一种快速有效的方法来实现这一点?我一直在努力,但到目前为止运气不好
谢谢你的帮助 这将完成工作:
np.mean(np.absolute(a[1:]-a[:-1]),0)
这是回报
array([ 3.16666667, 3.16666667])
说明:
首先,np.absolute(a[1:]-a[:-1])
返回
array([[5, 4],
[6, 3],
[0, 0],
[4, 7],
[3, 5],
[1, 0]])
这是差的绝对值的数组(我假设一个数的范数表示绝对值)。然后应用np.mean
和axis=0
返回每列的平均值。在norm(2-3)
中2-3
是什么意思?你能写下实际的期望值吗?