All examples
Machine learning

KNN native parallelism and distance weighting

Compare unchanged uniform defaults with exact inverse distance votes and reusable local CPU query execution.

knn_parallel.aner
import aner.ml
import aner.parallel
import aner.tensor

let features = [[1.0], [2.0], [3.0]]
let labels = [[10.0], [20.0], [20.0]]
let queries = [[0.0], [1.0]]
let uniform = ml.fit_knn(features, labels, 3)
let weighted = ml.fit_knn(features, labels, 3, weights: "distance")
let execution = parallel.local()
let serial = ml.predict(weighted, queries)
let concurrent = ml.predict(weighted, queries, execution: execution,
    max_workspace_bytes: 64 * 1024 * 1024)
print(tensor.value(ml.predict(uniform, queries), 0, 0))
print(tensor.value(serial, 0, 0))
print(tensor.value(concurrent, 0, 0))
print(tensor.value(concurrent, 1, 0))
let probabilities = ml.predict_proba(weighted, queries, execution: execution)
print(tensor.value(probabilities, 0, 0))
print(tensor.value(probabilities, 0, 1))

Make it your experiment.

Use the matching development runtime. Save this program as examples/knn_parallel.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/knn_parallel.aner
aner run examples/knn_parallel.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Requires the matching unreleased Aner development runtime with KNN weights, local execution and max_workspace_bytes controls. Aner 0.0.5 version text alone does not establish these capabilities. Uses CPU Float64 and an admitted local worker pool; GPU and cluster KNN are unavailable. No download, external service or output files are required.

Credential setup
aner check examples/knn_parallel.aner
aner run examples/knn_parallel.aner

Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner