Skip to content

Solution

This section documents the solution components of the linear_model module.

solution

Defines sklearn.linear_model solution interoperability.

This module provides classes for converting between scikit-learn linear models and Nextmv compatible representations.

CLASS DESCRIPTION
LinearRegressionSolution

A Pydantic model representation of scikit-learn's LinearRegression model

Variables

Loss A type annotation for serializing loss functions

LinearRegressionSolution

Bases: BaseModel

Linear Regression scikit-learn model representation.

You can import the LinearRegressionSolution class directly from linear_model:

from nextmv_sklearn.linear_model import LinearRegressionSolution

This class provides a Pydantic model representation of scikit-learn's LinearRegression model, with methods for conversion between the scikit-learn model and this representation for serialization and deserialization.

PARAMETER DESCRIPTION

coef_

Estimated coefficients for the linear regression problem.

TYPE: ndarray

rank_

Rank of matrix X. Only available when X is dense.

TYPE: int DEFAULT: 0

singular_

Singular values of X. Only available when X is dense.

TYPE: ndarray

intercept_

Independent term in the linear model. Set to 0.0 if fit_intercept=False.

TYPE: float DEFAULT: 0

n_features_in_

Number of features seen during fit.

TYPE: int DEFAULT: 0

feature_names_in_

Names of features seen during fit. Defined only when X has feature names that are all strings.

TYPE: ndarray

Examples:

>>> import numpy as np
>>> from sklearn.linear_model import LinearRegression
>>> from nextmv_sklearn.linear_model import LinearRegressionSolution
>>>
>>> # Create a scikit-learn model and fit it
>>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
>>> y = np.array([1, 2, 2, 3])
>>> reg = LinearRegression().fit(X, y)
>>>
>>> # Convert to LinearRegressionSolution
>>> solution = LinearRegressionSolution.from_model(reg)
>>>
>>> # Convert back to scikit-learn model
>>> reg_restored = solution.to_model()

coef_ class-attribute instance-attribute

coef_: ndarray = None

Estimated coefficients for the linear regression problem.

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]) -> LinearRegressionSolution

Creates a LinearRegressionSolution instance from a dictionary.

PARAMETER DESCRIPTION
data

Dictionary containing the model attributes.

TYPE: dict[str, Any]

RETURNS DESCRIPTION
LinearRegressionSolution

Instance of LinearRegressionSolution.

Source code in nextmv-scikit-learn/nextmv_sklearn/linear_model/solution.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "LinearRegressionSolution":
    """
    Creates a LinearRegressionSolution instance from a dictionary.

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

    Returns
    -------
    LinearRegressionSolution
        Instance of LinearRegressionSolution.
    """

    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: LinearRegression,
) -> LinearRegressionSolution

Creates a LinearRegressionSolution instance from a scikit-learn LinearRegression model.

PARAMETER DESCRIPTION
model

scikit-learn LinearRegression model.

TYPE: LinearRegression

RETURNS DESCRIPTION
LinearRegressionSolution

Instance of LinearRegressionSolution containing the attributes from the provided model.

Examples:

>>> from sklearn.linear_model import LinearRegression
>>> import numpy as np
>>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
>>> y = np.array([1, 2, 2, 3])
>>> reg = LinearRegression().fit(X, y)
>>> solution = LinearRegressionSolution.from_model(reg)
Source code in nextmv-scikit-learn/nextmv_sklearn/linear_model/solution.py
@classmethod
def from_model(cls, model: linear_model.LinearRegression) -> "LinearRegressionSolution":
    """
    Creates a LinearRegressionSolution instance from a scikit-learn
    LinearRegression model.

    Parameters
    ----------
    model : LinearRegression
        scikit-learn LinearRegression model.

    Returns
    -------
    LinearRegressionSolution
        Instance of LinearRegressionSolution containing the attributes
        from the provided model.

    Examples
    --------
    >>> from sklearn.linear_model import LinearRegression
    >>> import numpy as np
    >>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
    >>> y = np.array([1, 2, 2, 3])
    >>> reg = LinearRegression().fit(X, y)
    >>> solution = LinearRegressionSolution.from_model(reg)
    """

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

    return cls(**data)

intercept_ class-attribute instance-attribute

intercept_: float = 0

Independent term in the linear model. Set to 0.0 if fit_intercept = False.

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.

rank_ class-attribute instance-attribute

rank_: int = 0

Rank of matrix X. Only available when X is dense.

singular_ class-attribute instance-attribute

singular_: ndarray = None

Singular values of X. Only available when X is dense.

to_dict

to_dict()

Convert a data model instance to a dict with associated class info.

RETURNS DESCRIPTION
dict

Dictionary containing the class module and name, along with the model attributes.

Examples:

>>> solution = LinearRegressionSolution(coef_=np.array([1.0, 2.0]))
>>> solution_dict = solution.to_dict()
>>> print(solution_dict['class']['name'])
'LinearRegressionSolution'
Source code in nextmv-scikit-learn/nextmv_sklearn/linear_model/solution.py
def to_dict(self):
    """
    Convert a data model instance to a dict with associated class info.

    Returns
    -------
    dict
        Dictionary containing the class module and name, along with the
        model attributes.

    Examples
    --------
    >>> solution = LinearRegressionSolution(coef_=np.array([1.0, 2.0]))
    >>> solution_dict = solution.to_dict()
    >>> print(solution_dict['class']['name'])
    'LinearRegressionSolution'
    """

    d = self.model_dump(mode="json", exclude_none=True, by_alias=True)

    t = type(self)
    return {
        "class": {
            "module": t.__module__,
            "name": t.__name__,
        },
        "attributes": d,
    }

to_model

to_model() -> LinearRegression

Transforms the LinearRegressionSolution instance into a scikit-learn LinearRegression model.

RETURNS DESCRIPTION
LinearRegression

scikit-learn LinearRegression model with attributes set from the current LinearRegressionSolution instance.

Examples:

>>> # Assuming we have a LinearRegressionSolution instance
>>> solution = LinearRegressionSolution(
...     coef_=np.array([1.0, 2.0]),
...     intercept_=0.5
... )
>>> # Convert back to scikit-learn model
>>> sklearn_model = solution.to_model()
>>> sklearn_model.coef_
array([1., 2.])
>>> sklearn_model.intercept_
0.5
Source code in nextmv-scikit-learn/nextmv_sklearn/linear_model/solution.py
def to_model(self) -> linear_model.LinearRegression:
    """
    Transforms the LinearRegressionSolution instance into a scikit-learn
    LinearRegression model.

    Returns
    -------
    LinearRegression
        scikit-learn LinearRegression model with attributes set from
        the current LinearRegressionSolution instance.

    Examples
    --------
    >>> # Assuming we have a LinearRegressionSolution instance
    >>> solution = LinearRegressionSolution(
    ...     coef_=np.array([1.0, 2.0]),
    ...     intercept_=0.5
    ... )
    >>> # Convert back to scikit-learn model
    >>> sklearn_model = solution.to_model()
    >>> sklearn_model.coef_
    array([1., 2.])
    >>> sklearn_model.intercept_
    0.5
    """

    m = linear_model.LinearRegression()
    for key in self.model_fields:
        setattr(m, key, self.__dict__[key])

    return m

Loss module-attribute

Loss = Annotated[
    Any,
    BeforeValidator(lambda x: x),
    PlainSerializer(lambda x: b64encode(dumps(x))),
]

Type annotation for serializing loss functions.

This annotation combines validation and serialization for scikit-learn loss functions. It uses base64 encoding and pickle to serialize the loss object to a string.