Skip to content

Instantly share code, notes, and snippets.

@MSHelm
Last active May 7, 2023 13:01
Show Gist options
  • Save MSHelm/aed5635263a0ec05feebfae8b01b13c2 to your computer and use it in GitHub Desktop.
Save MSHelm/aed5635263a0ec05feebfae8b01b13c2 to your computer and use it in GitHub Desktop.
my_kmeans <- function(data, k, n_iterations) {
# Helper function for euclidean distance
euclidean_distance <- function(p1, p2) {
dist <- sqrt(sum((p1-p2)^2))
return(dist)
}
# Initialize centers randomly
centers <- df[sample(nrow(df), k, replace = FALSE), ]
# Perform n iterations
iteration <- 1
while(iteration < n_iterations) {
# Calculate distance of each point to each center
distances <- matrix(Inf, nrow = nrow(df), ncol = k)
for (i in seq_len(nrow(df))) {
for (j in seq_len(k)) {
distances[i, j] <- euclidean_distance(df[i, ], centers[j, ])
}
}
# Assign each point to the closest center
cluster_id <- apply(distances, 1, which.min)
# Calculate new centers
for (i in seq_len(k)) {
this_cluster <- df[cluster_id == i,]
centers[k, ] <- colMeans(this_cluster)
}
iteration <- iteration + 1
}
cluster_id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment