-1

Description: The following codes recieves the coordinates of two dots with n dimensions. It calculates the manhanttan distance of theses two dots The codes:

def manhanttan( ponto1, ponto2 ):
    totalp1 = 0
    totalp2 = 0
    for x in range( 0, len( ponto1 ) ):
        totalp1 += ponto1[x]
        totalp2 += ponto2[x]
    return abs( totalp1 - totalp2 )

and

def manhanttan( ponto1, ponto2 ):
    total = 0
    for x in range( 0, len( ponto1 ) ):
        total += abs( ponto1[x] - ponto2[x] )
    return total

are giving different results and i don't know why. Can somebody help me?

PS: All values in the lists are positives

PS2: With the first one my classifications gets

K1: Expected Class: 6, Found Class: 0 K2: Expected Class: 6, Found Class: 0 K3: Expected Class: 6, Found Class: 0 K4: Expected Class: 6, Found Class: 0 K5: Expected Class: 6, Found Class: 0

and with the other i get K1: Expected Class: 6, Found Class: 6 K2: Expected Class: 6, Found Class: 6 K3: Expected Class: 6, Found Class: 6 K4: Expected Class: 6, Found Class: 6 K5: Expected Class: 6, Found Class: 6

Victor
  • 319
  • 2
  • 10

3 Answers3

3

Why do you think they should be the same?

Consider summing only a single sequence of numbers:

a = [1, -1, 1, -1, 1, -1]

If you take the absolute value as you total, then you actually are summing the sequence:

a = [1, 1, 1, 1, 1, 1]

which will result in the total being reported as 6. But, if you wait until the end, to take the absolute value, you get the sum of a (0) and take the absolute value which is still 0.

*Note -- Just because all the values in each of the lists are positive doesn't mean that their differences are. e.g.
ponto1[x] - ponto2[x] could still be negative.

mgilson
  • 300,191
  • 65
  • 633
  • 696
1

It shouldn't give the save results.

Consider p1 = [0,1] and p2 = [1,0].

Then abs(sum(p1)-sum(p2)) gives 0, where sum(abs(p1-p2)) gives 2.

The distribution property of the summation gives you that abs(sum(p1)-sum(p2)) == abs(sum(p1-p2)), so you should only take the absolute value once you have calculated the total difference.

By the way, sum is a built-in python function that does exactly what you think it does.

asimoneau
  • 695
  • 7
  • 18
0

Your second function is calculating Manhattan distance between two vectors, i.e. the sum of how far apart each individual dimension is. The first function reduces each vector to a single number before taking a difference; assuming all coordinates are positive, that means it takes the Manhattan distance from origo to each vector, then takes the difference of those. That's a very different function!

Consider this pair of 2D vectors:

y
5   a
4
3
2         b
1
0 o
  0 1 2 3 4 5 x

Here we have origo at (0,0), a at (1,5) and b at (4,2). To get from a to b, I need to move b-a=(4-1,2-5)=(3,-3), for a total of sum(map(abs,[3,-3]))=6 steps. But getting from o to a is 6 steps, and from o to b likewise, so the reduction first method considers a and b equal, even though they just happen to be on the same distance line running through (6,0) and (0,6) (because the Manhattan distance equivalent of a circle is actually a rhombus, a 45° rotated square).

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26