NormalizationPreprocessor implements Preprocessor<K, V>, DeployableObject { @Override public LabeledVector apply(K k, V v) { LabeledVector res = basePreprocessor.apply(k, v); double pNorm = Math.pow(foldMap(res.features(), Functions.PLUS, Functions.pow(p), 0d), 1.0 / p); for (int i = 0; i < res.size(); i++) res.set(i, res.get(i) / pNorm); return res; } NormalizationPreprocessor(int p, Preprocessor<K, V> basePreprocessor); @Override LabeledVector apply(K k, V v); double p(); @Override List<Object> getDependencies();  }
@Test public void testApply() { double[][] data = new double[][]{ {1, 2, 1}, {1, 1, 1}, {1, 0, 0}, }; Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<>(0, 1, 2); NormalizationPreprocessor<Integer, double[]> preprocessor = new NormalizationPreprocessor<>( 1, vectorizer ); double[][] postProcessedData = new double[][]{ {0.25, 0.5, 0.25}, {0.33, 0.33, 0.33}, {1, 0, 0} }; for (int i = 0; i < data.length; i++) assertArrayEquals(postProcessedData[i], preprocessor.apply(i, data[i]).features().asArray(), 1e-2); }