You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@mahout.apache.org by WangRamon <ra...@hotmail.com> on 2011/11/04 15:58:01 UTC
Can anybody explain the distance method in
SquaredEuclideanDistanceMeasure?
Hi All I'm reading the code of SquaredEuclideanDistanceMeasure, the "distance(double centroidLengthSquare, Vector centroid, Vector v)" method confused me a lot, i don't know why we choose this expression "centroidLengthSquare - 2 * v.dot(centroid) + v.getLengthSquared()" to calculate the distance? What is the best case to use this Measure? BTW, I think it would be great if we can have a wiki or some detail user guide or more comments in the source code to explain each distance measure implementation, that will really help, thanks guys. Cheers Ramon
Re: Can anybody explain the distance method in SquaredEuclideanDistanceMeasure?
Posted by Ted Dunning <te...@gmail.com>.
2011/11/4 Grant Ingersoll <gs...@apache.org>
> > Hi All I'm reading the code of SquaredEuclideanDistanceMeasure, the
> "distance(double centroidLengthSquare, Vector centroid, Vector v)" method
> confused me a lot, i don't know why we choose this expression
> "centroidLengthSquare - 2 * v.dot(centroid) + v.getLengthSquared()" to
> calculate the distance?
>
> Math is as Sebastian said, the purpose is that it can save some steps in
> the computation, for instance, see the KMeans implementation where you are
> often calculating the distance between some point and a centroid. We have
> the centroidLengthSquare handy, so it saves a fair amount of work.
Grant is right, but he is soft-pedaling the savings.
c^2 is known ahead. v is very, very sparse. Thus c v is very sparse and
has the same sparsity pattern as v^2 (which can even be cached). On the
other hand c-v is much less sparse.
The difference looks minor when you first write it down, but it can easily
be a factor of 1000 or more.
Re: Can anybody explain the distance method in SquaredEuclideanDistanceMeasure?
Posted by Grant Ingersoll <gs...@apache.org>.
On Nov 4, 2011, at 10:58 AM, WangRamon wrote:
>
>
>
>
> Hi All I'm reading the code of SquaredEuclideanDistanceMeasure, the "distance(double centroidLengthSquare, Vector centroid, Vector v)" method confused me a lot, i don't know why we choose this expression "centroidLengthSquare - 2 * v.dot(centroid) + v.getLengthSquared()" to calculate the distance?
Math is as Sebastian said, the purpose is that it can save some steps in the computation, for instance, see the KMeans implementation where you are often calculating the distance between some point and a centroid. We have the centroidLengthSquare handy, so it saves a fair amount of work.
> What is the best case to use this Measure? BTW, I think it would be great if we can have a wiki or some detail user guide or more comments in the source code to explain each distance measure implementation, that will really help, thanks guys.
That would be a great addition! Also, javadoc would be helpful, so patches would be great there.
--------------------------------------------
Grant Ingersoll
http://www.lucidimagination.com
RE: Can anybody explain the distance method in
SquaredEuclideanDistanceMeasure?
Posted by WangRamon <ra...@hotmail.com>.
haha, thanks for the math, i almost forget
> Date: Fri, 4 Nov 2011 16:01:02 +0100
> From: ssc@apache.org
> To: user@mahout.apache.org
> Subject: Re: Can anybody explain the distance method in SquaredEuclideanDistanceMeasure?
>
> c = centroid
> v = vector
>
> (c - v)^2 = c^2 - 2cv + v^2
>
> On 04.11.2011 15:58, WangRamon wrote:
> >
> >
> >
> >
> > Hi All I'm reading the code of SquaredEuclideanDistanceMeasure, the "distance(double centroidLengthSquare, Vector centroid, Vector v)" method confused me a lot, i don't know why we choose this expression "centroidLengthSquare - 2 * v.dot(centroid) + v.getLengthSquared()" to calculate the distance? What is the best case to use this Measure? BTW, I think it would be great if we can have a wiki or some detail user guide or more comments in the source code to explain each distance measure implementation, that will really help, thanks guys. Cheers Ramon
Re: Can anybody explain the distance method in SquaredEuclideanDistanceMeasure?
Posted by Sebastian Schelter <ss...@apache.org>.
c = centroid
v = vector
(c - v)^2 = c^2 - 2cv + v^2
On 04.11.2011 15:58, WangRamon wrote:
>
>
>
>
> Hi All I'm reading the code of SquaredEuclideanDistanceMeasure, the "distance(double centroidLengthSquare, Vector centroid, Vector v)" method confused me a lot, i don't know why we choose this expression "centroidLengthSquare - 2 * v.dot(centroid) + v.getLengthSquared()" to calculate the distance? What is the best case to use this Measure? BTW, I think it would be great if we can have a wiki or some detail user guide or more comments in the source code to explain each distance measure implementation, that will really help, thanks guys. Cheers Ramon