fix: count k-means cluster sizes once per point - #397
Open
spokodev wants to merge 1 commit into
Open
Conversation
In the k-means path of `limits()`, `clusterSizes[best]++` and
`assignments[i] = best` sat inside the inner nearest-centroid loop
(`for j`), so for every point the tally was incremented up to `num` times
using the intermediate best-so-far index. The centroid update then divides
each cluster's value-sum by that corrupted count
(`newCentroids[j] *= 1 / clusterSizes[j]`), so the "centroids" are not the
cluster means and k-means converges to wrong breaks:
chroma.limits([0,1,2, 50,51,52, 100,101,102], 'k', 3)
// [0, 2, 102] -> the middle cluster {50,51,52} is dropped
// expected [0, 2, 52, 102]
Move the tally and assignment out of the inner loop so the winning cluster
is counted exactly once per point, after the argmin over j.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
chroma.limits(data, 'k', n)(k-means classification) computes wrong class breaks:In the assignment step,
clusterSizes[best]++andassignments[i] = bestare inside the inner nearest-centroid loop (for (let j = 0; j < num; j++)), so for each point the tally is incremented up tonumtimes using the intermediate best-so-far index. The centroid-update step then doesnewCentroids[j] *= 1 / clusterSizes[j], dividing each cluster's value-sum by a corrupted count, so the "centroids" are not the cluster means and k-means converges to wrong clusters. Across 200 random datasets the output differs from correct 1-D k-means on ~168.Fix
Move the tally and assignment out of the inner loop, so the winning cluster is counted exactly once per point after the argmin over
j.newCentroids[j] = sum / clusterSizes[j]is then the true cluster mean.Tests
Added a
limits()case asserting three well-separated clusters yield[0, 2, 52, 102]. Fails onmain, passes with the fix. Full suite green (2520/2520), prettier + eslint clean.