All examples
Machine learning

Inspect a native classification tree

Fit a deterministic held out classifier and export its fitted structure for inspection.

ml_decision_tree.aner
// Iris: Fisher, R. (1936). UCI. https://doi.org/10.24432/C56C76
// CC BY 4.0: https://creativecommons.org/licenses/by/4.0/
// Original UCI records retained; class names mapped to zero-based IDs.
// Full attribution and variant notes: datasets/iris/README.md.
import aner.dataset;
import aner.tensor;
import aner.ml;
import aner.metrics;

fn main() -> Unit {
    let flowers = dataset.iris();
    let partitions = dataset.split(flowers, 0.2, 2026);
    let training = dataset.train(partitions);
    let testing = dataset.test(partitions);

    // Choose these controls before inspecting the held-out test results.
    // Numeric binary splits support multiple classes. Scaling is unnecessary.
    let model: DecisionTreeClassifier = ml.fit_tree(
        features: dataset.features(training),
        labels: dataset.targets(training),
        criterion: "gini",
        max_depth: 4,
        min_samples_split: 4,
        min_samples_leaf: 2);
    let predicted = model.predict(dataset.features(testing));
    print("Dataset citation"); print(dataset.citation(flowers));
    print("Dataset SHA256"); print(dataset.sha256(flowers));
    print("Split seed"); print(2026);
    print("Training rows"); print(dataset.rows(training));
    print("Held-out rows"); print(dataset.rows(testing));
    print("Held-out accuracy"); print(metrics.accuracy(predicted, dataset.targets(testing)));
    print("Tree depth"); print(model.depth());
    print("Tree nodes"); print(model.node_count());
    print("Feature indices: 0 sepal length, 1 sepal width, 2 petal length, 3 petal width (cm)");
    print(model.tree_text());

    let probabilities = model.predict_proba(dataset.features(testing));
    let class_ids = model.classes();
    print("First held-out row: class ID then leaf class frequency (not calibrated)");
    var class_index = 0;
    while class_index < tensor.rows(class_ids) {
        print(tensor.value(class_ids, class_index, 0));
        print(tensor.value(probabilities, 0, class_index));
        class_index = class_index + 1;
    }
    // Files are created in the current working directory; existing paths are refused.
    // Choose new names or remove earlier exports before running this example again.
    // SVG opens directly in a browser and needs no Graphviz installation.
    model.export_tree("iris-decision-tree.svg");
    model.export_tree("iris-decision-tree.dot");
    model.export_tree("iris-decision-tree.txt");
    print("Exported iris-decision-tree.svg, .dot and .txt");
}

Make it your experiment.

With Aner 0.0.3 or newer installed, save this program as examples/ml_decision_tree.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/ml_decision_tree.aner
aner run examples/ml_decision_tree.aner

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

Requires the current native Aner runtime with the documented API. Keep the source directory structure and review the guide for data, optional map packs and output files before running. Earlier release binaries may predate these features.

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