UNPKG

3.87 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
11/**
12 * Encoder of categorical values to integers. For k different values, encoding yields integer
13 * numbers from 0 to k-1 (inclusive).
14 *
15 * @example
16 * // List of raw input values
17 * var y = ['Car', 'Bike', 'Bike', 'Car', 'Duck', 'Bike'];
18 *
19 * // Create encoder
20 * var encoder = new LabelEncoder();
21 *
22 * // Encode input values, encoding the values to integers
23 * var yEncoded = encoder.encode(y);
24 * console.log(yEncoded); // [0, 1, 1, 0, 2, 0]
25 *
26 * // Decode the encoded values, and see that they match the original input values
27 * console.log(encoder.decode(yEncoded)); // ['Car', 'Bike', 'Bike', 'Car', 'Duck', 'Bike']
28 */
29var LabelEncoder = function () {
30 /**
31 * Initialize object properties.
32 */
33 function LabelEncoder() {
34 _classCallCheck(this, LabelEncoder);
35
36 /**
37 * Dictionary mapping class labels to class indices.
38 *
39 * @type {Object.<string, number>}
40 */
41 this.labelsClassIndex = {};
42
43 /**
44 * Array of class labels for class indices
45 *
46 * @type {Array.<string>}
47 */
48 this.classIndicesLabel = [];
49
50 /**
51 * Number of unique class labels
52 *
53 * @type {number}
54 */
55 this.numLabels = 0;
56 }
57
58 /**
59 * Encode a set of labels or a single label.
60 *
61 * @param {mixed|Array.<mixed>} labels - Single label or list of labels. Each label should support
62 * the toString() method
63 * @return {number|Array.<number>} Single encoded label or list of encoded labels (i.e., integers
64 * associated with the input labels)
65 */
66
67
68 _createClass(LabelEncoder, [{
69 key: 'encode',
70 value: function encode(labels) {
71 var _this = this;
72
73 // In case multiple labels are passed, encode them one-by-one
74 if (Array.isArray(labels)) {
75 return labels.map(function (label) {
76 return _this.encode(label);
77 });
78 }
79
80 // In case a single label is passed, encode it
81 var labelString = labels.toString();
82
83 if (typeof this.labelsClassIndex[labelString] === 'undefined') {
84 this.labelsClassIndex[labelString] = this.numLabels;
85 this.classIndicesLabel.push(labelString);
86 this.numLabels += 1;
87 }
88
89 return this.labelsClassIndex[labelString];
90 }
91
92 /**
93 * Decode a set of labels or a single label.
94 *
95 * @param {number|Array.<number>} encodedLabels - Single encoded label or list of encoded labels
96 * @return {mixed|Array.<mixed>} Single decoded label or list of decoded labels
97 */
98
99 }, {
100 key: 'decode',
101 value: function decode(encodedLabels) {
102 var _this2 = this;
103
104 // In case multiple labels are passed, decode them one-by-one
105 if (Array.isArray(encodedLabels)) {
106 return encodedLabels.map(function (label) {
107 return _this2.decode(label);
108 });
109 }
110
111 // In case a single label is passed, decode it
112 return typeof this.classIndicesLabel[encodedLabels] === 'undefined' ? -1 : this.classIndicesLabel[encodedLabels];
113 }
114 }]);
115
116 return LabelEncoder;
117}();
118
119exports.default = LabelEncoder;
120module.exports = exports.default;
\No newline at end of file