-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
plot_non_fittable_mixin.py
92 lines (70 loc) · 3.67 KB
/
plot_non_fittable_mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
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.
..
Copyright 2019, Neuraxio Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of tche License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
..
Thanks to Umaneo Technologies Inc. for their contributions to this Machine Learning
project, visit https://www.umaneo.com/ for more information on Umaneo Technologies Inc.
"""
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()