Solution¶
This section documents the solution components of the tree module.
solution
¶
Defines sklearn.tree solution interoperability.
This module provides classes for working with scikit-learn tree models.
CLASS | DESCRIPTION |
---|---|
DecisionTreeRegressorSolution |
Represents a scikit-learn DecisionTreeRegressor model, allowing conversion to and from a serializable format. |
DecisionTreeRegressorSolution
¶
Bases: BaseModel
Decision Tree Regressor scikit-learn model representation.
You can import the DecisionTreeRegressorSolution
class directly from tree
:
This class provides functionality to convert between scikit-learn's DecisionTreeRegressor model and a serializable format. It enables saving and loading trained models through dictionaries or JSON.
PARAMETER | DESCRIPTION |
---|---|
|
The inferred value of max_features.
TYPE:
|
|
Number of features seen during fit.
TYPE:
|
|
Names of features seen during fit.
TYPE:
|
|
The number of outputs when fit is performed.
TYPE:
|
|
The underlying Tree object.
TYPE:
|
Examples:
>>> from sklearn.datasets import load_diabetes
>>> from sklearn.tree import DecisionTreeRegressor
>>> from nextmv_sklearn.tree import DecisionTreeRegressorSolution
>>>
>>> # Train a scikit-learn model
>>> X, y = load_diabetes(return_X_y=True)
>>> model = DecisionTreeRegressor().fit(X, y)
>>>
>>> # Convert to solution object
>>> solution = DecisionTreeRegressorSolution.from_model(model)
>>>
>>> # Convert to dictionary for serialization
>>> model_dict = solution.to_dict()
>>>
>>> # Recreate solution from dictionary
>>> restored = DecisionTreeRegressorSolution.from_dict(model_dict["attributes"])
>>>
>>> # Convert back to scikit-learn model
>>> restored_model = restored.to_model()
feature_names_in_
class-attribute
instance-attribute
¶
feature_names_in_: ndarray = None
Names of features seen during fit. Defined only when X has feature names that are all strings.
from_dict
classmethod
¶
from_dict(
data: dict[str, Any],
) -> DecisionTreeRegressorSolution
Creates a DecisionTreeRegressorSolution instance from a dictionary.
PARAMETER | DESCRIPTION |
---|---|
|
Dictionary containing the model attributes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DecisionTreeRegressorSolution
|
Instance of DecisionTreeRegressorSolution. |
Examples:
>>> solution_dict = {
... "max_features_": 10,
... "n_features_in_": 10,
... "n_outputs_": 1,
... "tree_": "base64encodedtreedata"
... }
>>> solution = DecisionTreeRegressorSolution.from_dict(solution_dict)
Source code in nextmv-scikit-learn/nextmv_sklearn/tree/solution.py
from_model
classmethod
¶
from_model(
model: DecisionTreeRegressor,
) -> DecisionTreeRegressorSolution
Creates a DecisionTreeRegressorSolution instance from a scikit-learn DecisionTreeRegressor model.
PARAMETER | DESCRIPTION |
---|---|
|
scikit-learn DecisionTreeRegressor model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DecisionTreeRegressorSolution
|
Instance of DecisionTreeRegressorSolution. |
Examples:
>>> from sklearn.datasets import load_diabetes
>>> from sklearn.tree import DecisionTreeRegressor
>>> X, y = load_diabetes(return_X_y=True)
>>> model = DecisionTreeRegressor().fit(X, y)
>>> solution = DecisionTreeRegressorSolution.from_model(model)
Source code in nextmv-scikit-learn/nextmv_sklearn/tree/solution.py
max_features_
class-attribute
instance-attribute
¶
The inferred value of max_features.
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.
to_dict
¶
Convert a data model instance to a dict with associated class info.
RETURNS | DESCRIPTION |
---|---|
dict
|
Dictionary with class information and model attributes. The dictionary has two main keys: - 'class': Contains module and class name information - 'attributes': Contains the serialized model attributes |
Examples:
>>> solution = DecisionTreeRegressorSolution(max_features_=10)
>>> solution_dict = solution.to_dict()
>>> print(solution_dict['class']['name'])
'DecisionTreeRegressorSolution'
Source code in nextmv-scikit-learn/nextmv_sklearn/tree/solution.py
to_model
¶
Transforms the DecisionTreeRegressorSolution instance into a scikit-learn DecisionTreeRegressor model.
RETURNS | DESCRIPTION |
---|---|
DecisionTreeRegressor
|
scikit-learn DecisionTreeRegressor model. |
Examples:
>>> solution = DecisionTreeRegressorSolution(max_features_=10, n_features_in_=10)
>>> model = solution.to_model()
>>> isinstance(model, tree.DecisionTreeRegressor)
True
Source code in nextmv-scikit-learn/nextmv_sklearn/tree/solution.py
Tree
module-attribute
¶
Tree = Annotated[
Tree,
BeforeValidator(lambda x: x),
PlainSerializer(lambda x: b64encode(dumps(x))),
]
Type annotation for handling scikit-learn Tree objects.
This type is annotated with Pydantic validators and serializers to handle the conversion between scikit-learn Tree objects and base64-encoded strings for JSON serialization.