Minimal Training Example¶
Train surrogate models from a CSV dataset.
Dataset¶
Example CSV (15 points minimum recommended - with fewer rows than
n_folds, cross-validation cannot run):
x1,x2,y1,y2
0.1,1.2,7.02,85.5
0.2,1.3,8.48,106.9
0.3,1.4,10.1,128.4
0.4,1.5,11.52,150.8
0.5,1.6,12.92,173.1
0.6,1.7,14.55,191.7
0.7,1.8,16.2,215.7
0.8,1.9,17.64,232.7
0.9,2.0,18.89,255.7
1.0,2.1,20.31,277.8
1.1,2.2,21.91,297.2
1.2,2.3,23.51,316.5
1.3,2.4,24.65,337.2
1.4,2.5,26.47,359.1
1.5,2.6,27.81,381.4
x1,x2→ input featuresy1,y2→ prediction targets
Note
n_folds (default: 5) cannot exceed the number of rows in the
dataset — each fold needs at least one row. For a real project,
aim for well more than n_folds rows so that each model has
enough points to fit on.
Single-Output Training¶
from pathlib import Path
from simml import (
LinearModelConfig,
GaussianProcessModelConfig,
NeuralNetworkModelConfig,
SurrogateTrainer,
)
trainer = SurrogateTrainer(
feature_names=["x1", "x2"],
target_names=["y1", "y2"],
model_configs=[
LinearModelConfig(),
GaussianProcessModelConfig(),
NeuralNetworkModelConfig(
hidden_layer_sizes=[64, 64],
),
],
n_folds=5,
)
results = trainer.run(
Path("dataset.csv")
)
trainer.print_best(
results,
trainer.target_names,
)
Saved models:
Multi-Output Training¶
from pathlib import Path
from simml import LinearModelConfig, GaussianProcessModelConfig
from simml.multi_trainer import MultiOutputSurrogateTrainer
trainer = MultiOutputSurrogateTrainer(
feature_names=["x1", "x2"],
target_names=["y1", "y2"],
model_configs=[
LinearModelConfig(),
GaussianProcessModelConfig(),
],
)
results = trainer.run(
Path("dataset.csv")
)
trainer.print_best(results)
Saved models:
Loading a Saved Model¶
from pathlib import Path
import pandas as pd
from simml import StoreManager, PredictManager
models = StoreManager.load("models")
X = pd.DataFrame({"x1": [0.15], "x2": [1.4]})
predictions = PredictManager(models).predict(X)
print(predictions)
You can still load a single .joblib file directly with joblib.load()
if you only need one specific model rather than every method saved for
a target:s
import joblib
import numpy as np
pipeline = joblib.load("models_saved/y1/Gaussian_Process.joblib")
X = np.array([[0.15, 1.4]])
print(pipeline.predict(X))