Source: eclairjs/ml/feature/OneHotEncoder.js

  1. /*
  2. * Copyright 2016 IBM Corp.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. (function () {
  17. var Transformer = require(EclairJS_Globals.NAMESPACE + '/ml/Transformer');
  18. var Logger = require(EclairJS_Globals.NAMESPACE + '/Logger');
  19. var Utils = require(EclairJS_Globals.NAMESPACE + '/Utils');
  20. /**
  21. * @classdesc
  22. * A one-hot encoder that maps a column of category indices to a column of binary vectors, with
  23. * at most a single one-value per row that indicates the input category index.
  24. * For example with 5 categories, an input value of 2.0 would map to an output vector of
  25. * `[0.0, 0.0, 1.0, 0.0]`.
  26. * The last category is not included by default (configurable via OneHotEncoder!.dropLast
  27. * because it makes the vector entries sum up to one, and hence linearly dependent.
  28. * So an input value of 4.0 maps to `[0.0, 0.0, 0.0, 0.0]`.
  29. * Note that this is different from scikit-learn's OneHotEncoder, which keeps all categories.
  30. * The output vectors are sparse.
  31. *
  32. * @see {@link module:eclairjs/ml/feature.StringIndexer} for converting categorical values into category indices
  33. * @class
  34. * @extends module:eclairjs/ml.Transformer
  35. * @memberof module:eclairjs/ml/feature
  36. * @param {string} [uid]
  37. */
  38. var OneHotEncoder = function (uid) {
  39. this.logger = Logger.getLogger("ml_feature_OneHotEncoder_js");
  40. var jvmObject;
  41. if (uid) {
  42. if (uid instanceof org.apache.spark.ml.feature.OneHotEncoder) {
  43. jvmObject = uid;
  44. } else {
  45. jvmObject = new org.apache.spark.ml.feature.OneHotEncoder(uid);
  46. }
  47. } else {
  48. jvmObject = new org.apache.spark.ml.feature.OneHotEncoder();
  49. }
  50. Transformer.call(this, jvmObject);
  51. };
  52. OneHotEncoder.prototype = Object.create(Transformer.prototype);
  53. OneHotEncoder.prototype.constructor = OneHotEncoder;
  54. /**
  55. * An immutable unique ID for the object and its derivatives.
  56. * @returns {string}
  57. */
  58. OneHotEncoder.prototype.uid = function () {
  59. return this.getJavaObject().uid();
  60. };
  61. /**
  62. * @returns {module:eclairjs/ml/param.BooleanParam}
  63. */
  64. OneHotEncoder.prototype.dropLast = function () {
  65. var javaObject = this.getJavaObject().dropLast();
  66. return Utils.javaToJs(javaObject);
  67. };
  68. /**
  69. * @param {boolean} value
  70. * @returns {module:eclairjs/ml/feature.OneHotEncoder}
  71. */
  72. OneHotEncoder.prototype.setDropLast = function (value) {
  73. var javaObject = this.getJavaObject().setDropLast(value);
  74. return new OneHotEncoder(javaObject);
  75. };
  76. /**
  77. * @returns {boolean}
  78. */
  79. OneHotEncoder.prototype.getDropLast = function() {
  80. return this.getJavaObject().getDropLast();
  81. };
  82. /**
  83. * @param {string} value
  84. * @returns {module:eclairjs/ml/feature.OneHotEncoder}
  85. */
  86. OneHotEncoder.prototype.setInputCol = function (value) {
  87. var javaObject = this.getJavaObject().setInputCol(value);
  88. return new OneHotEncoder(javaObject);
  89. };
  90. /**
  91. * @param {string} value
  92. * @returns {module:eclairjs/ml/feature.OneHotEncoder}
  93. */
  94. OneHotEncoder.prototype.setOutputCol = function (value) {
  95. var javaObject = this.getJavaObject().setOutputCol(value);
  96. return new OneHotEncoder(javaObject);
  97. };
  98. /**
  99. * @param {module:eclairjs/ml/param.ParamMap} extra
  100. * @returns {module:eclairjs/ml/feature.OneHotEncoder}
  101. */
  102. OneHotEncoder.prototype.copy = function (extra) {
  103. var extra_uw = Utils.unwrapObject(extra);
  104. var javaObject = this.getJavaObject().copy(extra_uw);
  105. return new OneHotEncoder(javaObject);
  106. };
  107. //
  108. // static methods
  109. //
  110. /**
  111. * @param {string} path
  112. * @returns {module:eclairjs/ml/feature.OneHotEncoder}
  113. */
  114. OneHotEncoder.load = function (path) {
  115. var javaObject = org.apache.spark.ml.feature.OneHotEncoder.load(path);
  116. return new OneHotEncoder(javaObject);
  117. };
  118. module.exports = OneHotEncoder;
  119. })();