Qu

kmeans

Builtin index · Statistics & ML

result = kmeans(X, k, [seed=])

Input: X, k, seed · Output: result

What it is

Lloyd's algorithm on an N-row, D-column data matrix X, partitioning it into the integer number of clusters k, starting from randomly chosen initial centroids (an optional integer seed= fixes the draw).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = kmeans(X, k, [seed=])
X matrix or numbernot documented yet
k matrix or numbernot documented yet
seed matrix or number, optionalnot documented yet
result vector
Returns a length-N vector of integer cluster labels in 0..k-1, one per row of X.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

G = vstack(randn(40, 2, seed = 31) + 4,
           randn(40, 2, seed = 32) - 4,
           randn(40, 2, seed = 33))
labels = kmeans(G, 3, seed = 5)
centers = kmeans_centers(G, 3, seed = 5)
xs = G[:, 0]
ys = G[:, 1]
for k in 0 to 2
    inside = labels == k
    print("cluster {k}: {len(xs[inside])} points")
    scatter(xs[inside], ys[inside], label = "cluster {k}")
end for
scatter(centers[:, 0], centers[:, 1], marker = "x", color = "#0f172a", size = 12, label = "centroids")
legend()
title("kmeans, k = 3")
output
cluster 0: 40 points
· scatter: figure 6 (40 points)
cluster 1: 39 points
· scatter: figure 7 (39 points)
cluster 2: 41 points
· scatter: figure 8 (41 points)
· scatter: figure 9 (3 points)
-6 -4 -2 0 2 4 6 0 50 100 150 200 250 300 350 400 450 500 550 x=-1.058, y=-0.826 x=0.594, y=0.636 x=0.88, y=1.305 x=-0.379, y=-0.489 x=0.152, y=1.215 x=1.407, y=0.754 x=0.231, y=-0.942 x=-1.291, y=-0.12 x=-0.085, y=-0.627 x=0.304, y=-0.101 x=0.037, y=0.004 x=-0.705, y=-0.429 x=0.241, y=-0.559 x=-0.582, y=-1.167 x=-0.277, y=-0.685 x=-1.636, y=-1.342 x=-0.865, y=-0.799 x=0.131, y=0.029 x=-1.581, y=-1.773 x=-2.048, y=-2.111 x=2.947, y=2.32 x=-0.456, y=-1.875 x=0.908, y=0.097 x=0.85, y=1.1 x=0.517, y=0.032 x=0.003, y=0.246 x=-0.201, y=0.212 x=0.466, y=-0.074 x=0.426, y=1.096 x=-0.066, y=-0.164 x=1.021, y=1.029 x=0.113, y=0.035 x=-0.998, y=-0.494 x=-0.066, y=-0.991 x=2.823, y=0.731 x=-0.046, y=-0.746 x=-0.029, y=-0.9 x=-0.031, y=0.196 x=-0.616, y=0.539 x=-1.349, y=-0.313 x=-1.085, y=-1.65 x=-1.452, y=-1.608 x=0.973, y=0.867 x=0.737, y=0.242 x=-0.185, y=0.468 x=1.175, y=1.485 x=-1.836, y=-0.822 x=-0.157, y=-0.076 x=0.407, y=0.307 x=-1.365, y=-1.022 x=-0.847, y=1.143 x=-0.762, y=0.021 x=-1.044, y=-0.505 x=0.478, y=1.123 x=0.316, y=-0.102 x=0.965, y=0.825 x=-0.751, y=-0.957 x=-0.988, y=-0.033 x=-1.22, y=-0.385 x=-0.681, y=-0.432 x=-0.621, y=0.256 x=0.918, y=0.744 x=1.182, y=-0.477 x=-0.039, y=0.425 x=0.585, y=-0.031 x=0.297, y=-0.535 x=0.112, y=-0.357 x=0.039, y=0.8 x=0.16, y=0.201 x=1.261, y=-0.955 x=0.95, y=0.238 x=-0.013, y=0.393 x=-1.577, y=-0.343 x=-0.67, y=-0.101 x=-0.631, y=-1.302 x=-0.84, y=-0.949 x=1.692, y=0.99 x=0.389, y=0.473 x=-0.261, y=-0.509 x=-0.491, y=-0.033 x=0.634, y=0.806 x=-0.342, y=0.622 x=-1.674, y=-1.63 x=0.039, y=-0.203 x=0.175, y=-0.688 x=0.206, y=0.293 x=1.405, y=0.994 x=-0.644, y=-0.672 x=-0.623, y=-0.7 x=1.569, y=0.384 x=-0.533, y=-0.154 x=0.682, y=-0.014 x=1.089, y=1.13 x=0.342, y=0.984 x=1.161, y=0.991 x=-0.232, y=0.289 x=-0.607, y=-0.737 x=0.295, y=0.07 x=0.275, y=0.25 x=-1.141, y=-0.456 x=0.461, y=0.554 x=-0.671, y=-0.906 x=0.513, y=1.154 x=0.499, y=-0.224 x=1.753, y=1.474 x=0.103, y=0.61 x=-0.351, y=0.258 x=0.553, y=-0.788 x=0.298, y=0.904 x=0.472, y=0.843 x=-0.94, y=-0.939 x=-1.062, y=-1.629 x=0.467, y=0.806 x=-0.653, y=-1.083 x=1.172, y=1.15 x=0.206, y=0.15 x=-1.077, y=-0.827 x=0.153, y=-0.987 x=0.202, y=0.439 x=0.011, y=-0.385 x=-0.67, y=0.029 x=1.367, y=0.502 x=0.955, y=0.531 x=0.352, y=0.238 x=1.543, y=0.703 x=-1.029, y=-0.474 x=0.104, y=0.508 x=0.877, y=-0.209 x=-1.366, y=-1.921 x=-1.583, y=-1.15 x=1.748, y=0.901 x=0.359, y=0.563 x=0.309, y=-0.002 x=1.322, y=1.929 x=-0.57, y=-0.79 x=-0.857, y=0.127 x=0.177, y=-0.37 x=-1.161, y=-1.563 x=0.371, y=1.281 x=-0.811, y=-1.39 x=0.168, y=1.089 x=-0.599, y=0.823 x=-0.452, y=-1.067 x=-1.241, y=-1.344 x=-0.711, y=-0.72 x=-0.233, y=-0.53 x=-0.344, y=0.761 x=1.152, y=0.27 x=0.989, y=0.896 x=0.308, y=0.67 x=-1.851, y=-1.723 x=0.866, y=0.926 x=-0.586, y=0.32 x=-0.204, y=-0.76 x=1.617, y=1.863 x=-0.897, y=-1.095 x=1.375, y=0.119 x=0.027, y=0.911 x=-0.268, y=-0.958 x=-0.58, y=-0.581 x=1.263, y=0.222 x=0.021, y=0.109 x=0.228, y=-0.664 x=0.002, y=-0.26 x=-0.301, y=0.237 x=0.646, y=0.863 x=0.272, y=-1.038 x=-0.432, y=-0.686 x=-0.123, y=1.22 x=0.844, y=1.163 x=-1.237, y=-1.157 x=2, y=1.132 x=-0.836, y=0.309 x=-0.332, y=-0.738 x=0.17, y=0.198 x=-0.327, y=0.587 x=-1.737, y=-1.131 x=-0.054, y=0.877 x=0.317, y=0.75 x=-1.406, y=-1.905 x=0.04, y=0.092 x=1.456, y=0.878 x=0.373, y=0.985 x=-0.912, y=-0.551 x=-0.104, y=0.042 x=0.349, y=0.549 x=1.098, y=0.24 x=0.588, y=-0.011 x=-0.164, y=-0.854 x=0.235, y=-0.679 x=-0.713, y=0.139 x=0.252, y=-0.404 x=-0.029, y=-0.281 x=-0.988, y=-0.818 x=0.223, y=-0.99 x=-0.591, y=0.839 x=-0.36, y=-0.088 x=-0.006, y=-0.156 x=-0.054, y=-0.158 x=0.644, y=-0.111 x=-3, y=-4.018 x=-2.75, y=-6.388 x=-2.5, y=-5.262 x=-2.25, y=0.293 x=-2, y=1.587 x=-1.75, y=1.194 x=-1.5, y=1.829 x=-1.25, y=2.719 x=-1, y=-0.145 x=-0.75, y=0.779 x=-0.5, y=-0.837 x=-0.25, y=2.101 x=0, y=0.378 x=0.25, y=1.148 x=0.5, y=1.019 x=0.75, y=-3.978 x=1, y=-3.731 x=1.25, y=-0.191 x=1.5, y=-1.178 x=1.75, y=-2.55 x=2, y=-0.452 x=2.25, y=4.657 x=2.5, y=0.591 x=2.75, y=5.418 x=3, y=6.826 x=-4.221, y=-3.675 x=-4.749, y=-4.599 x=-4.378, y=-5.049 x=-4.908, y=-2.938 x=-3.111, y=-4.705 x=-4.7, y=-3.185 x=-4.948, y=-4.24 x=-5.087, y=-4.991 x=-3.755, y=-2.658 x=-4.866, y=-3.893 x=-3.202, y=-4.799 x=-3.536, y=-4.59 x=-4.643, y=-4.655 x=-4.223, y=-3.8 x=-4.771, y=-4.791 x=-5.162, y=-3.624 x=-2.644, y=-3.501 x=-5.565, y=-4.823 x=-3.561, y=-2.64 x=-4.529, y=-4.524 x=-5.6, y=-5.042 x=-3.048, y=-3.184 x=-4.283, y=-4.206 x=-5.239, y=-4.032 x=-2.871, y=-5.062 x=-2.117, y=-2.96 x=-3.794, y=-3.873 x=-3.245, y=-4.153 x=-4.242, y=-4.691 x=-3.935, y=-4.681 x=-2.991, y=-2.757 x=-4.498, y=-3.651 x=-4.482, y=-5.712 x=-3.324, y=-4.723 x=-5.062, y=-5.077 x=-4.34, y=-4.914 x=-4.945, y=-5.731 x=-4.523, y=-3.545 x=-2.907, y=-3.457 x=-4.38, y=-3.659 x=3.22, y=4.412 x=3.089, y=4.062 x=2.827, y=3.584 x=3.848, y=5.345 x=4.196, y=4.669 x=3.111, y=4.336 x=2.961, y=3.91 x=5.036, y=4.596 x=5.339, y=4.031 x=4.874, y=2.505 x=4.357, y=2.791 x=3.318, y=5.241 x=2.544, y=3.948 x=6.731, y=3.889 x=4.101, y=4.456 x=3.18, y=2.902 x=4.415, y=7.292 x=3.9, y=6.008 x=3.342, y=4.252 x=2.138, y=4.074 x=3.304, y=4.752 x=4.53, y=4.152 x=3.264, y=4.913 x=3.885, y=2.94 x=5.542, y=3.917 x=1.488, y=3.061 x=2.847, y=3.236 x=3.93, y=3.488 x=2.913, y=2.598 x=3.839, y=4.13 x=2.508, y=3.961 x=2.846, y=4.195 x=4.278, y=4.197 x=4.971, y=5.339 x=4.568, y=5.297 x=3.797, y=3.895 x=3.459, y=4.494 x=4.285, y=3.369 x=3.836, y=2.999 x=2.123, y=1.312 x=-0.678, y=-0.737 x=-0.734, y=0.333 x=-0.016, y=1.204 x=0.605, y=0.633 x=0.426, y=-0.995 x=-0.383, y=0.403 x=-0.064, y=0.253 x=-1.862, y=-0.361 x=0.298, y=0.52 x=-1.481, y=-1.025 x=0.719, y=0.808 x=1.151, y=-0.726 x=-0.082, y=-0.357 x=-0.401, y=0.791 x=-0.182, y=0.737 x=0.394, y=0.021 x=0.954, y=0.671 x=0.405, y=0.336 x=-1.617, y=-0.65 x=-0.669, y=-1.147 x=-2.452, y=1.586 x=1.709, y=-0.65 x=0.122, y=-0.171 x=1.066, y=-1.195 x=0.053, y=-0.57 x=-0.396, y=-0.235 x=0.819, y=-0.788 x=0.012, y=0.012 x=-2.723, y=0.397 x=1.545, y=0.484 x=-1.119, y=0.98 x=-0.22, y=-0.804 x=-1.564, y=-1.293 x=-1.811, y=-0.276 x=0.774, y=-1.055 x=0.575, y=1.214 x=-0.348, y=-1.078 x=0.015, y=-0.404 x=0.245, y=-1.466 x=0.436, y=1.71 kmeans, k = 3 a b measured cubic fit measured forecast cluster 0 cluster 1 cluster 2 centroids

from statistics-ml

How it works

kmeans(X, k, seed=0) — Lloyd's algorithm with random (not k-means++) initial centroids, for a simple, deterministic first cut; returns just the cluster label per row (0..k-1) — kmeans_centers reruns the same deterministic computation (same seed => same result) to hand back the centroid matrix, since Qu has no tuple/multi-return value to carry both back from one call, and cheaply recomputing beats inventing one.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.