Skip to content

Instantly share code, notes, and snippets.

@davidski
Created April 23, 2014 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidski/11201851 to your computer and use it in GitHub Desktop.
Save davidski/11201851 to your computer and use it in GitHub Desktop.
simpleKMeans problems
> simpleKMeans()
[,1] [,2]
[1,] -0.0520000 -1.8500000
[2,] -2.0040000 -0.8500000
[3,] 0.1413699 0.1849315
simpleKMeans <- function(points = testPoints, centroids = testCentroids)
{
# Get ridiculous values for the initial cluster ids
oldClusterID <- -1
# Determine the number of centroids
numCentroids <- nrow(centroids)
# repeat the processes using a loop of no more than 20 iterations
for (n in 1:20)
{
# For each point find its closest cluster centre aka centroid
clusterID <- simpleAssignToCentroids(points, centroids)
# Plot the points and centroids
# plotClustering(points, centroids, clusterID)
# If there was no change in cluster assignments, then stop
if (length(setdiff(clusterID, oldClusterID)) == 0) { break }
# For each cluster of points determine its centroid
centroids <- simpleDetermineCentroids(points, clusterID);
# remember clusterID before clusterID is re-assigned in the next iteration
oldClusterID <- clusterID
}
# Return the centroids
centroids
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment