Tools for working with geographic data
e-n-f / geotools Goto Github PK
View Code? Open in Web Editor NEWTools for working with geographic data
Tools for working with geographic data
Hi, I was reading your code the other day because I was interested to see an Hamerly's implementation of the Kmeans in java.
But after reading it I spotted a few mistakes in your code that you might take a look at.
Based on the spotted errors I will show you an example which proves that it doesn't work with your code:
Let's take the points represented on the graph (8 points, in two dimensions)
Impose the centers as : (7.5;6.5) and (11.5;4.5) (close to each other in the upper right part of the graph)
Now let's start your algorithm
Output (I changed the printing form):
Center of cluster = [4.0, 4.166666666666667]
Point number 0: [1.0, 1.5]
Point number 1: [3.5, 2.0]
Point number 2: [2.0, 4.0]
Point number 3: [1.0, 6.5]
Point number 4: [7.5, 6.5]
Point number 5: [9.0, 4.5]
Center of cluster = [11.0, 5.5]
Point number 0: [11.5, 4.5]
Point number 1: [10.5, 6.5]
Here it is clear that you have a problem since the right answer should obviously return 4 points in each cluster especially with these two centers coordinates at the end.
Let's take a step back:
In fact, you use the squared euclidean distance everywhere. It's actually a good choice since you avoid computing square roots here and there. At first sight you only need to compare distances and as long as you only need to compare distances keeping these are fine from a mathematical point of view.
However, it is really important to keep in mind that in the Hamerly's optimization (with the two boundaries) you also ADD and SUBSTRACT distances and then compare them, that's where the nightmare begins and where you have to be careful.
If you take the pseudo-codes on which your implementation is based on, the errors can be pointed out at line 6 of the Kmeans(dataset x, initial centers c) function
Here: m = max(s(a(i))/2, l(i))
and at line 4,6,8 of the Update-Bounds(p,a,u,l) function
Here : u(i) = u(i) + p(a(i))
Here : l(i) = l(i) - p(r')
Here : l(i) = l(i) - p(r)
The solution if you want to keep your squared euclidean distance mechanism as it is (what I recommend you to keep) then, you have to edit the lines I mentioned above.
In the k-means function
final double m = Math.max(s[a[i]] * 0.5f, l[i]);
As we apply the square on the coefficient. It becomes
final double m = Math.max(s[a[i]] * 0.25f, l[i]);
In the update-bounds function:
u[i] += p[a[i]];
Becomes
double final sumSqrt = Math.sqrt(u[i]) + Math.sqrt(p[a[i]]);
u[i] = sumSqrt * sumSqrt;
And
l[i] -= p[rp];
Becomes
double final diffSqrt = Math.sqrt(l[i]) - Math.sqrt(p[rp]);
l[i] = diffSqrt * diffSqrt;
And
l[i] -= p[r];
Becomes
double final diffSqrt = Math.sqrt(l[i]) - Math.sqrt(p[r]);
l[i] = diffSqrt * diffSqrt;
And the final output is now correct
Center of cluster = [1.875, 3.5]
Point number 0: [1.0, 1.5]
Point number 1: [3.5, 2.0]
Point number 2: [2.0, 4.0]
Point number 3: [1.0, 6.5]
Center of cluster = [9.625, 5.5]
Point number 0: [7.5, 6.5]
Point number 1: [9.0, 4.5]
Point number 2: [11.5, 4.5]
Point number 3: [10.5, 6.5]
I hope I am not mistaken and that it'll help people that may use your code !
I'm open to any questions regarding these modifications
Have a nice day.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.