-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLabelEncoder.ecl
More file actions
206 lines (184 loc) · 6.79 KB
/
LabelEncoder.ecl
File metadata and controls
206 lines (184 loc) · 6.79 KB
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
/*##############################################################################
## HPCC SYSTEMS software Copyright (C) 2020 HPCC Systems®. All rights reserved.
############################################################################## */
/**
* Convert categorical values into discrete numbers
* in the range [0 ..(n - 1)] where n is the number of categories of a feature.
*/
EXPORT LabelEncoder := MODULE
/**
* Builds a mapping between feature names and categories.
*
* @param dataForUndefinedCategories: any record-oriented dataset.
* <p>The data from which the categories are extracted
* if not predefined in the list of categorical features.
*
* @param partialKey: same record structure as the key (see below).
* <p> Mapping between feature names and categories.
* Some names are mapped to empty categories such that
* their categories could be extracted from dataForUndefinedCategories.
* Names which are mapped to non-empty categories will be assigned the same categories.
*
* @return key: DATASET(KeyLayout)
* <p>The full mapping between categorical feature names and their categories.
* Its record structure has the following format:
* <p>
* <pre>
* KeyLayout := RECORD
* SET OF STRING <name of categorical feature 1>;
* SET OF STRING <name of categorical feature 2>;
* ...
* SET OF STRING <name of categorical feature n>;
* END;
* </pre>
*/
EXPORT GetKey(dataForUndefinedCategories, partialKey) := FUNCTIONMACRO
IMPORT ML_Core;
Utl := ML_Core.Preprocessing.Utils;
KeyLayout := RECORDOF(partialKey);
#EXPORTXML(KeyMetaInfo, partialKey)
dta := #TEXT(dataForUndefinedCategories);
KeyLayout completeKey(KeyLayout L) := TRANSFORM
#FOR(KeyMetaInfo)
#FOR(field)
#EXPAND('SELF.' + %'@label'% + ' := IF(EXISTS(L.' + %'@label'% + '), '
+ 'L.' + %'@label'% + ','
+ 'Utl.GetCategories(' + dta + ',' + %'@label'% + '))');
#END
#END
END;
Result := PROJECT(partialKey, completeKey(LEFT));
RETURN Result;
ENDMACRO;
/**
* Builds a lookup table that maps each category of a feature to a unique number.
* Each category is assigned its index in the category set.
*
* @param key: DATASET(KeyLayout).
* <p> Mapping between feature names and categories.
*
* @return categoriesMapping: DATASET(MappingLayout).
* <p> A table with each feature name mapped to its categories and each category
* mapped to its value.
*
* <pre>
* //record mapping a category to its value.
* Category := RECORD
* STRING categoryName;
* INTEGER value;
* END;
*
* //record mapping feature names to their categories.
* MappingLayout := RECORD
* STRING featureName;
* DATASET(Category) categories;
* END;
* </pre>
*/
EXPORT GetMapping(key) := FUNCTIONMACRO
IMPORT ML_Core;
RETURN ML_Core.LabelEncoder.MapCategoriesToValues(key);
ENDMACRO;
/**
* Replaces each categorical value in the data with its index in the key.
* Every unknown category (not in the key) is replaced by -1.
*
* @param dataToEncode: any dataset.
* <p> The data to encode.
*
* @param key: DATASET(KeyLayout).
* <p> Mapping between feature names and their categories.
*
* @return encodedData: same record structure as dataToEncode
* with the datatype of all categorical features changed to INTEGER.
* <p> Data with categorical values replaced by numbers.
*/
EXPORT Encode(dataToEncode, key) := FUNCTIONMACRO
IMPORT ML_Core;
utils := ML_Core.Preprocessing.Utils;
//build mapping between categories and values
#UNIQUENAME(mapping)
%mapping% := Utils.LabelEncoder.MapCategoriesToValues(key);
//build final record structure
featureNameSET := Utils.GetFeatureNames(key);
#EXPORTXML(dataMetaInfo, RECORDOF(dataToEncode))
EncodedDataLayout := RECORD
#FOR(dataMetaInfo)
#FOR(field)
#IF(%'@label'% IN featureNameSET)
#EXPAND('INTEGER ' + %'@label'%);
#ELSE
#EXPAND(%'@type'% + ' ' + %'@label'%);
#END
#END
#END
END;
//replace categories by corresponding value
#EXPORTXML(keyMetaInfo, RECORDOF(key))
#UNIQUENAME(categories)
#UNIQUENAME(category)
EncodedDataLayout replace (RECORDOF(dataToEncode) L):= TRANSFORM
#FOR(keyMetaInfo)
#FOR(field)
#SET(categories, %'mapping'% + '(featureName = \'' + %'@label'% + '\')[1].categories')
#SET(category, %'categories'% + '(categoryName = (STRING)L.' + %'@label'% + ')')
SELF.%@label% := IF(EXISTS(%category%), %category%[1].value, -1);
#END
#END
SELF := L;
END;
result := PROJECT(dataToEncode, replace(LEFT));
RETURN result;
ENDMACRO;
/**
* Converts back the categorical values into their original labels.
* Every -1 is replaced by an empty string.
*
* @param dataToDecode: any dataset.
* <p> The data to decode.
*
* @param key: DATASET(KeyLayout).
* <p> Mapping between feature names and their categories.
*
* @return decodedData: same record structure as dataToDecode
* with the datatype of all categorical features changed to STRING.
* <p> Data with categorical values replaced by their original labels.
*/
EXPORT Decode(dataToDecode, encoderKey) := FUNCTIONMACRO
IMPORT ML_Core;
utils := ML_Core.Preprocessing.Utils;
//build mapping between categories and values
#UNIQUENAME(mapping)
%mapping% := Utils.LabelEncoder.MapCategoriesToValues(key);
//build final record structure
featureNameSET := Utils.GetFeatureNames(key);
#EXPORTXML(dataMetaInfo, RECORDOF(dataToDecode))
DecodedDataLayout := RECORD
#FOR(dataMetaInfo)
#FOR(field)
#IF(%'@label'% IN featureNameSET)
#EXPAND('STRING ' + %'@label'%);
#ELSE
#EXPAND(%'@type'% + ' ' + %'@label'%);
#END
#END
#END
END;
//replace values by original labels
#EXPORTXML(keyMetaInfo, RECORDOF(key))
#UNIQUENAME(categories)
#UNIQUENAME(category)
DecodedDataLayout replace (RECORDOF(dataToDecode) L):= TRANSFORM
#FOR(keyMetaInfo)
#FOR(field)
#SET(categories, %'mapping'% + '(featureName = \'' + %'@label'% + '\')[1].categories')
#SET(category, %'categories'% + '(value = L.' + %'@label'% + ')')
SELF.%@label% := %category%[1].categoryName;
#END
#END
SELF := L;
END;
result := PROJECT(dataToDecode, replace(LEFT));
RETURN result;
ENDMACRO;
END;