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.
NonFittableStep: I transformed. NonTransformableStep: I fitted. NonFittableStep: I transformed.
import numpy as np from neuraxle.base import NonTransformableMixin, Identity, BaseStep, NonFittableMixin 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 fitting. Also, make sure that BaseStep is the last step you inherit from. Note that we could also define the inverse_transform method in the present object. """ def transform(self, data_inputs): # insert your transform code here print("NonFittableStep: I transformed.") return data_inputs 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. ]) some_data = np.array([0, 1]) p = p.fit(some_data) # Out: # NonFittableStep: I transformed. # NonTransformableStep: I fitted. out = p.transform(some_data) # Out: # NonFittableStep: I transformed. assert np.array_equal(out, some_data) # Data is unchanged as we've done nothing in the only transform. if __name__ == "__main__": main()
Total running time of the script: ( 0 minutes 0.053 seconds)