I'm trying to write a k-means function in javascript. And here is my code.
function kmeans(arrayToProcess,cluster_n){
var pointDimension = arrayToProcess[0].length;
var ClusterResult = new Array();
var ClusterCenter = new Array();
var oldClusterCenter = new Array();
var changed=false;
for(var i = 0;i<cluster_n;i++)
ClusterCenter.push(arrayToProcess[randomInt(arrayToProcess.length-1)]);
console.log(ClusterCenter);
// do{
for(var k=0;k<50;k++){//loop
for(var i = 0; i<cluster_n; i++){
ClusterResult[i] = new Array();
}
for(var i = 0; i<arrayToProcess.length; i++){
//for every point element
var oldDistance=-1;
var newClusterNumber = 0;
for(var j = 0; j<cluster_n; j++){
//for every cluster
var distance = Math.abs(computeDistanceBetween(arrayToProcess[i], ClusterCenter[j]));
if (oldDistance == -1){
oldDistance = distance;
newClusterNumber = j;
}else if ( distance <= oldDistance ){
newClusterNumber = j;
oldDistance = distance;
}
}
ClusterResult[newClusterNumber].push(arrayToProcess[i]);
}
oldClusterCenter = ClusterCenter;
//compute new centroid
for(var i = 0; i<cluster_n; i++){
newCentroid = pinit(pointDimension);
for(var j = 0; j<ClusterResult[i].length; j++){
newCentroid = padd(ClusterResult[i][j], newCentroid);
}
ClusterCenter[i] = pdivide(newCentroid, ClusterResult[i].length);
}
changed=false;
for(var i = 0; i<cluster_n; i++){
if(!pequal(ClusterCenter[i],oldClusterCenter[i]))
changed = true;
}
}//while (changed == true);
return ClusterResult;
}
function computeDistanceBetween(a,b){
var result = 0;
for(var i = 0; i<a.length;i++) result += a[i] * b[i];
return result;
}
function pinit(n){
var result = new Array(n);
for(var i=0;i<n;i++) result[i] = 0;
return result;
}
function padd(a,b){
var result = new Array(a.length);
for(var i = 0; i<a.length;i++) result[i] = a[i] + b[i];
return result;
}
function pdivide(a,d){
var result = new Array(a.length);
for(var i = 0; i<a.length;i++) result[i] = a[i] / d;
return result;
}
function pequal(a,b){
for(var i = 0; i<a.length;i++)
if(a[i] != b[i]) return false;
return true;
}
function randomInt(max){
return randomIntBetween(0,max);
}
function randomIntBetween(min,max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
If I stop the for-loop(k<0), the console gives the right answer. But if I start the for-loop(k<1),the array ClusterCenter will always has some NaN items. How dose the NaN appear?
Edit: Further explanation: if the for-loop in the 14th line has been executed, the ClusterCenter above will give some NaN items.Why?
Example input
var testArray = new Array();
for(var i=0; i<100; i++) testArray.push([randomInt(-150,150),randomInt(-150,150)]);
kmeans(testArray,4);
the ClusterCenter above will give some NaN items.Why?
Because you're diving zero by zero, which is not a number. This does happen for every empty cluster in the ClusterResult
- it will create ClusterCenter[i] = pdivide(pinit(pointDimension), 0);
.
How to deal with empty clusters? Possible strategies I could think of would be to make 0/0 = 0
, to choose a new random cluster center, or to drop the cluster alltogether ( cluster_n--
).
But why do you get so many empty clusters in the first place? Because your computeDistanceBetween
function is seriously flawed. Every (non-0|0) point is distant from itself . Choose a more reasonable distance function, like euclidian distance. It should always return a positive number, rendering the Math.abs
in the loop superflouos.
Some other points:
newCentroid
misses a var
statement and leaks into global scope Your changed
is flawed. When setting oldClusterCenter = ClusterCenter
, both variables will hold the same array that is then mutated. Not only is pequal(ClusterCenter[i],oldClusterCenter[i])
always true, but even ClusterCenter[i]===oldClusterCenter[i]
because of oldClusterCenter === ClusterCenter
.
To fix this, either make oldClusterCenter = ClusterCenter.slice()
or introduce ClusterCenter = new Array(cluster_n);
after the assignment.
Your code for computing the nearest cluster could be simplified to
var newClusterNumber = 0, oldDistance = computeDistanceBetween(arrayToProcess[i], ClusterCenter[0])); for (var j=1; j<cluster_n; j++) { var distance = computeDistanceBetween(arrayToProcess[i], ClusterCenter[j]); if (distance <= oldDistance) { newClusterNumber = j; oldDistance = distance; } }
or
var onewClusterNumber, ldDistance=Infinity; for (var j=0; j<cluster_n; j++) { var distance = computeDistanceBetween(arrayToProcess[i], ClusterCenter[j]); if (distance <= oldDistance) { newClusterNumber = j; oldDistance = distance; } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.