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

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

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

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

public class OneHotEncoder implements CategoricalEncoder {

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

  OneHotEncoder(String columnName, int targetIndex, String[] domainValues) {
    this.columnName = columnName;
    this.targetIndex = targetIndex;
    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;
    makeHot(levelIndex, rawData);
    return true;
  }

  @Override
  public void encodeNA(double[] rawData) {
    makeHot(domainMap.size(), rawData);
  }

  private void makeHot(int index, double[] rawData) {
    for (int i = 0; i < domainMap.size() + 1; i++) {
      rawData[targetIndex + i] = index == i ? 1 : 0;
    }
  }

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy