MaxAbsScalerPreprocessor implements Preprocessor<K, V>, DeployableObject { @Override public LabeledVector apply(K k, V v) { LabeledVector res = basePreprocessor.apply(k, v); assert res.size() == maxAbs.length; for (int i = 0; i < res.size(); i++) res.set(i, res.get(i) / maxAbs[i]); return res; } MaxAbsScalerPreprocessor(double[] maxAbs, Preprocessor<K, V> basePreprocessor); @Override LabeledVector apply(K k, V v); double[] getMaxAbs(); @Override List<Object> getDependencies();  }
@Test public void testApply() { double[][] data = new double[][] { {2., 4., 1.}, {1., 8., 22.}, {-4., 10., 100.}, {0., 22., 300.} }; double[] maxAbs = new double[] {4, 22, 300}; Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<>(0, 1, 2); MaxAbsScalerPreprocessor<Integer, double[]> preprocessor = new MaxAbsScalerPreprocessor<>( maxAbs, vectorizer ); double[][] expData = new double[][] { {.5, 4. / 22, 1. / 300}, {.25, 8. / 22, 22. / 300}, {-1., 10. / 22, 100. / 300}, {0., 22. / 22, 300. / 300} }; for (int i = 0; i < data.length; i++) assertArrayEquals(expData[i], preprocessor.apply(i, data[i]).features().asArray(), 1e-8); }