finding euclidean distance,better code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • devnew@gmail.com

    #1

    finding euclidean distance,better code?

    hello
    while trying to write a function that processes some numpy arrays and
    calculate euclidean distance ,i ended up with this code
    (though i used numpy ,i believe my problem has more to do with python
    coding style..so am posting it here)

    ....
    # i am using these numpy.ndarrays to do the calculation
    facespace # of shape(totalimgs ,imgpixels)
    weights # of shape(totalimgs ,selectedfacesp aces)
    input_wk # of shape(selectedf acespaces,)
    distance # of shape(selectedf acespaces,) initally all 0.0 's
    mindistance #of shape(selectedf acespaces,) initally all
    0.0 's
    ....
    ....
    #here is the calculations part

    for image in range(numimgs):
    distance = abs(input_wk - weights[image, :])
    if image==0:
    #copy from distance to mindistance
    mindistance=dis tance.copy()
    if sum(mindistance ) sum(distance):
    imgindex=image
    mindistance=dis tance.copy()
    if max(mindistance ) 0.0:
    #normalise mindistance
    mindistance=min distance/(max(mindistanc e)+1)
    dist=sum(mindis tance)

    this gets me the euclidean distance value.I want to know if the way i
    coded it can be improved,made more compact....if someone can give
    suggestions it will be a great help .
    thanks
    D
  • Gabriel Genellina

    #2
    Re: finding euclidean distance,better code?

    En Mon, 17 Mar 2008 03:04:16 -0200, devnew@gmail.co m <devnew@gmail.c om>
    escribi�:
    while trying to write a function that processes some numpy arrays and
    calculate euclidean distance ,i ended up with this code
    (though i used numpy ,i believe my problem has more to do with python
    coding style..so am posting it here)
    >
    for image in range(numimgs):
    distance = abs(input_wk - weights[image, :])
    if image==0:
    #copy from distance to mindistance
    mindistance=dis tance.copy()
    if sum(mindistance ) sum(distance):
    imgindex=image
    mindistance=dis tance.copy()
    if max(mindistance ) 0.0:
    #normalise mindistance
    mindistance=min distance/(max(mindistanc e)+1)
    dist=sum(mindis tance)
    >
    this gets me the euclidean distance value.
    It looks like you're rather computing a distance derived from the 1-norm
    (sum of coordinates; like in a city with square blocks).
    I'd save the sum(mindistance ) value to avoid recomputing it in every
    iteration.
    I want to know if the way i
    coded it can be improved,made more compact....if someone can give
    suggestions it will be a great help .
    The code is pretty legible as it is now. Anyway, using min() and a
    generator:

    _, imgindex = min((sum(abs(in put_wk - weights[image, :])),image) for image
    in xrange(numimgs) )
    mindistance = abs(input_wk - weights[imgindex, :])
    # normalize and sum again


    --
    Gabriel Genellina

    Comment

    • devnew@gmail.com

      #3
      Re: finding euclidean distance,better code?

      On Mar 17, 6:17 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote
      >
      _, imgindex = min((sum(abs(in put_wk - weights[image, :])),image) for image
      in xrange(numimgs) )
      mindistance = abs(input_wk - weights[imgindex, :])
      # normalize and sum again

      thanks Gabriel
      D

      Comment

      • harryos

        #4
        Re: finding euclidean distance,better code?

        The code is pretty legible as it is now. Anyway, using min() and a
        generator:
        >
        >
        hi
        is this calculated distance really Euclidean distance? When i checked
        wikipedia http://en.wikipedia.org/wiki/Euclidean_distance
        it shows a calculation involving sum of squares of the differences of
        elements.Here in this code ,the sum of coordinates are used? is that a
        different measure?

        oharry

        Comment

        • Gabriel Genellina

          #5
          Re: finding euclidean distance,better code?

          En Fri, 28 Mar 2008 12:15:48 -0300, harryos <oswald.harry@g mail.com>
          escribió:
          >The code is pretty legible as it is now. Anyway, using min() and a
          >generator:
          >>
          >>
          >
          hi
          is this calculated distance really Euclidean distance? When i checked
          wikipedia http://en.wikipedia.org/wiki/Euclidean_distance
          it shows a calculation involving sum of squares of the differences of
          elements.Here in this code ,the sum of coordinates are used? is that a
          different measure?
          (Thanks for trimming the irrelevant parts of the message, that's good; but
          you trimmed too much text, even attribution lines - the above quoted
          sentence was mine)

          That's what I said in another paragraph. "sum of coordinates" is using a
          different distance definition; it's the way you measure distance in a city
          with square blocks. I don't know if the distance itself has a name, but
          the norm from which it is derived is called norm-1, or L1; the usual
          euclidean distance is derived from norm-2.
          See http://mathworld.wolfram.com/VectorNorm.html

          If you only want to see if two things are "close enough", this provides a
          faster measure than the euclidean distance.

          --
          Gabriel Genellina

          Comment

          • Robert Bossy

            #6
            Re: finding euclidean distance,better code?

            Gabriel Genellina wrote:
            That's what I said in another paragraph. "sum of coordinates" is using a
            different distance definition; it's the way you measure distance in a city
            with square blocks. I don't know if the distance itself has a name, but
            I think it is called Manhattan distance in reference of the walking
            distance from one point to another in this city.

            RB

            Comment

            • castironpi@gmail.com

              #7
              Re: finding euclidean distance,better code?

              On Mar 28, 10:15 am, harryos <oswald.ha...@g mail.comwrote:
              The code is pretty legible as it is now. Anyway, using min() and a
              generator:
              >
              hi
              is this calculated distance really Euclidean distance? When i checked
              wikipediahttp://en.wikipedia.or g/wiki/Euclidean_dista nce
              it shows a calculation involving sum of squares of the differences of
              elements.Here in this code ,the sum of coordinates are used? is that a
              different measure?
              I want the angle into an array. < sign bit on distance to [zero
              element/kernel]. Are you coming out in uni-pole and/or lensed/focused/
              parabolaed? Is that a yes-or-no question?

              Comment

              • harryos

                #8
                Re: finding euclidean distance,better code?

                the norm from which it is derived is called norm-1, or L1; the usual euclidean distance is derived from norm-2.
                If you only want to see if two things are "close enough", this provides a faster measure than the euclidean distance.

                thanks Gabriel for the detailed explanation..
                if i were to calculate the euclidean distance in the above example how
                should i go about it..?
                should i replace
                distance = abs(input_wk - weights[image, :])
                with something else?
                thanks again
                oharry

                Comment

                • Gabriel Genellina

                  #9
                  Re: finding euclidean distance,better code?

                  En Fri, 28 Mar 2008 18:15:04 -0300, harryos <oswald.harry@g mail.com>
                  escribió:
                  if i were to calculate the euclidean distance in the above example how
                  should i go about it..?
                  should i replace
                  distance = abs(input_wk - weights[image, :])
                  with something else?
                  For a single 2D vector, math.hypot does what you want. But the above looks
                  like a NumPy array; see this thread


                  --
                  Gabriel Genellina

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: finding euclidean distance,better code?

                    On Fri, 28 Mar 2008 16:59:59 +0100, Robert Bossy wrote:
                    Gabriel Genellina wrote:
                    >That's what I said in another paragraph. "sum of coordinates" is using
                    >a different distance definition; it's the way you measure distance in a
                    >city with square blocks. I don't know if the distance itself has a
                    >name, but
                    I think it is called Manhattan distance in reference of the walking
                    distance from one point to another in this city.
                    You know, there are other cities than Manhattan. Some of them even have
                    streets and blocks.


                    --
                    Steven

                    Comment

                    • Roel Schroeven

                      #11
                      Re: finding euclidean distance,better code?

                      Steven D'Aprano schreef:
                      On Fri, 28 Mar 2008 16:59:59 +0100, Robert Bossy wrote:
                      >
                      >Gabriel Genellina wrote:
                      >>That's what I said in another paragraph. "sum of coordinates" is using
                      >>a different distance definition; it's the way you measure distance in a
                      >>city with square blocks. I don't know if the distance itself has a
                      >>name, but
                      >I think it is called Manhattan distance in reference of the walking
                      >distance from one point to another in this city.
                      >
                      You know, there are other cities than Manhattan. Some of them even have
                      streets and blocks.
                      I'm not sure what your point is. The name of the distance happens to be
                      Manhattan distance (or taxicab distance, rectilinear distance, L1
                      distance, city block distance; see
                      http://en.wikipedia.org/wiki/Manhattan_distance) so Robert has a valid
                      point.


                      --
                      The saddest aspect of life right now is that science gathers knowledge
                      faster than society gathers wisdom.
                      -- Isaac Asimov

                      Roel Schroeven

                      Comment

                      • Steven D'Aprano

                        #12
                        Re: finding euclidean distance,better code?

                        On Sat, 29 Mar 2008 13:06:27 +0100, Roel Schroeven wrote:
                        In any case, I replied because your reaction didn't feel all that gentle
                        to me; to be honest, it felt rather rude.
                        Are you new to Usenet? :-)

                        No offense taken; I hope Robert didn't take offense either, but took the
                        little dig in the spirit it was intended.



                        --
                        Steven

                        Comment

                        Working...