Qu

kmedians_model

Builtin index · Statistics & ML

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

Input: X, k, seed · Output: result

What it is

The outlier-resistant sibling of kmeans_model, same X/k/seed= shape: clusters by L1 distance around coordinate-wise median centres instead of means.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = kmedians_model(X, k, [seed=])
X —not documented yet
k —not documented yet
seed optionalnot documented yet
result Record
Returns a fitted model Record with a .predict of the same shape as kmeans_model.

Examples

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

km = kmedians_model(G, 3, seed = 5)
kd = kmedoids_model(G, 3, seed = 5)
gm = gmm_model(G, 3, seed = 5)
print("kmedians label for row 0: {km.predict(G)[0]}")
print("kmedoids label for row 0: {kd.predict(G)[0]}")
print("gmm log-likelihood: {round(gm.log_likelihood, 1)}")

gb = gradient_boosting_model(X, y, 20, learning_rate = 0.1, max_depth = 3)
print("gradient boosting r2: {round(gb.score(X, y), 3)}")

sv = svm_model(Xc, yc, kernel = "linear", C = 1.0)
lg = logistic_model(Xc, yc)
print("svm accuracy:      {round(sv.score(Xc, yc), 3)}")
print("logistic accuracy: {round(lg.score(Xc, yc), 3)}")

svr = svr_model(X, y)
print("svr r2: {round(svr.score(X, y), 3)}")
output
kmedians label for row 0: 1
kmedoids label for row 0: 1
gmm log-likelihood: [-448.7, -448.7, -448.7, -448.7]
gradient boosting r2: 0.953
svm accuracy:      0.995
logistic accuracy: 0.995
svr r2: 0.596

from statistics-ml

How it works

kmedians_model(X, k, [seed=]) — kmeans_model's robust-to- outliers sibling: reuses kmeans_fit's overall assign/update/ repeat-to-convergence loop shape (see kmedians_fit's own doc comment), but swaps BOTH the pieces that actually make it "k-medians" rather than a relabeled k-means — L1 (Manhattan) distance for cluster assignment instead of L2, and the coordinate-wise MEDIAN of each cluster's points for the center update instead of the mean. The median being far less sensitive to a single extreme point than the mean is the for a numeric side-by-side against kmeans_model on data with a planted outlier.

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