@Test public void test3() throws Exception { TableSchema schema = new TableSchema(new String[] {"double", "bool", "number", "str"}, new TypeInformation<?>[] {Types.DOUBLE(), Types.BOOLEAN(), Types.STRING(), Types.STRING()}); Params params = new Params() .set(FeatureHasherParams.SELECTED_COLS, new String[] {"double", "bool", "number", "str"}) .set(FeatureHasherParams.OUTPUT_COL, "output") .set(FeatureHasherParams.NUM_FEATURES, 10) .set(FeatureHasherParams.CATEGORICAL_COLS, new String[] {"double"}); FeatureHasherMapper mapper = new FeatureHasherMapper(schema, params); assertEquals(mapper.map(Row.of(1.1, true, "2", "A")).getField(4), new SparseVector(10, new int[]{0, 5, 9}, new double[]{1.0, 2.0, 1.0})); assertEquals(mapper.map(Row.of(2.1, true, "1", "B")).getField(4), new SparseVector(10, new int[]{1, 5, 6}, new double[]{2.0, 1.0, 1.0})); }
@Override public Row map(Row row) { TreeMap<Integer, Double> feature = new TreeMap<>(); for (int key : numericColIndexes) { if (null != row.getField(key)) { double value = ((Number)row.getField(key)).doubleValue(); String colName = colNames[key]; updateMap(colName, value, feature, numFeature); } } for (int key : categoricalColIndexes) { if (null != row.getField(key)) { String colName = colNames[key]; updateMap(colName + "=" + row.getField(key).toString(), 1.0, feature, numFeature); } } return outputColsHelper.getResultRow(row, Row.of(new SparseVector(numFeature, feature))); }
FeatureHasherMapper extends Mapper { @Override public Row map(Row row) { TreeMap<Integer, Double> feature = new TreeMap<>(); for (int key : numericColIndexes) { if (null != row.getField(key)) { double value = ((Number)row.getField(key)).doubleValue(); String colName = colNames[key]; updateMap(colName, value, feature, numFeature); } } for (int key : categoricalColIndexes) { if (null != row.getField(key)) { String colName = colNames[key]; updateMap(colName + "=" + row.getField(key).toString(), 1.0, feature, numFeature); } } return outputColsHelper.getResultRow(row, Row.of(new SparseVector(numFeature, feature))); } }
FeatureHasherMapper extends Mapper { @Override public Row map(Row row) { TreeMap<Integer, Double> feature = new TreeMap<>(); for (int key : numericColIndexes) { if (null != row.getField(key)) { double value = ((Number)row.getField(key)).doubleValue(); String colName = colNames[key]; updateMap(colName, value, feature, numFeature); } } for (int key : categoricalColIndexes) { if (null != row.getField(key)) { String colName = colNames[key]; updateMap(colName + "=" + row.getField(key).toString(), 1.0, feature, numFeature); } } return outputColsHelper.getResultRow(row, Row.of(new SparseVector(numFeature, feature))); } FeatureHasherMapper(TableSchema dataSchema, Params params); }
FeatureHasherMapper extends Mapper { @Override public Row map(Row row) { TreeMap<Integer, Double> feature = new TreeMap<>(); for (int key : numericColIndexes) { if (null != row.getField(key)) { double value = ((Number)row.getField(key)).doubleValue(); String colName = colNames[key]; updateMap(colName, value, feature, numFeature); } } for (int key : categoricalColIndexes) { if (null != row.getField(key)) { String colName = colNames[key]; updateMap(colName + "=" + row.getField(key).toString(), 1.0, feature, numFeature); } } return outputColsHelper.getResultRow(row, Row.of(new SparseVector(numFeature, feature))); } FeatureHasherMapper(TableSchema dataSchema, Params params); @Override TableSchema getOutputSchema(); @Override Row map(Row row); }
FeatureHasherMapper extends Mapper { @Override public Row map(Row row) { TreeMap<Integer, Double> feature = new TreeMap<>(); for (int key : numericColIndexes) { if (null != row.getField(key)) { double value = ((Number)row.getField(key)).doubleValue(); String colName = colNames[key]; updateMap(colName, value, feature, numFeature); } } for (int key : categoricalColIndexes) { if (null != row.getField(key)) { String colName = colNames[key]; updateMap(colName + "=" + row.getField(key).toString(), 1.0, feature, numFeature); } } return outputColsHelper.getResultRow(row, Row.of(new SparseVector(numFeature, feature))); } FeatureHasherMapper(TableSchema dataSchema, Params params); @Override TableSchema getOutputSchema(); @Override Row map(Row row);  }