Reputation: 1073
Let's say that I have the following array of numbers:
array([[-3 , 3],
[ 2, -1],
[-4, -4],
[-4, -4],
[ 0, 3],
[-3, -2],
[-4, -2]])
I would then like to compute the norm of the distance between each pair of consecutive numbers in the columns, i.e.
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)])
I would then like to take the mean of each column.
Is there a quick and efficient way of doing this in Python? I've been trying but have had no luck so far.
Thank you for your help!
Upvotes: 0
Views: 1263
Reputation: 19624
This will do the job:
np.mean(np.absolute(a[1:]-a[:-1]),0)
This returns
array([ 3.16666667, 3.16666667])
Explanation:
First of all, np.absolute(a[1:]-a[:-1])
returns
array([[5, 4],
[6, 3],
[0, 0],
[4, 7],
[3, 5],
[1, 0]])
which is the array of the absolute values of the differences (I assume that by norm of a number you mean absolute value). Then applying np.mean
with axis=0
returns the average value of every column.
Upvotes: 1