All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hex.genmodel.easy.BinaryEncoder Maven / Gradle / Ivy

There is a newer version: 3.46.0.6
Show newest version
package hex.genmodel.easy;

import hex.genmodel.utils.MathUtils;

import java.util.HashMap;
import java.util.Map;

public class BinaryEncoder implements CategoricalEncoder {

  private final String columnName;
  private final int targetIndex;
  private final Map domainMap;
  private final int binaryCategorySizes;

  BinaryEncoder(String columnName, int targetIndex, String[] domainValues) {
    this.columnName = columnName;
    this.targetIndex = targetIndex;
    this.binaryCategorySizes = 1 + MathUtils.log2(domainValues.length - 1 + 1/* for NAs */);
    domainMap = new HashMap<>(domainValues.length);
    for (int j = 0; j < domainValues.length; j++) {
      domainMap.put(domainValues[j], j);
    }
  }

  @Override
  public boolean encodeCatValue(String levelName, double[] rawData) {
    Integer levelIndex = domainMap.get(levelName);
    if (levelIndex == null)
      return false;
    makeBinary(levelIndex, rawData);
    return true;
  }

  @Override
  public void encodeNA(double[] rawData) {
    makeBinary(-1, rawData);
  }

  private void makeBinary(int index, double[] rawData) {
    long val = index + 1; //0 is used for NA
    for (int i = 0; i < binaryCategorySizes; i++) {
      rawData[targetIndex + i] = val & 1;
      val >>>= 1;
    }
  }

  @Override
  public String toString() {
    return "BinaryEncoder{" +
            "columnName='" + columnName + '\'' +
            ", targetIndex=" + targetIndex +
            ", domainMap=" + domainMap +
            '}';
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy