Skip to content

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:

from nextmv_sklearn.tree import DecisionTreeRegressorSolution

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

max_features_

The inferred value of max_features.

TYPE: int DEFAULT: 0

n_features_in_

Number of features seen during fit.

TYPE: int DEFAULT: 0

feature_names_in_

Names of features seen during fit.

TYPE: ndarray DEFAULT: None

n_outputs_

The number of outputs when fit is performed.

TYPE: int DEFAULT: 0

tree_

The underlying Tree object.

TYPE: Tree DEFAULT: None

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
data

Dictionary containing the model attributes.

TYPE: dict[str, Any]

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
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "DecisionTreeRegressorSolution":
    """
    Creates a DecisionTreeRegressorSolution instance from a dictionary.

    Parameters
    ----------
    data : dict[str, Any]
        Dictionary containing the model attributes.

    Returns
    -------
    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)
    """

    if "tree_" in data:
        data["tree_"] = pickle.loads(base64.b64decode(data["tree_"]))

    for key, value in cls.__annotations__.items():
        if key in data and value is ndarray:
            data[key] = np.array(data[key])

    return cls(**data)

from_model classmethod

from_model(
    model: DecisionTreeRegressor,
) -> DecisionTreeRegressorSolution

Creates a DecisionTreeRegressorSolution instance from a scikit-learn DecisionTreeRegressor model.

PARAMETER DESCRIPTION
model

scikit-learn DecisionTreeRegressor model.

TYPE: DecisionTreeRegressor

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
@classmethod
def from_model(cls, model: tree.DecisionTreeRegressor) -> "DecisionTreeRegressorSolution":
    """
    Creates a DecisionTreeRegressorSolution instance from a scikit-learn
    DecisionTreeRegressor model.

    Parameters
    ----------
    model : tree.DecisionTreeRegressor
        scikit-learn DecisionTreeRegressor model.

    Returns
    -------
    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)
    """

    data = {}
    for key in cls.__annotations__:
        try:
            data[key] = getattr(model, key)
        except AttributeError:
            pass

    return cls(**data)

max_features_ class-attribute instance-attribute

max_features_: int = 0

The inferred value of max_features.

model_config class-attribute instance-attribute

model_config = ConfigDict(arbitrary_types_allowed=True)

n_features_in_ class-attribute instance-attribute

n_features_in_: int = 0

Number of features seen during fit.

n_outputs_ class-attribute instance-attribute

n_outputs_: int = 0

The number of outputs when fit is performed.

to_dict

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
def to_dict(self):
    """
    Convert a data model instance to a dict with associated class info.

    Returns
    -------
    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'
    """

    t = type(self)
    return {
        "class": {
            "module": t.__module__,
            "name": t.__name__,
        },
        "attributes": self.model_dump(mode="json", exclude_none=True, by_alias=True),
    }

to_model

to_model() -> DecisionTreeRegressor

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
def to_model(self) -> tree.DecisionTreeRegressor:
    """
    Transforms the DecisionTreeRegressorSolution instance into a scikit-learn
    DecisionTreeRegressor model.

    Returns
    -------
    tree.DecisionTreeRegressor
        scikit-learn DecisionTreeRegressor model.

    Examples
    --------
    >>> solution = DecisionTreeRegressorSolution(max_features_=10, n_features_in_=10)
    >>> model = solution.to_model()
    >>> isinstance(model, tree.DecisionTreeRegressor)
    True
    """
    m = tree.DecisionTreeRegressor()
    for key in self.model_fields:
        setattr(m, key, self.__dict__[key])

    return m

tree_ class-attribute instance-attribute

tree_: Tree = None

The underlying Tree object.

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.