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
:
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 |
---|---|
|
Estimated coefficients for the linear regression problem.
TYPE:
|
|
Rank of matrix X. Only available when X is dense.
TYPE:
|
|
Singular values of X. Only available when X is dense.
TYPE:
|
|
Independent term in the linear model. Set to 0.0 if fit_intercept=False.
TYPE:
|
|
Number of features seen during fit.
TYPE:
|
|
Names of features seen during fit. Defined only when X has feature names that are all strings.
TYPE:
|
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 |
---|---|
|
Dictionary containing the model attributes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
LinearRegressionSolution
|
Instance of LinearRegressionSolution. |
Source code in nextmv-scikit-learn/nextmv_sklearn/linear_model/solution.py
from_model
classmethod
¶
from_model(
model: LinearRegression,
) -> LinearRegressionSolution
Creates a LinearRegressionSolution instance from a scikit-learn LinearRegression model.
PARAMETER | DESCRIPTION |
---|---|
|
scikit-learn LinearRegression model.
TYPE:
|
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
intercept_
class-attribute
instance-attribute
¶
Independent term in the linear model. Set to 0.0 if fit_intercept = False.
model_config
class-attribute
instance-attribute
¶
n_features_in_
class-attribute
instance-attribute
¶
Number of features seen during fit.
rank_
class-attribute
instance-attribute
¶
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
¶
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
to_model
¶
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
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.