Predictions API Reference
This page is the API reference for the prediction-related classes. For conceptual guidance and practical workflows, see the Predictions User Guide.
Module-Level Functions
nirs4all.predict()
nirs4all.predict(
model=None, # Path to .n4a bundle, prediction dict, or Path
data=None, # numpy array, tuple, dict, path, or SpectroDataset
*,
chain_id=None, # Chain ID for store-based prediction (alternative to model)
workspace_path=None, # Workspace root (required with chain_id outside a session)
name="prediction_dataset",
all_predictions=False,
session=None,
verbose=0,
**runner_kwargs,
) -> PredictResult
Two prediction paths:
Store-based (preferred): pass
chain_idto replay a stored chain directly from the workspace.Model-based: pass
model(bundle path, prediction dict, or config path).
model and chain_id are mutually exclusive.
See: Making Predictions
nirs4all.run()
nirs4all.run(
pipeline, # List of steps, dict, path, or PipelineConfigs
dataset, # Path, arrays, dict, SpectroDataset, or DatasetConfigs
*,
name="",
session=None,
verbose=1,
save_artifacts=True,
save_charts=True,
plots_visible=False,
random_state=None,
**runner_kwargs,
) -> RunResult
See: Analyzing Results
nirs4all.retrain()
nirs4all.retrain(
source, # Prediction dict, path to .n4a bundle, or config path
data, # New dataset
*,
mode="full", # "full", "transfer", or "finetune"
name="retrain_dataset",
new_model=None,
epochs=None,
session=None,
verbose=1,
save_artifacts=True,
**kwargs,
) -> RunResult
See: Advanced Predictions
nirs4all.explain()
nirs4all.explain(
model, # Prediction dict, path to .n4a bundle, or config path
data, # Data to explain
*,
name="explain_dataset",
session=None,
verbose=1,
plots_visible=True,
n_samples=None,
explainer_type="auto",
**shap_params,
) -> ExplainResult
See: Advanced Predictions
Result Classes
RunResult
Returned by nirs4all.run() and nirs4all.retrain().
Properties:
Property |
Type |
Description |
|---|---|---|
|
dict |
Best prediction entry (ranked by validation score) |
|
float |
Best model’s primary test score |
|
float |
Best model’s RMSE (NaN if unavailable) |
|
float |
Best model’s R2 (NaN if unavailable) |
|
float |
Best model’s accuracy (NaN if unavailable) |
|
int |
Total number of predictions |
|
Path or None |
Path to run artifacts directory |
Methods:
Method |
Returns |
Description |
|---|---|---|
|
PredictionResultsList |
Top N predictions by ranking |
|
list[dict] |
Filter predictions by criteria |
|
list[str] |
Unique dataset names |
|
list[str] |
Unique model names |
|
Path |
Export model to bundle |
|
Path |
Export model artifact only |
|
str |
Multi-line summary string |
|
dict |
Check for common issues |
top() keyword arguments:
rank_metric: Metric to rank by (default: stored metric)rank_partition: Partition to rank on (default:"val")display_metrics: List of additional metrics to compute for displaydisplay_partition: Partition for display metrics (default:"test")ascending: Sort order (None infers from metric)group_by: Group results by column(s) – returns top N per groupreturn_grouped: If True with group_by, return dict of group to resultsaggregate: Aggregate predictions by metadata column or"y"aggregate_method:"mean","median", or"vote"
PredictResult
Returned by nirs4all.predict().
Attributes:
Attribute |
Type |
Description |
|---|---|---|
|
numpy.ndarray |
Predicted values |
|
dict |
Additional prediction metadata |
|
numpy.ndarray or None |
Indices of predicted samples |
|
str |
Name of the model used |
|
list[str] |
Preprocessing steps applied |
Properties:
Property |
Type |
Description |
|---|---|---|
|
numpy.ndarray |
Alias for y_pred |
|
tuple |
Shape of prediction array |
|
bool |
True if multi-output prediction |
Methods:
Method |
Returns |
Description |
|---|---|---|
|
numpy.ndarray |
Predictions as numpy array |
|
list[float] |
Predictions as Python list |
|
pandas.DataFrame |
Predictions as DataFrame |
|
numpy.ndarray |
Flattened 1D predictions |
ExplainResult
Returned by nirs4all.explain().
Attributes:
Attribute |
Type |
Description |
|---|---|---|
|
Any |
SHAP values (Explanation or ndarray) |
|
list[str] or None |
Feature names |
|
float or ndarray or None |
Baseline prediction |
|
dict[str, Path] |
Generated plot files |
|
str |
SHAP explainer type used |
|
str |
Explained model name |
|
int |
Number of samples explained |
Properties:
Property |
Type |
Description |
|---|---|---|
|
numpy.ndarray |
Raw SHAP values array |
|
tuple |
Shape of SHAP values |
|
numpy.ndarray |
Mean absolute SHAP per feature |
|
list[str] |
Features sorted by importance (descending) |
Methods:
Method |
Returns |
Description |
|---|---|---|
|
dict[str, float] |
Feature importance ranking |
|
dict[str, float] |
SHAP values for one sample |
|
pandas.DataFrame |
SHAP values as DataFrame |
PredictionResultsList
Returned by RunResult.top() and Predictions.top(). Extends Python’s built-in list with additional methods.
Methods:
Method |
Returns |
Description |
|---|---|---|
|
None |
Save all predictions to structured CSV |
|
PredictionResult or None |
Retrieve prediction by ID |
Supports all standard list operations: indexing, slicing, iteration, len(), etc.
PredictionResult
A dict subclass representing a single prediction. Returned as elements of PredictionResultsList.
Properties:
Property |
Type |
Description |
|---|---|---|
|
str |
Prediction identifier |
|
str |
Dataset name |
|
str |
Model name |
|
str |
Fold identifier |
|
str |
Configuration name |
|
int |
Pipeline step index |
|
int |
Operation counter |
Additional fields are accessible via dict access (e.g., pred["model_classname"], pred.get("preprocessings")).
Methods:
Method |
Returns |
Description |
|---|---|---|
|
str |
Formatted metric table (train/val/test) |
|
None |
Save to CSV file |
|
dict |
Compute metrics for this prediction |
WorkspaceStore (Prediction Queries)
For store-level queries across runs. See full API in the storage reference.
Prediction query methods:
Method |
Returns |
Description |
|---|---|---|
|
dict or None |
Single prediction record |
|
polars.DataFrame |
Filtered prediction records |
|
polars.DataFrame |
Top-N ranked predictions |
|
Path |
Export to Parquet file |
Filter arguments for query_predictions:
dataset_name,model_class,partition,fold_id,branch_id,pipeline_id,run_id,limit,offset
See Also
Predictions – Predictions user guide (overview)
Making Predictions – Practical prediction workflows
Analyzing Results – Querying and visualization
Exporting Models – Export formats
Advanced Predictions – Transfer, retrain, SHAP