Skip to contents

k-means clustering via Lloyd's algorithm, k-means++ seeded (Geron Ch 8, morie.fn hmkmn)

Usage

morie_geron_kmeans(
  X,
  n_clusters,
  seed = 0,
  max_iter = 300,
  tol = 1e-10,
  n_init = 10
)

Arguments

X

Data.

n_clusters

Clusters.

seed

Base seed.

max_iter

Iteration cap.

tol

Centre-shift tolerance.

n_init

Restarts.

Value

List with labels, centers, inertia, n_iter, distances, counts, estimate, n, method.

Examples

morie_geron_kmeans(X = c(1, 2, 3, 4, 5, 6, 7, 8), n_clusters = 5L)
#> $labels
#> [1] 0 0 0 2 3 1 4 4
#> 
#> $centers
#>      [,1]
#> [1,]  2.0
#> [2,]  6.0
#> [3,]  4.0
#> [4,]  5.0
#> [5,]  7.5
#> 
#> $inertia
#> [1] 2.5
#> 
#> $n_iter
#> [1] 2
#> 
#> $distances
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    5    3    4  6.5
#> [2,]    0    4    2    3  5.5
#> [3,]    1    3    1    2  4.5
#> [4,]    2    2    0    1  3.5
#> [5,]    3    1    1    0  2.5
#> [6,]    4    0    2    1  1.5
#> [7,]    5    1    3    2  0.5
#> [8,]    6    2    4    3  0.5
#> 
#> $counts
#> [1] 3 1 1 1 2
#> 
#> $estimate
#> [1] 2.5
#> 
#> $n
#> [1] 8
#> 
#> $method
#> [1] "k-means (Lloyd's algorithm, k-means++ seeded)"
#>