Quickstart¶
Time: about 10 minutes · You need: ml4 installed (Installation) and a labelled table split into a training file and a test file. You'll end with: a trained binary classifier, its scores on your test set, and a saved model you can reload anywhere.
AutoSupervised.fit() takes one training frame, with the column you want to predict (the
target) inside it. It handles validation itself. Keep your test frame out of fit, and use it
only for the final evaluation.
1. Load your data¶
import pandas as pd
train_df = pd.read_csv("train.csv") # includes the target column "y"
test_df = pd.read_csv("test.csv")
2. Configure¶
from parleml_ml4 import Ml4Config, Tasks
from parleml_ml4.contracts.metrics import EvalMetric
cfg = Ml4Config()
cfg.ml_task = Tasks.BINARY_CLASSIFICATION
cfg.eval_metric = EvalMetric.f1
cfg.data.target_column = "y"
3. Train¶
from parleml_ml4 import AutoSupervised
trainer = AutoSupervised(cfg)
model = trainer.fit(train_df) # plan features, train, fit the post-fit layers
Details: what fit does on the default settings
- It bags a LightGBM over five cross-validation folds.
- It reserves 10% of the training frame for the post-fit layers: calibration, the decision threshold, the baseline check and, for regression, prediction intervals. The served model never trains on those rows.
- It retrains the chosen model on the remaining training rows.
fit returns a FittedPipeline: the trained model, the
frozen feature recipe and the fitted post-fit layers, which is all scoring needs. It is also
available as trainer.pipeline_.
4. Predict and evaluate¶
X_test = test_df.drop(columns=["y"])
labels = model.predict(X_test) # labels at the tuned threshold
proba = model.predict_proba(X_test) # one probability column per class
report = model.evaluate(X_test, test_df["y"]) # fit-time measures and test scores
5. Save and reload¶
The saved model is a folder, not a single file. Reload it with parleml_ml4.load_pipeline in any
environment with the same ml4 version.
Name the target or the models per call
fit reads the target from cfg.data.target_column. You can pass it per call instead:
trainer.fit(train_df, label="churned"). You can also choose the models to train:
trainer.fit(train_df, models=("GBM:gbdt", "GBM:dart", "CAT:symmetric")). A gradient booster
is named with the variant it trains, so GBM:dart and GBM:rf are separate models from
GBM:gbdt.
Start from a preset¶
A preset is a ready configuration for a common goal. You still set the target:
from parleml_ml4.config import presets
cfg = presets.quick_explore() # one LightGBM, one holdout, light features: a fast first pass
cfg.data.target_column = "y"
Every preset is also in a registry by name:
When you are ready to spend compute on the strongest model, switch to high_accuracy, or to
foundation_ensemble on small data.
Next¶
- Configuration & presets: the
Ml4Configobject and every preset. - Worked examples: end-to-end runs for each kind of task.