Solution¶
This section documents the solution components of the dummy module.
solution
¶
Defines sklearn.dummy solution interoperability.
This module provides classes and utilities for working with scikit-learn's dummy models in the Nextmv ecosystem.
CLASS | DESCRIPTION |
---|---|
DummyRegressorSolution |
A Pydantic model representation of scikit-learn's DummyRegressor. It allows for serialization, deserialization, and conversion between the scikit-learn model and this representation. |
DummyRegressorSolution
¶
Bases: BaseModel
Dummy Regressor scikit-learn model representation.
You can import the DummyRegressorSolution
class directly from dummy
:
This class provides a Pydantic model representation of scikit-learn's DummyRegressor model. It allows for serialization, deserialization, and conversion between the scikit-learn model and this representation.
PARAMETER | DESCRIPTION |
---|---|
|
Mean or median or quantile of the training targets or constant value given by the user.
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:
|
|
Number of outputs.
TYPE:
|
Examples:
>>> from sklearn.dummy import DummyRegressor
>>> from nextmv_sklearn.dummy import DummyRegressorSolution
>>> # Train a scikit-learn dummy regressor
>>> model = DummyRegressor(strategy="mean")
>>> model.fit([[1], [2], [3]], [4, 5, 6])
>>> # Convert to DummyRegressorSolution
>>> solution = DummyRegressorSolution.from_model(model)
>>> # Convert back to scikit-learn model
>>> model_restored = solution.to_model()
constant_
class-attribute
instance-attribute
¶
Mean or median or quantile of the training targets or constant value given by the user.
feature_names_in_
class-attribute
instance-attribute
¶
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]) -> DummyRegressorSolution
Creates a DummyRegressorSolution instance from a dictionary.
This method creates an instance of DummyRegressorSolution from a dictionary containing model attributes. It converts any array-like attributes to numpy arrays.
PARAMETER | DESCRIPTION |
---|---|
|
Dictionary containing the model attributes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DummyRegressorSolution
|
Instance of DummyRegressorSolution. |
Examples:
>>> from nextmv_sklearn.dummy import DummyRegressorSolution
>>> data = {
... 'constant_': [5.0],
... 'n_features_in_': 1,
... 'feature_names_in_': ['x'],
... 'n_outputs_': 1
... }
>>> solution = DummyRegressorSolution.from_dict(data)
>>> solution.n_features_in_
1
Source code in nextmv-scikit-learn/nextmv_sklearn/dummy/solution.py
from_model
classmethod
¶
from_model(model: DummyRegressor) -> DummyRegressorSolution
Creates a DummyRegressorSolution instance from a scikit-learn DummyRegressor model.
This method extracts relevant attributes from a scikit-learn DummyRegressor model and creates a DummyRegressorSolution instance with those attributes.
PARAMETER | DESCRIPTION |
---|---|
|
scikit-learn DummyRegressor model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DummyRegressorSolution
|
Instance of DummyRegressorSolution containing the model's attributes. |
Examples:
>>> from sklearn.dummy import DummyRegressor
>>> from nextmv_sklearn.dummy import DummyRegressorSolution
>>> # Create and fit a scikit-learn model
>>> model = DummyRegressor().fit([[1, 2], [3, 4]], [5, 6])
>>> # Convert to DummyRegressorSolution
>>> solution = DummyRegressorSolution.from_model(model)
>>> solution.n_features_in_
2
Source code in nextmv-scikit-learn/nextmv_sklearn/dummy/solution.py
model_config
class-attribute
instance-attribute
¶
n_features_in_
class-attribute
instance-attribute
¶
Number of features seen during fit.
to_dict
¶
Convert a data model instance to a dict with associated class info.
This method converts the instance to a dictionary that includes class information (module and name) and the model's attributes.
RETURNS | DESCRIPTION |
---|---|
dict
|
Dictionary containing class information and attributes of the model. |
Examples:
>>> from sklearn.dummy import DummyRegressor
>>> from nextmv_sklearn.dummy import DummyRegressorSolution
>>> model = DummyRegressor().fit([[1], [2]], [1, 2])
>>> solution = DummyRegressorSolution.from_model(model)
>>> solution_dict = solution.to_dict()
>>> print(solution_dict['class']['name'])
'DummyRegressorSolution'
Source code in nextmv-scikit-learn/nextmv_sklearn/dummy/solution.py
to_model
¶
Transforms the DummyRegressorSolution instance into a scikit-learn DummyRegressor model.
This method creates a new scikit-learn DummyRegressor model and sets its attributes based on the current instance's attributes.
RETURNS | DESCRIPTION |
---|---|
DummyRegressor
|
scikit-learn DummyRegressor model with attributes copied from this instance. |
Examples:
>>> from sklearn.dummy import DummyRegressor
>>> from nextmv_sklearn.dummy import DummyRegressorSolution
>>> # Create a solution instance
>>> solution = DummyRegressorSolution(
... constant_=np.array([5.0]),
... n_features_in_=2,
... n_outputs_=1
... )
>>> # Convert to scikit-learn model
>>> model = solution.to_model()
>>> isinstance(model, DummyRegressor)
True