Predictions

Predictions are the core output of nirs4all. Every time you run a pipeline, nirs4all evaluates each model on each cross-validation fold and partition, storing the results as prediction records in the workspace. These records contain scalar scores for ranking, full arrays for visualization, and links to the trained model chain for export and replay.

The Prediction Lifecycle

Train               Store                    Query              Export             Predict
nirs4all.run() ---> store.sqlite +        -> result.top(n) ---> result.export() -> nirs4all.predict()
                    arrays/*.parquet         result.filter()    "model.n4a"        (new data)
                    (metadata, arrays,
                     chains, artifacts)
  1. Train – nirs4all.run() executes your pipeline, fits models on each fold, and evaluates on train/val/test partitions.

  2. Store – Prediction scores and metadata are persisted in SQLite (store.sqlite), while dense arrays (y_true, y_pred) are stored in Parquet sidecar files (arrays/).

  3. Query – Use result.top(n), result.filter(), result.best_rmse, etc. to find the best models.

  4. Export – Export the best model as a .n4a bundle for sharing or deployment.

  5. Predict – Apply the trained model to new data with nirs4all.predict().

Quick Start

import nirs4all
from sklearn.preprocessing import MinMaxScaler
from sklearn.cross_decomposition import PLSRegression

# 1. Train
result = nirs4all.run(
    pipeline=[MinMaxScaler(), PLSRegression(10)],
    dataset="sample_data/regression",
)

# 2. Check results
print(f"Best RMSE: {result.best_rmse:.4f}")
print(f"Best R2: {result.best_r2:.4f}")

# 3. Export best model
result.export("best_model.n4a")

# 4. Predict on new data
preds = nirs4all.predict(model="best_model.n4a", data=X_new)
print(preds.values)

Documentation Sections

📘 Understanding Predictions

What predictions are, how they are stored, key concepts (chains, partitions, scores)

Understanding Predictions
🎯 Making Predictions

How to predict on new data from a RunResult, exported bundle, or chain ID

Making Predictions
🔄 Session API

Stateful workflows for production pipelines, model persistence, and iterative experimentation

Session API - Stateful Workflows
📊 Analyzing Results

Querying, filtering, ranking, and visualizing prediction results

Analyzing Results
📦 Exporting Models

Exporting models as bundles, scripts, or configs for sharing and deployment

Exporting Models
🚀 Advanced Predictions

Transfer learning, retraining, SHAP explanations, batch prediction patterns

Advanced Predictions

See Also