Qu

kmeans_model

Builtin index · Statistics & ML

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

Input: X, k, seed · Output: result

What it is

k-means as a reusable handle: fits on an N-row, D-column matrix X into k clusters (optional integer seed=), so .predict(Xnew) later assigns new rows to the nearest centroid rather than only labelling the fitting data.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = kmeans_model(X, k, [seed=])
X matrixnot documented yet
k matrixnot documented yet
seed matrix, optionalnot documented yet
result Record or vector
Returns a fitted model Record whose .predict gives a length-M integer label vector for an M-row Xnew.

Examples

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

r = ridge_model(X, y, 0.1)           # least squares with L2 regularization

k = kmeans_model(X, 2, seed = 42)    # .predict assigns to the nearest centroid

p = pca_model(X, 1)                  # .predict projects onto the fitted components

print("cluster of row 0: {k.predict(X)[0]}")
output
cluster of row 0: 1

from specialized

Gs = vstack(randn(20, 2, seed = 64) + 4, randn(20, 2, seed = 65) - 4)
km = kmeans_model(Gs, 2, seed = 5)
print("kmeans score is negative inertia: {round(km.score(Gs), 4)}")
output
kmeans score is negative inertia: -59.3334

from statistics-ml

How it works

kmeans_model(X, k, seed=) — wraps kmeans_fit's labels/centers in a handle so .predict(Xnew) (nearest- centroid assignment) and .score(X) (negative inertia, sklearn's convention: higher/less-negative is better) work on new data, which the flat kmeans/kmeans_centers can't do — those only ever describe the training set they were called on.

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