SingleModelContainer¶
SingleModelContainer
¶
SingleModelContainer(
model_config: BaseMlModelConfig[T, U, V, W],
)
Single model container for machine learning models.
This class provides a container for a single machine learning model. It takes a model configuration and provides methods for training a model and making predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_config
|
BaseMlModelConfig
|
The model configuration, which includes the learner, predictor, training configuration, and prediction configuration. |
required |
Examples:
>>> import lightgbm as lgb
>>> from sklearn.datasets import make_regression
>>> from sklearn.metrics import r2_score
>>> from sklearn.model_selection import train_test_split
>>> from factrainer.core import SingleModelContainer
>>> from factrainer.lightgbm import LgbDataset, LgbModelConfig, LgbTrainConfig
>>>
>>> # Load data
>>> X, y = make_regression()
>>> train_X, test_X, train_y, test_y = train_test_split(
... X, y, test_size=0.2, random_state=1
... )
>>>
>>> # Create datasets
>>> train_dataset = LgbDataset(dataset=lgb.Dataset(train_X, train_y))
>>> val_dataset = LgbDataset(dataset=lgb.Dataset(test_X, test_y))
>>> test_dataset = LgbDataset(dataset=lgb.Dataset(test_X, test_y))
>>>
>>> # Configure model
>>> config = LgbModelConfig.create(
... train_config=LgbTrainConfig(
... params={
... "objective": "regression",
... "seed": 1,
... "deterministic": True,
... "verbose": -1,
... },
... callbacks=[lgb.early_stopping(100, verbose=False)],
... ),
... )
>>>
>>> # Create and train model
>>> model = SingleModelContainer(config)
>>> model.train(train_dataset, val_dataset)
>>>
>>> # Make predictions
>>> y_pred = model.predict(test_dataset)
>>>
>>> # Evaluate predictions
>>> metric = model.evaluate(test_y, y_pred, r2_score)
Attributes¶
raw_model
property
¶
raw_model: U
Get the trained raw model.
Returns:
Type | Description |
---|---|
U
|
The trained model as a RawModel object. |
train_config
property
writable
¶
train_config: V
Get the training configuration.
Returns:
Type | Description |
---|---|
V
|
The training configuration. |
pred_config
property
writable
¶
pred_config: W
Get the prediction configuration.
Returns:
Type | Description |
---|---|
W
|
The prediction configuration. |
Functions¶
train
¶
train(
train_dataset: T, val_dataset: T | None = None
) -> None
Train the model. The trained model can be accessed through
the raw_model
property.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_dataset
|
T
|
The training dataset. |
required |
val_dataset
|
T | None
|
The validation dataset if needed. |
None
|
Returns:
Type | Description |
---|---|
None
|
|
predict
¶
predict(pred_dataset: T) -> Prediction
Make predictions using the trained model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_dataset
|
T
|
The test dataset. |
required |
Returns:
Type | Description |
---|---|
Prediction
|
The predictions as a NumPy array. |
evaluate
¶
evaluate(
y_true: Target,
y_pred: Prediction,
eval_func: EvalFunc[X],
) -> X
Evaluate the model's predictions against true values.
This method evaluates predictions from a single trained model, typically on a held-out test set or validation set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
Target
|
The true target values as a NumPy array. |
required |
y_pred
|
Prediction
|
The predicted values as a NumPy array. Must have the same shape as y_true. |
required |
eval_func
|
EvalFunc[X]
|
The evaluation function that takes (y_true, y_pred) and returns a metric. Common examples include sklearn.metrics functions like r2_score, mae, etc. |
required |
Returns:
Type | Description |
---|---|
X
|
The evaluation score of type X, as returned by eval_func. |
Raises:
Type | Description |
---|---|
ValueError
|
If y_true or y_pred are not NumPy arrays. |