Created
April 23, 2014 03:22
-
-
Save davidski/11201851 to your computer and use it in GitHub Desktop.
simpleKMeans problems
This file contains 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
> simpleKMeans() | |
[,1] [,2] | |
[1,] -0.0520000 -1.8500000 | |
[2,] -2.0040000 -0.8500000 | |
[3,] 0.1413699 0.1849315 |
This file contains 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
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