UNPKG

@jsmlt/jsmlt

Version:

JavaScript Machine Learning

123 lines (102 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** * Encoder of categorical values to integers. For k different values, encoding yields integer * numbers from 0 to k-1 (inclusive). * * @example * // List of raw input values * var y = ['Car', 'Bike', 'Bike', 'Car', 'Duck', 'Bike']; * * // Create encoder * var encoder = new LabelEncoder(); * * // Encode input values, encoding the values to integers * var yEncoded = encoder.encode(y); * console.log(yEncoded); // [0, 1, 1, 0, 2, 0] * * // Decode the encoded values, and see that they match the original input values * console.log(encoder.decode(yEncoded)); // ['Car', 'Bike', 'Bike', 'Car', 'Duck', 'Bike'] */ var LabelEncoder = /*#__PURE__*/ function () { /** * Initialize object properties. */ function LabelEncoder() { _classCallCheck(this, LabelEncoder); /** * Dictionary mapping class labels to class indices. * * @type {Object.<string, number>} */ this.labelsClassIndex = {}; /** * Array of class labels for class indices * * @type {Array.<string>} */ this.classIndicesLabel = []; /** * Number of unique class labels * * @type {number} */ this.numLabels = 0; } /** * Encode a set of labels or a single label. * * @param {mixed|Array.<mixed>} labels - Single label or list of labels. Each label should support * the toString() method * @return {number|Array.<number>} Single encoded label or list of encoded labels (i.e., integers * associated with the input labels) */ _createClass(LabelEncoder, [{ key: "encode", value: function encode(labels) { var _this = this; // In case multiple labels are passed, encode them one-by-one if (Array.isArray(labels)) { return labels.map(function (label) { return _this.encode(label); }); } // In case a single label is passed, encode it var labelString = labels.toString(); if (typeof this.labelsClassIndex[labelString] === 'undefined') { this.labelsClassIndex[labelString] = this.numLabels; this.classIndicesLabel.push(labelString); this.numLabels += 1; } return this.labelsClassIndex[labelString]; } /** * Decode a set of labels or a single label. * * @param {number|Array.<number>} encodedLabels - Single encoded label or list of encoded labels * @return {mixed|Array.<mixed>} Single decoded label or list of decoded labels */ }, { key: "decode", value: function decode(encodedLabels) { var _this2 = this; // In case multiple labels are passed, decode them one-by-one if (Array.isArray(encodedLabels)) { return encodedLabels.map(function (label) { return _this2.decode(label); }); } // In case a single label is passed, decode it return typeof this.classIndicesLabel[encodedLabels] === 'undefined' ? -1 : this.classIndicesLabel[encodedLabels]; } }]); return LabelEncoder; }(); exports["default"] = LabelEncoder; module.exports = exports.default;