craft#
- craft(spec)[source]#
Instantiate an object from the specification string.
The
craftutility can be used to deserialize an estimator specification string, including composites such as pipelines.It takes a specification string and returns an
sktimeestimator, class, or object, corresponding to that string.Specification strings can be:
simple expressions such as
"NaiveForecaster"or"NaiveForecaster(sp=2)"compositions such as
"Deseasonalizer() * NaiveForecaster()"a longer block of code, closing with a return statement, e.g., the string block
deseason = Deseasonalizer() naive = NaiveForecaster() return deseason * naive
The
craftutility is useful as a serialization / deserialization pair, together withstrcoercion (or commandline printing) of an unfitted estimator.craftrecognizes estimators present insktimeandscikit-learn, and base python.- Parameters:
- specstr, sktime/skbase compatible object specification
i.e., a string that executes to construct an object if all imports were present imports inferred are of any classes in the scope of
all_estimatorsoption 1: a string that evaluates to an estimator
option 2: a sequence of assignments in valid python code, with the object to be defined preceded by a “return”. assignments can use names of classes as if all imports were present
- Returns:
- objskbase BaseObject descendant, constructed from
spec this will have the property that
spec == str(obj)(up to formatting)
- objskbase BaseObject descendant, constructed from
Examples
>>> from sktime.registry import craft
Example 1: simple estimator
serialized as the string
"NaiveForecaster(sp=2)"deserialized as the estimator
NaiveForecaster(sp=2)
>>> spec = "NaiveForecaster(sp=2)" >>> est = craft(spec) >>> print(est) NaiveForecaster(sp=2)
Example 2: composite estimator
serialized as the string
"Deseasonalizer() * NaiveForecaster()"deserialized as the estimator
Deseasonalizer() * NaiveForecaster(), same asForecastingPipeline([Deseasonalizer(), NaiveForecaster()])
>>> spec = "Deseasonalizer() * NaiveForecaster()" >>> est = craft(spec)
Example 3: longer code block
serialized as a code block with assignments and return statement
deserialized as the estimator defined in the return statement
>>> spec = ''' ... deseason = Deseasonalizer() ... naive = NaiveForecaster() ... return deseason * naive ... ''' >>> est = craft(spec)