Evaluate a complete clustering experiment
Fit preprocessing on training rows, compare seeds and inspect held out distortion, partition agreement and final cluster sizes on Iris.
// Iris: Fisher, R. (1936), UCI. https://doi.org/10.24432/C56C76
// CC BY 4.0. Full attribution: datasets/iris/README.md.
// Requires 0.0.10-dev.6. This small dataset favors CPU execution.
import aner.dataset
import aner.tensor
import aner.ml
import aner.metrics
import aner.parallel
fn fit_candidate(x: Tensor, k: Int64, seed: Int64, algorithm: Int64,
execution: ParallelContext) -> CentroidModel {
if algorithm == 2 {
return ml.fit_pmeans(x, k, 3.0, seed, execution: execution)
}
if algorithm == 1 {
return ml.fit_omeans(x, k, 3.0, seed, execution: execution)
}
return ml.fit_emeans(x, k, 3.0, seed, execution: execution)
}
fn best_restart(x: Tensor, k: Int64, algorithm: Int64,
execution: ParallelContext) -> CentroidModel {
var best = fit_candidate(x, k, 42, algorithm, execution)
for restart in Range(2) {
let seed = 43 + restart
let candidate = fit_candidate(x, k, seed, algorithm, execution)
if candidate.inertia() < best.inertia() { best = candidate }
}
return best
}
let flowers = dataset.iris()
let parts = flowers.partition(validation: 0.2, test: 0.2, seed: 2026)
let training = parts.train()
let validation = parts.validation()
let testing = parts.test()
print(parts.audit())
print(flowers.citation())
print(flowers.sha256())
print(training.provenance())
print(validation.provenance())
print(testing.provenance())
// Fit the Dataset overload: it rejects fitting held-out roles.
let scaler = dataset.fit_standardizer(training)
let x_train = dataset.transform(scaler, training.features())
let x_validation = dataset.transform(scaler, validation.features())
let x_test = dataset.transform(scaler, testing.features())
let execution = parallel.local(workers: 2)
// Declare k=3 before examining test rows: this example studies the known
// three-species benchmark. Nearby k values are a validation curve, not a rule
// to choose whichever k has the smallest distortion.
for algorithm in Range(3) {
for choice in Range(3) {
let k = 2 + choice
let candidate = best_restart(x_train, k, algorithm, execution)
print(candidate.algorithm())
print(k)
print(candidate.seed())
print("Validation MSE")
print(candidate.score(x_validation, execution: execution) / Float64(validation.rows()))
print("Validation adjusted Rand index")
print(metrics.adjusted_rand_index(candidate.predict(x_validation), validation.targets()))
}
let selected = best_restart(x_train, 3, algorithm, execution)
print(selected.summary())
print(selected.closing_mode())
print(selected.closing_length())
print("Final test MSE: one evaluation per declared algorithm")
print(selected.score(x_test, execution: execution) / Float64(testing.rows()))
print("Final test adjusted Rand index")
print(metrics.adjusted_rand_index(selected.predict(x_test), testing.targets()))
print("Training cluster sizes")
let counts = selected.cluster_sizes()
for cluster in Range(tensor.rows(counts)) { print(tensor.value(counts, cluster, 0)) }
print("First test observation squared distance: not an outlier probability")
let distances = selected.squared_distances(x_test)
print(tensor.value(distances, 0, 0))
}
Make it your experiment.
Use Aner 0.1.0 or newer. Save this program as examples/clustering_workflow.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/clustering_workflow.aner
aner run examples/clustering_workflow.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Requires Aner 0.1.0 or newer and its bundled Iris dataset. Compares e-means, o-means and experimental p-means with CPU Float64 and a budget of two local workers. No external service, credentials, network download or output file is required.
aner check examples/clustering_workflow.aner
aner run examples/clustering_workflow.anerBundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution