Solution¶
This section documents the solution components of the ensemble module.
solution
¶
Defines sklearn.ensemble solution interoperability.
This module provides classes for interoperability between scikit-learn ensemble models and Nextmv's serialization format.
CLASS | DESCRIPTION |
---|---|
GradientBoostingRegressorSolution |
A Pydantic model representing scikit-learn's GradientBoostingRegressor. |
RandomForestRegressorSolution |
A Pydantic model representing scikit-learn's RandomForestRegressor. |
GradientBoostingRegressorSolution
¶
Bases: BaseModel
Gradient Boosting Regressor scikit-learn model representation.
You can import the GradientBoostingRegressorSolution
class directly from ensemble
:
This class provides methods to convert between scikit-learn's GradientBoostingRegressor and a serializable Pydantic model representation. It allows scikit-learn models to be stored in JSON format and restored later with all their properties intact.
PARAMETER | DESCRIPTION |
---|---|
|
The number of estimators as selected by early stopping (if n_iter_no_change is specified). Otherwise it is set to n_estimators.
TYPE:
|
|
The number of trees that are built at each iteration. For regressors, this is always 1.
TYPE:
|
|
The improvement in loss on the out-of-bag samples relative to the previous iteration.
TYPE:
|
|
The full history of the loss values on the out-of-bag samples. Only available if subsample < 1.0.
TYPE:
|
|
The last value of the loss on the out-of-bag samples.
TYPE:
|
|
The i-th score train_score_[i] is the loss of the model at iteration i on the in-bag sample.
TYPE:
|
|
The estimator that provides the initial predictions.
TYPE:
|
|
The collection of fitted sub-estimators.
TYPE:
|
|
Number of features seen during fit.
TYPE:
|
|
Names of features seen during fit.
TYPE:
|
|
The inferred value of max_features.
TYPE:
|
|
The loss function used in the model, serialized with pickle and base64 encoding.
TYPE:
|
|
Configuration for the Pydantic model, allowing arbitrary types.
TYPE:
|
estimators_
class-attribute
instance-attribute
¶
estimators_: list[DecisionTreeRegressorSolution] = None
The collection of fitted sub-estimators.
feature_names_in_
class-attribute
instance-attribute
¶
feature_names_in_: ndarray = None
Names of features seen during fit.
from_dict
classmethod
¶
from_dict(
data: dict[str, Any],
) -> GradientBoostingRegressorSolution
Creates a GradientBoostingRegressorSolution instance from a dictionary.
This method is used to deserialize a previously serialized GradientBoostingRegressor model. It handles the conversion of nested models and arrays.
PARAMETER | DESCRIPTION |
---|---|
|
Dictionary containing the model attributes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GradientBoostingRegressorSolution
|
Instance of GradientBoostingRegressorSolution. |
Examples:
>>> model_dict = {"n_estimators_": 100, "n_features_in_": 10, ...}
>>> model = GradientBoostingRegressorSolution.from_dict(model_dict)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
from_model
classmethod
¶
from_model(
model: GradientBoostingRegressor,
) -> GradientBoostingRegressorSolution
Creates a GradientBoostingRegressorSolution instance from a scikit-learn GradientBoostingRegressor model.
This method converts a trained scikit-learn GradientBoostingRegressor model into a serializable Pydantic model representation.
PARAMETER | DESCRIPTION |
---|---|
|
scikit-learn GradientBoostingRegressor model. |
RETURNS | DESCRIPTION |
---|---|
GradientBoostingRegressorSolution
|
Instance of GradientBoostingRegressorSolution. |
Examples:
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from nextmv_sklearn.ensemble import GradientBoostingRegressorSolution
>>> sklearn_model = GradientBoostingRegressor().fit(X, y)
>>> model_solution = GradientBoostingRegressorSolution.from_model(sklearn_model)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
init_
class-attribute
instance-attribute
¶
init_: DummyRegressorSolution = None
The estimator that provides the initial predictions.
max_features_
class-attribute
instance-attribute
¶
The inferred value of max_features.
model_config
class-attribute
instance-attribute
¶
n_estimators_
class-attribute
instance-attribute
¶
The number of estimators as selected by early stopping (if n_iter_no_change is specified). Otherwise it is set to n_estimators.
n_features_in_
class-attribute
instance-attribute
¶
Number of features seen during fit.
n_trees_per_iteration_
class-attribute
instance-attribute
¶
The number of trees that are built at each iteration. For regressors, this is always 1.
oob_improvement_
class-attribute
instance-attribute
¶
oob_improvement_: ndarray = None
The improvement in loss on the out-of-bag samples relative to the previous iteration.
oob_score_
class-attribute
instance-attribute
¶
The last value of the loss on the out-of-bag samples.
oob_scores_
class-attribute
instance-attribute
¶
oob_scores_: ndarray = None
The full history of the loss values on the out-of-bag samples. Only available if subsample < 1.0.
to_dict
¶
Convert a data model instance to a dict with associated class info.
This method serializes the GradientBoostingRegressorSolution instance to a dictionary that can be stored in JSON format. The dictionary includes class metadata to facilitate deserialization.
RETURNS | DESCRIPTION |
---|---|
dict
|
Dictionary representation of the model with class metadata. |
Examples:
>>> model_solution = GradientBoostingRegressorSolution.from_model(sklearn_model)
>>> model_dict = model_solution.to_dict()
>>> import json
>>> with open('model.json', 'w') as f:
... json.dump(model_dict, f)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
to_model
¶
Transforms the GradientBoostingRegressorSolution instance into a scikit-learn GradientBoostingRegressor model.
This method converts a serializable Pydantic model back to a scikit-learn GradientBoostingRegressor model with all its properties and trained state intact.
RETURNS | DESCRIPTION |
---|---|
GradientBoostingRegressor
|
scikit-learn GradientBoostingRegressor model. |
Examples:
>>> # Load a model from JSON
>>> with open('model.json', 'r') as f:
... model_dict = json.load(f)
>>> model_solution = GradientBoostingRegressorSolution.from_dict(model_dict["attributes"])
>>> sklearn_model = model_solution.to_model()
>>> predictions = sklearn_model.predict(X_test)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
Loss
module-attribute
¶
Loss = Annotated[
Any,
BeforeValidator(lambda x: x),
PlainSerializer(lambda x: b64encode(dumps(x))),
]
Type annotation for loss functions in scikit-learn models.
This annotated type handles serialization/deserialization of scikit-learn loss functions using pickle and base64 encoding.
RandomForestRegressorSolution
¶
Bases: BaseModel
Random Forest Regressor scikit-learn model representation.
You can import the RandomForestRegressorSolution
class directly from ensemble
:
This class provides methods to convert between scikit-learn's RandomForestRegressor and a serializable Pydantic model representation. It allows scikit-learn models to be stored in JSON format and restored later with all their properties intact.
PARAMETER | DESCRIPTION |
---|---|
|
The child estimator template used to create the collection of fitted sub-estimators. |
|
The collection of fitted sub-estimators.
TYPE:
|
|
Number of features seen during fit.
TYPE:
|
|
Names of features seen during fit.
TYPE:
|
|
The number of outputs when fit is performed.
TYPE:
|
|
Score of the training dataset obtained using an out-of-bag estimate.
TYPE:
|
|
Prediction computed with out-of-bag estimate on the training set.
TYPE:
|
|
Configuration for the Pydantic model, allowing arbitrary types.
TYPE:
|
estimator_
class-attribute
instance-attribute
¶
estimator_: DecisionTreeRegressorSolution = None
The child estimator template used to create the collection of fitted sub-estimators.
estimators_
class-attribute
instance-attribute
¶
estimators_: list[DecisionTreeRegressorSolution] = None
The collection of fitted sub-estimators.
feature_names_in_
class-attribute
instance-attribute
¶
feature_names_in_: ndarray = None
Names of features seen during fit.
from_dict
classmethod
¶
from_dict(
data: dict[str, Any],
) -> RandomForestRegressorSolution
Creates a RandomForestRegressorSolution instance from a dictionary.
This method is used to deserialize a previously serialized RandomForestRegressor model. It handles the conversion of nested models and arrays.
PARAMETER | DESCRIPTION |
---|---|
|
Dictionary containing the model attributes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RandomForestRegressorSolution
|
Instance of RandomForestRegressorSolution. |
Examples:
>>> model_dict = {"estimators_": [...], "n_features_in_": 10, ...}
>>> model = RandomForestRegressorSolution.from_dict(model_dict)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
from_model
classmethod
¶
from_model(
model: RandomForestRegressor,
) -> RandomForestRegressorSolution
Creates a RandomForestRegressorSolution instance from a scikit-learn RandomForestRegressor model.
This method converts a trained scikit-learn RandomForestRegressor model into a serializable Pydantic model representation.
PARAMETER | DESCRIPTION |
---|---|
|
scikit-learn RandomForestRegressor model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RandomForestRegressorSolution
|
Instance of RandomForestRegressorSolution. |
Examples:
>>> from sklearn.ensemble import RandomForestRegressor
>>> from nextmv_sklearn.ensemble import RandomForestRegressorSolution
>>> sklearn_model = RandomForestRegressor().fit(X, y)
>>> model_solution = RandomForestRegressorSolution.from_model(sklearn_model)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
model_config
class-attribute
instance-attribute
¶
n_features_in_
class-attribute
instance-attribute
¶
Number of features seen during fit.
n_outputs_
class-attribute
instance-attribute
¶
The number of outputs when fit is performed.
oob_prediction_
class-attribute
instance-attribute
¶
oob_prediction_: ndarray = None
Prediction computed with out-of-bag estimate on the training set.
oob_score_
class-attribute
instance-attribute
¶
Score of the training dataset obtained using an out-of-bag estimate.
to_dict
¶
Convert a data model instance to a dict with associated class info.
This method serializes the RandomForestRegressorSolution instance to a dictionary that can be stored in JSON format. The dictionary includes class metadata to facilitate deserialization.
RETURNS | DESCRIPTION |
---|---|
dict
|
Dictionary representation of the model with class metadata. |
Examples:
>>> model_solution = RandomForestRegressorSolution.from_model(sklearn_model)
>>> model_dict = model_solution.to_dict()
>>> import json
>>> with open('model.json', 'w') as f:
... json.dump(model_dict, f)
Source code in nextmv-scikit-learn/nextmv_sklearn/ensemble/solution.py
to_model
¶
Transforms the RandomForestRegressorSolution instance into a scikit-learn RandomForestRegressor model.
This method converts a serializable Pydantic model back to a scikit-learn RandomForestRegressor model with all its properties and trained state intact.
RETURNS | DESCRIPTION |
---|---|
RandomForestRegressor
|
scikit-learn RandomForestRegressor model. |
Examples:
>>> # Load a model from JSON
>>> with open('model.json', 'r') as f:
... model_dict = json.load(f)
>>> model_solution = RandomForestRegressorSolution.from_dict(model_dict["attributes"])
>>> sklearn_model = model_solution.to_model()
>>> predictions = sklearn_model.predict(X_test)