You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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
finaldoublem = Math.max(s[a[i]] * 0.5f, l[i]);
As we apply the square on the coefficient. It becomes
You should also consider using Mathf.max for the difference between two square root. Otherwise you could get a negative value and square it which falses the lower bound.
Be careful while calculating the new center as you might divide by zero if the cluster is empty.
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)
![Mon image](https://camo.githubusercontent.com/9b038ca08bf3c602cca7d04d349b02ca0fa471830825a0828f832471503f3509/68747470733a2f2f6e736133392e636173696d616765732e636f6d2f696d672f323031382f30352f32332f6d696e695f3138303532333034323134313130363032362e706e67)
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):
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
As we apply the square on the coefficient. It becomes
In the update-bounds function:
Becomes
And
Becomes
And
Becomes
And the final output is now correct
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.
The text was updated successfully, but these errors were encountered: