Create Pipeline Steps in Neuraxle that doesn’t fit or transform¶
If a pipeline step doesn’t need to be fitted and only transforms data (e.g.: taking the logarithm of the data), then you can inherit from the NonFittableMixin as demonstrated here, which will override the fit method properly for you. You can also use a NonTransformableMixin if your step doesn’t transform anything, which is rarer. If your step simply just does nothing to the data, then you could even use the Identity class of Neuraxle, which is simply a class that inherits from both the NonFittableMixin, the NonTransformableMixin, and BaseStep.
Mixins are an old Object Oriented Programming (OOP) design pattern that resurfaces when designing Machine Learning Pipelines. Those are add-ons to classes to implement some methods in some specific ways already. A mixin doesn’t inherit from BaseStep itself, because we can combine many of them in one class. However, a mixin must suppose that the object that inherits from the mixin also inherits from it’s base class. Here, our base class is the BaseStep class.
import numpy as np from neuraxle.base import NonTransformableMixin, NonFittableMixin, Identity, BaseStep from neuraxle.pipeline import Pipeline class NonFittableStep(NonFittableMixin, BaseStep): """ Fit method is automatically implemented as changing nothing. Please make your steps inherit from NonFittableMixin, when they don't need any transformations. Also, make sure that BaseStep is the last step you inherit from. """ def transform(self, data_inputs): # insert your transform code here print("NonFittableStep: I transformed.") return self, data_inputs def inverse_transform(self, processed_outputs): # insert your inverse transform code here print("NonFittableStep: I inverse transformed.") return processed_outputs class NonTransformableStep(NonTransformableMixin, BaseStep): """ Transform method is automatically implemented as returning data inputs as it is. Please make your steps inherit from NonTransformableMixin, when they don't need any transformations. Also, make sure that BaseStep is the last step you inherit from. """ def fit(self, data_inputs, expected_outputs=None) -> 'NonTransformableStep': # insert your fit code here print("NonTransformableStep: I fitted.") return self def main(): p = Pipeline([ NonFittableStep(), NonTransformableStep(), Identity() # Note: Identity does nothing: it inherits from both NonFittableMixin and NonTransformableMixin. ]) p = p.fit(np.array([0, 1]), np.array([0, 1])) out = p.transform(np.array([0, 1])) if __name__ == "__main__": main()
Total running time of the script: ( 0 minutes 0.000 seconds)