Titanic survival classifier
Load Titanic by name, make a seeded split, fit standardization on training rows and evaluate a dense survival classifier on held out data.
// Titanic: Harrell Jr. and Cason, Vanderbilt titanic3 (1,309 passengers).
// Four predictors: pclass, sex (female=0, male=1), sibsp, parch.
// Attribution and Vanderbilt permission: datasets/titanic/README.md.
// A row split does not keep related passengers together; no family groups are supplied.
import aner.dataset
import aner.nn
import aner.ml
// 1. Load the bundled numeric teaching subset; no CSV or download is needed.
let passengers = dataset.load("titanic")
// 2. Split before fitting anything: a seeded, stratified 80/20 row split.
let split = passengers.split(test: 0.2, seed: 2026)
let training = split.train()
let testing = split.test()
print("Training rows")
print(training.rows())
print("Held-out test rows")
print(testing.rows())
// 3. Describe a classifier with four inputs and two survival-class logits.
let network = nn.sequential(seed: 42)
.dense(inputs: 4, outputs: 8, initialization: "xavier")
.tanh()
.dense(inputs: 8, outputs: 2, initialization: "xavier")
let pipeline = ml.classifier(network, standardize: true,
loss: "cross_entropy", optimizer: "adam")
.on(device: "cpu", precision: "Float64", policy: "preserve")
// 4. Fit standardization AND model parameters on training rows only.
// The fitted model stores each training feature's mean and population scale.
// Keep this fixed teaching configuration independent of the test results.
let fitted = pipeline.fit(training, epochs: 400, rate: 0.01)
print("Training-fitted pclass center")
print(nn.value(ml.center(fitted), 0, 0))
print("Training-fitted pclass scale")
print(nn.value(ml.scale(fitted), 0, 0))
// 5. Evaluate the held-out rows once, reusing the stored training scaler.
// Evaluation does not refit preprocessing or update model parameters.
print(fitted.evaluate(testing).summary())
Run step by step in VS Code.
The notebook separates loading, splitting, model configuration, training, and evaluation into cells. It opens with empty outputs, shared variables, and Development inspection enabled.
- Install a compatible native Aner executable and VS Code extension using the editor guide. These datasets and fitted classifier pipelines require the current native development engine.
- Download the
.anernbnotebook and open it in your VS Code project. If it appears as JSON, choose Reopen Editor With… → Aner Notebook. - Select the
Aner — notebookkernel and keep Shared variables active. Run code cells from top to bottom, or choose Run All. Later cells reuse the datasets, network, and fitted model from earlier cells. - Read the results below each cell. Open Inspector to inspect native variables. Use Aner: Open Development Training Report to inspect the recorded loss, topology, and available gradients after the fitting cell completes.
After changing an earlier cell, rerun its dependent cells. Use Aner: Restart Notebook Session, then Run All, for a clean experiment. Saved outputs are historical; reopening the file starts without live variables. The notebook guide explains session state, inspection, and runtime controls.
Make it your experiment.
With Aner installed, save this program as examples/neural_titanic_concise.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/neural_titanic_concise.aner
aner run examples/neural_titanic_concise.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Use the current native Aner development engine with dataset.load, the bundled Titanic and Pima Indians Diabetes loaders, and fitted classifier pipelines. Earlier native packages may predate these dataset loaders. No CSV preparation or network download is required. The seeded split happens before the pipeline fits standardization and model parameters on training rows; evaluation uses the held out test partition.
Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution