-
Notifications
You must be signed in to change notification settings - Fork 311
/
data.py
1122 lines (896 loc) · 38.7 KB
/
data.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 the 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.
#
from sklearn.preprocessing.data import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler, MinMaxScaler, Imputer, Binarizer, PolynomialFeatures
from sklearn.preprocessing.data import OneHotEncoder
from sklearn.preprocessing.label import LabelEncoder
from mleap.bundle.serialize import MLeapSerializer, MLeapDeserializer, Vector
from sklearn.utils import column_or_1d
from sklearn.utils.validation import check_is_fitted
from sklearn.utils.fixes import np_version
import warnings
import numpy as np
import pandas as pd
import uuid
import os
import shutil
import json
class ops(object):
def __init__(self):
self.STANDARD_SCALER = 'standard_scaler'
self.MIN_MAX_SCALER = 'min_max_scaler'
self.LABEL_ENCODER = 'string_indexer'
self.ONE_HOT_ENCODER = 'one_hot_encoder'
self.IMPUTER = 'imputer'
self.NDARRAYTODATAFRAME = 'one_dim_array_to_dataframe'
self.TODENSE = 'dense_transformer'
self.BINARIZER = 'sklearn_binarizer'
self.POLYNOMIALEXPANSION = 'sklearn_polynomial_expansion'
ops = ops()
def _check_numpy_unicode_bug(labels):
"""Check that user is not subject to an old numpy bug
Fixed in master before 1.7.0:
https://github.com/numpy/numpy/pull/243
"""
if np_version[:3] < (1, 7, 0) and labels.dtype.kind == 'U':
raise RuntimeError("NumPy < 1.7.0 does not implement searchsorted"
" on unicode data correctly. Please upgrade"
" NumPy to use LabelEncoder with unicode inputs.")
def serialize_to_bundle(self, path, model_name):
serializer = SimpleSerializer()
return serializer.serialize_to_bundle(self, path, model_name)
def deserialize_from_bundle(self, path, node_name):
serializer = SimpleSerializer()
return serializer.deserialize_from_bundle(self, path, node_name)
def mleap_init(self, prior_tf, output_features=None):
self.name = "{}_{}".format(self.op, uuid.uuid1())
if prior_tf.op == 'vector_assembler':
self.input_features = prior_tf.output_vector
output_shape = 'tensor'
output_size = len(prior_tf.input_features)
output_feature_name = prior_tf.output_vector
else:
self.input_features = prior_tf.output_features
output_shape = 'scalar'
output_size = 1
output_feature_name = prior_tf.output_features
class_name = self.__class__.__name__
if output_features is not None:
self.output_features = output_features
else:
self.output_features = "{}_{}".format(output_feature_name, class_name.lower())
if class_name == 'PolynomialFeatures':
self.input_size = len(prior_tf.input_features)
elif output_shape == 'tensor':
self.input_shapes = {'data_shape': [{'shape':'tensor',
"tensor_shape": {"dimensions": [{"size": output_size}]}}]}
else:
self.input_shapes = {'data_shape': [{'shape': output_shape}]}
setattr(StandardScaler, 'op', ops.STANDARD_SCALER)
setattr(StandardScaler, 'serialize_to_bundle', serialize_to_bundle)
setattr(StandardScaler, 'deserialize_from_bundle', deserialize_from_bundle)
setattr(StandardScaler, 'mlinit', mleap_init)
setattr(StandardScaler, 'serializable', True)
setattr(MinMaxScaler, 'op', ops.MIN_MAX_SCALER)
setattr(MinMaxScaler, 'mlinit', mleap_init)
setattr(MinMaxScaler, 'serialize_to_bundle', serialize_to_bundle)
setattr(MinMaxScaler, 'deserialize_from_bundle', deserialize_from_bundle)
setattr(MinMaxScaler, 'serializable', True)
setattr(Imputer, 'op', ops.IMPUTER)
setattr(Imputer, 'mlinit', mleap_init)
setattr(Imputer, 'serialize_to_bundle', serialize_to_bundle)
setattr(Imputer, 'serializable', True)
setattr(OneHotEncoder, 'op', ops.ONE_HOT_ENCODER)
setattr(OneHotEncoder, 'mlinit', mleap_init)
setattr(OneHotEncoder, 'serialize_to_bundle', serialize_to_bundle)
setattr(OneHotEncoder, 'deserialize_from_bundle', deserialize_from_bundle)
setattr(OneHotEncoder, 'serializable', True)
setattr(Binarizer, 'op', ops.BINARIZER)
setattr(Binarizer, 'mlinit', mleap_init)
setattr(Binarizer, 'serialize_to_bundle', serialize_to_bundle)
setattr(Binarizer, 'deserialize_from_bundle', deserialize_from_bundle)
setattr(Binarizer, 'serializable', True)
setattr(PolynomialFeatures, 'op', ops.POLYNOMIALEXPANSION)
setattr(PolynomialFeatures, 'mlinit', mleap_init)
setattr(PolynomialFeatures, 'serialize_to_bundle', serialize_to_bundle)
setattr(PolynomialFeatures, 'deserialize_from_bundle', deserialize_from_bundle)
setattr(PolynomialFeatures, 'serializable', True)
class SimpleSerializer(object):
"""
Extends base scikit-learn transformers to include:
- set_input_features
- set_output_features
- serialize_to_bundle
methods. The set input/output features are required to maintain parity with the Spark/MLeap execution engine.
"""
def __init__(self):
super(SimpleSerializer, self).__init__()
@staticmethod
def _choose_serializer(transformer):
serializer = None
if transformer.op == ops.STANDARD_SCALER:
serializer = StandardScalerSerializer()
elif transformer.op == ops.MIN_MAX_SCALER:
serializer = MinMaxScalerSerializer()
elif transformer.op == ops.ONE_HOT_ENCODER:
serializer = OneHotEncoderSerializer()
elif transformer.op == ops.IMPUTER:
serializer = ImputerSerializer()
elif transformer.op == ops.BINARIZER:
serializer = BinarizerSerializer()
elif transformer.op == ops.POLYNOMIALEXPANSION:
serializer = PolynomialExpansionSerializer()
return serializer
@staticmethod
def set_input_features(transformer, input_features):
transformer.input_features = input_features
@staticmethod
def set_output_features(transformer, output_features):
transformer.output_features = output_features
def serialize_to_bundle(self, transformer, path, model_name):
serializer = self._choose_serializer(transformer)
serializer.serialize_to_bundle(transformer, path, model_name)
def deserialize_from_bundle(self, transformer, path, node_name):
deserializer = self._choose_serializer(transformer)
transformer = deserializer.deserialize_from_bundle(transformer, path, node_name)
return transformer
def _to_list(x):
if isinstance(x, list):
return x
return list(x)
class FeatureExtractor(BaseEstimator, TransformerMixin, MLeapSerializer):
def __init__(self, input_scalars=None, input_vectors=None, output_vector=None, output_vector_items=None):
"""
Selects a subset of features from a pandas dataframe that are then passed into a subsequent transformer.
MLeap treats this transformer like a VectorAssembler equivalent in spark.
>>> data = pd.DataFrame([['a', 0, 1], ['b', 1, 2], ['c', 3, 4]], columns=['col_a', 'col_b', 'col_c'])
>>> vector_items = ['col_b', 'col_c']
>>> input_shapes = {'data_shape': [{'shape':'scalar'}, {'shape':'scalar'}]}
>>> input_shapes = {'data_shape': [{'shape':'tensor', "tensor_shape": {"dimensions": [{"size": 23}]}}]}
>>> feature_extractor2_tf = FeatureExtractor(vector_items, 'continuous_features', vector_items, input_shapes)
>>> feature_extractor2_tf.fit_transform(data).head(1).values
>>> array([[0, 1]])
:param input_vectors: List of scalar feature names that are being extracted from a DataFrame
:param input_vectors: List of FeatureExtractors that were used to generate the input vectors
:param input_features: List of features to extracts from a pandas data frame
:param output_vector: Name of the output vector, only used for serialization
:param output_vector_items: List of output feature names
:param input_shapes: the shape of each input feature, whether it is scalar or a vector
if it's a vector, then we include size information of the vector
:return:
"""
self.input_scalars = input_scalars
self.input_vectors = input_vectors
self.input_features = self.get_input_features(input_scalars, input_vectors)
self.output_vector_items = output_vector_items
self.output_vector = output_vector
self.op = 'vector_assembler'
self.name = "{}_{}".format(self.op, uuid.uuid1())
self.dtypes = None
self.serializable = True
self.skip_fit_transform = False
self.input_size = None
self.input_shapes = None
def get_input_features(self, input_scalars, input_vectors):
if input_scalars is not None:
return input_scalars
elif input_vectors is not None and isinstance(input_vectors, list) and len(input_vectors)>0:
features = list()
for x in input_vectors:
if 'output_vector' in x.__dict__:
features.append(x.output_vector)
elif 'output_features' in x.__dict__:
features.append(x.output_features)
return features
else:
raise BaseException("Unable To Define Input Features")
def transform(self, df, **params):
if not self.skip_fit_transform:
return df[self.input_features]
return df
def assign_input_shapes(self, X):
# Figure out the data shape
if self.input_scalars is not None and isinstance(X, pd.DataFrame):
self.input_shapes = {'data_shape': [{'shape': 'scalar'}]*len(self.input_features)}
elif self.input_vectors is not None:
self.input_shapes = {'data_shape': []}
for vector in self.input_vectors:
if vector.op not in [ops.ONE_HOT_ENCODER, ops.POLYNOMIALEXPANSION]:
shape = {'shape': 'tensor', "tensor_shape": {"dimensions": [{"size": len(vector.input_features)}]}}
self.input_shapes['data_shape'].append(shape)
elif vector.op == ops.ONE_HOT_ENCODER:
shape = {'shape': 'tensor', "tensor_shape": {"dimensions": [{"size": vector.n_values_[0] - 1}]}}
self.input_shapes['data_shape'].append(shape)
return self
def fit(self, X, y=None, **fit_params):
self.assign_input_shapes(X)
if not self.skip_fit_transform:
self.dtypes = X[self.input_features].dtypes.to_dict()
if len([x for x in self.dtypes.values() if x.type == np.object_]) != 0:
self.serializable = False
return self
def fit_transform(self, X, y=None, **fit_params):
if not self.skip_fit_transform:
self.fit(X)
else:
self.assign_input_shapes(X)
df_subset = self.transform(X)
return df_subset
def serialize_to_bundle(self, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(("input_shapes", self.input_shapes))
# define node inputs and outputs
inputs = [{'name': x, 'port': 'input{}'.format(self.input_features.index(x))} for x in self.input_features]
outputs = [{
"name": self.output_vector,
"port": "output"
}]
self.serialize(self, path, model_name, attributes, inputs, outputs)
class LabelEncoder(BaseEstimator, TransformerMixin, MLeapSerializer, MLeapDeserializer):
"""
Copied from sklearn, but enables passing X and Y features, which allows this transformer
to be used in Pipelines.
Converts categorical values of a single column into categorical indices. This transformer should be followed by a
NDArrayToDataFrame transformer to maintain a data structure required by scikit pipelines.
NOTE: You can only LabelEncode/String Index one feature at a time!!!
>>> data = pd.DataFrame([['a', 0], ['b', 1], ['b', 3], ['c', 1]], columns=['col_a', 'col_b'])
>>> # Label Encoder for x1 Label
>>> label_encoder_tf = LabelEncoder(input_features = ['col_a'] , output_features='col_a_label_le')
>>> # Convert output of Label Encoder to Data Frame instead of 1d-array
>>> n_dim_array_to_df_tf = NDArrayToDataFrame('col_a_label_le')
>>> n_dim_array_to_df_tf.fit_transform(label_encoder_tf.fit_transform(data['col_a']))
Encode labels with value between 0 and n_classes-1.
Read more in the :ref:`User Guide <preprocessing_targets>`.
Attributes
----------
classes_ : array of shape (n_class,)
Holds the label for each class.
Examples
--------
`LabelEncoder` can be used to normalize labels.
>>> from sklearn import preprocessing
>>> le = preprocessing.LabelEncoder()
>>> le.fit([1, 2, 2, 6])
LabelEncoder()
>>> le.classes_
array([1, 2, 6])
>>> le.transform([1, 1, 2, 6]) #doctest: +ELLIPSIS
array([0, 0, 1, 2]...)
>>> le.inverse_transform([0, 0, 1, 2])
array([1, 1, 2, 6])
It can also be used to transform non-numerical labels (as long as they are
hashable and comparable) to numerical labels.
>>> le = preprocessing.LabelEncoder()
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()
>>> list(le.classes_)
['amsterdam', 'paris', 'tokyo']
>>> le.transform(["tokyo", "tokyo", "paris"]) #doctest: +ELLIPSIS
array([2, 2, 1]...)
>>> list(le.inverse_transform([2, 2, 1]))
['tokyo', 'tokyo', 'paris']
"""
def __init__(self, input_features=None, output_features=None):
self.input_features = input_features
self.output_features = output_features
self.op = 'string_indexer'
self.name = "{}_{}".format(self.op, uuid.uuid1())
self.serializable = True
def fit(self, X):
"""Fit label encoder
Parameters
----------
y : array-like of shape (n_samples,)
Target values.
Returns
-------
self : returns an instance of self.
"""
X = column_or_1d(X, warn=True)
_check_numpy_unicode_bug(X)
self.classes_ = np.unique(X)
return self
def fit_transform(self, X, y=None, **fit_params):
"""Fit label encoder and return encoded labels
Parameters
----------
y : array-like of shape [n_samples]
Target values.
Returns
-------
y : array-like of shape [n_samples]
"""
y = column_or_1d(X, warn=True)
_check_numpy_unicode_bug(X)
self.classes_, X = np.unique(X, return_inverse=True)
return X
def transform(self, y):
"""Transform labels to normalized encoding.
Parameters
----------
y : array-like of shape [n_samples]
Target values.
Returns
-------
y : array-like of shape [n_samples]
"""
check_is_fitted(self, 'classes_')
y = column_or_1d(y, warn=True)
classes = np.unique(y)
_check_numpy_unicode_bug(classes)
if len(np.intersect1d(classes, self.classes_)) < len(classes):
diff = np.setdiff1d(classes, self.classes_)
raise ValueError("y contains new labels: %s" % str(diff))
return np.searchsorted(self.classes_, y)
def inverse_transform(self, y):
"""Transform labels back to original encoding.
Parameters
----------
y : numpy array of shape [n_samples]
Target values.
Returns
-------
y : numpy array of shape [n_samples]
"""
check_is_fitted(self, 'classes_')
diff = np.setdiff1d(y, np.arange(len(self.classes_)))
if diff:
raise ValueError("y contains new labels: %s" % str(diff))
y = np.asarray(y)
return self.classes_[y]
def serialize_to_bundle(self, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('labels', self.classes_.tolist()))
attributes.append(('nullable_input', False))
# define node inputs and outputs
inputs = [{
"name": self.input_features[0],
"port": "input"
}]
outputs = [{
"name": self.output_features,
"port": "output"
}]
self.serialize(self, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, node_path, node_name):
attributes_map = {
'labels': 'classes_'
}
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(self, full_node_path, attributes_map)
return transformer
class MinMaxScalerSerializer(MLeapSerializer, MLeapDeserializer):
"""
Scales features by the range of values using calculated min and max from training data.
>>> data = pd.DataFrame([[1], [5], [6], [1]], columns=['col_a'])
>>> minmax_scaler_tf = MinMaxScaler()
>>> minmax_scaler_tf.mlinit(input_features='col_a', output_features='scaled_cont_features')
>>> minmax_scaler_tf.fit_transform(data)
>>> array([[ 0.],
>>> [ 0.8],
>>> [ 1.],
>>> [ 0.]])
"""
def __init__(self):
super(MinMaxScalerSerializer, self).__init__()
def serialize_to_bundle(self, transformer, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('min', _to_list(transformer.data_min_)))
attributes.append(('max', _to_list(transformer.data_max_)))
# define node inputs and outputs
inputs = [{
"name": transformer.input_features,
"port": "input"
}]
outputs = [{
"name": transformer.output_features,
"port": "output"
}]
self.serialize(transformer, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, transformer, node_path, node_name):
# Default (and only range supported)
feature_range = (0, 1)
attributes_map = {
'min': 'data_min_',
'max': 'data_max_'
}
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(transformer, full_node_path, attributes_map)
transformer.data_range_ = np.array(transformer.data_max_) - np.array(transformer.data_min_)
transformer.scale_ = ((feature_range[1] - feature_range[0]) / transformer.data_range_)
transformer.min_ = feature_range[0] - transformer.data_min_ * transformer.scale_
return transformer
class ImputerSerializer(MLeapSerializer):
def __init__(self):
super(ImputerSerializer, self).__init__()
self.serializable = False
def serialize_to_bundle(self, transformer, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('strategy', transformer.strategy))
attributes.append(('surrogate_value', transformer.statistics_.tolist()[0]))
if transformer.missing_values is not "NaN":
attributes.append(('missing_value', transformer.missing_values))
# define node inputs and outputs
inputs = [{
"name": transformer.input_features,
"port": "input"
}]
outputs = [{
"name": transformer.output_features,
"port": "output"
}]
self.serialize(transformer, path, model_name, attributes, inputs, outputs)
class StandardScalerSerializer(MLeapSerializer, MLeapDeserializer):
"""
Standardizes features by removing the mean and scaling to unit variance using mean and standard deviation from
training data.
>>> data = pd.DataFrame([[1], [5], [6], [1]], columns=['col_a'])
>>> standard_scaler_tf = StandardScaler()
>>> standard_scaler_tf.mlinit(input_features='col_a', output_features='scaled_cont_features')
>>> standard_scaler_tf.fit_transform(data)
>>> array([[-0.98787834],
>>> [ 0.76834982],
>>> [ 1.20740686],
>>> [-0.98787834]])
"""
def __init__(self):
super(StandardScalerSerializer, self).__init__()
def serialize_to_bundle(self, transformer, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('mean', transformer.mean_.tolist()))
attributes.append(('std', [np.sqrt(x) for x in transformer.var_]))
# define node inputs and outputs
inputs = [{
"name": transformer.input_features,
"port": "input"
}]
outputs = [{
"name": transformer.output_features,
"port": "output"
}]
self.serialize(transformer, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, transformer, node_path, node_name):
attributes_map = {
'mean': 'mean_',
'std': 'scale_'
}
# Set serialized attributes
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(transformer, full_node_path, attributes_map)
# Set Additional Attributes
if 'mean_' in transformer.__dict__:
transformer.with_mean = True
else:
transformer.with_mean = False
if 'scale_' in transformer.__dict__:
transformer.with_std = True
transformer.var = np.square(transformer.scale_)
else:
transformer.with_std = False
return transformer
class OneHotEncoderSerializer(MLeapSerializer, MLeapDeserializer):
"""
A one-hot encoder maps a single column of categorical indices to a
column of binary vectors, which can be re-assamble back to a DataFrame using a ToDense transformer.
"""
def __init__(self):
super(OneHotEncoderSerializer, self).__init__()
def serialize_to_bundle(self, transformer, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('size', transformer.n_values_.tolist()[0]))
# the default sklearn OneHotEncoder doesn't support 'drop_last'
# see mleap.sklearn.extensions.data for OneHotEncoder that does support 'drop_last'
attributes.append(('drop_last', False))
# define node inputs and outputs
inputs = [{
"name": transformer.input_features,
"port": "input"
}]
outputs = [{
"name": transformer.output_features,
"port": "output"
}]
self.serialize(transformer, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, transformer, node_path, node_name):
attributes_map = {
'size': 'n_values_'
}
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(transformer, full_node_path, attributes_map)
# Set Sparse = False
transformer.sparse = False
# Set Feature Indices
n_values = np.hstack([[0], [transformer.n_values_]])
indices = np.cumsum(n_values)
transformer.feature_indices_ = indices
transformer.active_features_ = range(0, transformer.n_values_)
return transformer
class BinarizerSerializer(MLeapSerializer, MLeapDeserializer):
def __init__(self):
super(BinarizerSerializer, self).__init__()
def serialize_to_bundle(self, transformer, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('threshold', float(transformer.threshold)))
attributes.append(("input_shapes", transformer.input_shapes))
# define node inputs and outputs
inputs = [{
"name": transformer.input_features,
"port": "input"
}]
outputs = [{
"name": transformer.output_features,
"port": "output"
}]
self.serialize(transformer, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, transformer, node_path, node_name):
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(transformer, full_node_path)
return transformer
class PolynomialExpansionSerializer(MLeapSerializer, MLeapDeserializer):
def __init__(self):
super(PolynomialExpansionSerializer, self).__init__()
def serialize_to_bundle(self, transformer, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('combinations', str(transformer.get_feature_names()).replace("'", "").replace(", ", ",")))
# define node inputs and outputs
inputs = [{
"name": transformer.input_features,
"port": "input"
}]
outputs = [{
"name": transformer.output_features,
"port": "output"
}]
self.serialize(transformer, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, transformer, node_path, node_name):
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(transformer, full_node_path)
transformer.include_bias = False
# Get number of input features
with open("{}/node.json".format(full_node_path)) as json_data:
node_j = json.load(json_data)
# Set powers
transformer.n_input_features_ = len(node_j['shape']['inputs'][0]['name']) # TODO: Make this dynamic
transformer.interaction_only = False
transformer.n_output_features_ = sum(1 for _ in transformer._combinations(transformer.n_input_features_,
transformer.degree,
transformer.interaction_only,
transformer.include_bias))
return transformer
class ReshapeArrayToN1(BaseEstimator, TransformerMixin):
def __init__(self):
self.serializable=False
self.op='reshape_array'
self.name = "{}_{}".format(self.op, uuid.uuid1())
def transform(self, X, **params):
"""
:type X: np.ndarray
:param X:
:param params:
:return:
"""
return X.reshape(X.size, 1)
def fit(self, df, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
return self.transform(X)
class NDArrayToDataFrame(BaseEstimator, TransformerMixin):
def __init__(self, input_features):
self.input_features = input_features
self.output_features = input_features
self.op = 'one_dim_array_to_dataframe'
self.name = "{}_{}".format(self.op, uuid.uuid1())
def transform(self, X, **params):
if isinstance(X, np.ndarray):
return pd.DataFrame(X, columns=[self.input_features])
return pd.DataFrame(X.todense(), columns=[self.input_features])
def fit(self, df, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
return self.transform(X)
def get_mleap_model(self):
js = {
"op": self.op
}
return js
def get_mleap_node(self):
js = {
"name": self.name,
"shape": {
"inputs": [{'name': x, 'port': 'input{}'.format(self.input_features.index(x))} for x in self.input_features],
"outputs": [{
"name": self.output_features,
"port": "output"
}]
}
}
return js
def _serialize_to_bundle(self, path, model_name):
# If bundle path already exists, delte it and create a clean directory
if os.path.exists("{}/{}".format(path, model_name)):
shutil.rmtree("{}/{}".format(path, model_name))
model_dir = "{}/{}".format(path, model_name)
os.mkdir(model_dir)
# Write bundle file
with open("{}/{}".format(model_dir, 'model.json'), 'w') as outfile:
json.dump(self.get_mleap_model(), outfile, indent=3)
# Write node file
with open("{}/{}".format(model_dir, 'node.json'), 'w') as outfile:
json.dump(self.get_mleap_node(), outfile, indent=3)
class ToDense(BaseEstimator, TransformerMixin):
def __init__(self, input_features):
self.op = 'dense_transformer'
self.name = "{}_{}".format(self.op, uuid.uuid1())
self.input_features = input_features
self.output_features = input_features
def transform(self, X, **params):
return X.todense()
def fit(self, df, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
return self.transform(X)
def get_mleap_model(self):
js = {
"op": self.op
}
return js
def get_mleap_node(self):
js = {
"name": self.name,
"shape": {
"inputs": {'name': self.input_features, 'port': 'input0'},
"outputs": [{
"name": self.output_features,
"port": "output"
}]
}
}
return js
def _serialize_to_bundle(self, path, model_name):
# If bundle path already exists, delte it and create a clean directory
if os.path.exists("{}/{}".format(path, model_name)):
shutil.rmtree("{}/{}".format(path, model_name))
model_dir = "{}/{}".format(path, model_name)
os.mkdir(model_dir)
# Write bundle file
with open("{}/{}".format(model_dir, 'model.json'), 'w') as outfile:
json.dump(self.get_mleap_model(), outfile, indent=3)
# Write node file
with open("{}/{}".format(model_dir, 'node.json'), 'w') as outfile:
json.dump(self.get_mleap_node(), outfile, indent=3)
class MathUnary(BaseEstimator, TransformerMixin, MLeapSerializer, MLeapDeserializer):
"""
Performs basic math operations on a single feature (column of a DataFrame). Supported operations include:
- log
- exp
- sqrt
- sin
- cos
- tan
Note, currently we only support 1d-arrays.
Inputs need to be floats.
"""
def __init__(self, input_features=None, output_features=None, transform_type=None):
self.valid_transforms = ['log', 'exp', 'sqrt', 'sin', 'cos', 'tan']
self.op = 'math_unary'
self.name = "{}_{}".format(self.op, uuid.uuid4())
self.input_features = input_features
self.output_features = output_features
self.transform_type = transform_type
self.serializable = True
def fit(self, X, y=None, **fit_params):
"""
Fit Unary Math Operator
:param X:
:return:
"""
X = column_or_1d(X, warn=True)
if self.transform_type not in self.valid_transforms:
warnings.warn("Invalid transform type.", stacklevel=2)
return self
def transform(self, y):
"""
Transform features per specified math function.
:param y:
:return:
"""
if self.transform_type == 'log':
return np.log(y)
elif self.transform_type == 'exp':
return np.exp(y)
elif self.transform_type == 'sqrt':
return np.sqrt(y)
elif self.transform_type == 'sin':
return np.sin(y)
elif self.transform_type == 'cos':
return np.cos(y)
elif self.transform_type == 'tan':
return np.tan(y)
def fit_transform(self, X, y=None, **fit_params):
"""
Transform data per specified math function.
:param X:
:param y:
:param fit_params:
:return:
"""
self.fit(X)
return self.transform(X)
def serialize_to_bundle(self, path, model_name):
# compile tuples of model attributes to serialize
attributes = list()
attributes.append(('operation', self.transform_type))
# define node inputs and outputs
inputs = [{
"name": self.input_features[0],
"port": "input"
}]
outputs = [{
"name": self.output_features,
"port": "output"
}]
self.serialize(self, path, model_name, attributes, inputs, outputs)
def deserialize_from_bundle(self, node_path, node_name):
attributes_map = {
'operation': 'transform_type'
}
full_node_path = os.path.join(node_path, node_name)
transformer = self.deserialize_single_input_output(self, full_node_path, attributes_map)
return transformer
class MathBinary(BaseEstimator, TransformerMixin, MLeapSerializer, MLeapDeserializer):
"""
Performs basic math operations on two features (columns of a DataFrame). Supported operations include:
- add: Add x + y
- sub: Subtract x - y
- mul: Multiply x * y
- div: Divide x / y
- rem: Remainder x % y
- logn: LogN log(x) / log(y)
- pow: Power x^y
These transforms work on 2-dimensional arrays/vectors, where the the first column is x and second column is y.
Inputs need to be floats.
"""
def __init__(self, input_features=None, output_features=None, transform_type=None):
self.valid_transforms = ['add', 'sub', 'mul', 'div', 'rem', 'logn', 'pow']
self.op = 'math_binary'
self.name = "{}_{}".format(self.op, uuid.uuid4())
self.input_features = input_features
self.output_features = output_features
self.transform_type = transform_type
self.serializable = True
def _return(self, y):
if type(y, np.ndarray):
return
def fit(self, X, y=None, **fit_params):
"""
Fit Unary Math Operator
:param y:
:return:
"""
if self.transform_type not in self.valid_transforms:
warnings.warn("Invalid transform type.", stacklevel=2)
return self