On this line, a data point is assigned to the centroids array by reference:
This means that as the centroids change later in the algorithm, the underlying data array is also changed. This is undesirable for two reasons: mutating the data array which is passed in could have downstream effects, and even if the data array isn't used subsequently, it makes the algorithm incorrect.
Assuming I'm not missing something here (very possible, as I am not a JS developer), data[idx].slice() would be the fix I would suggest.
On this line, a data point is assigned to the centroids array by reference:
skmeans/main.js
Line 60 in 17aaa1b
This means that as the centroids change later in the algorithm, the underlying data array is also changed. This is undesirable for two reasons: mutating the data array which is passed in could have downstream effects, and even if the data array isn't used subsequently, it makes the algorithm incorrect.
Assuming I'm not missing something here (very possible, as I am not a JS developer),
data[idx].slice()would be the fix I would suggest.