StandardScalerPreprocessor implements Preprocessor<K, V>, DeployableObject { @Override public LabeledVector apply(K k, V v) { LabeledVector res = basePreprocessor.apply(k, v); assert res.size() == means.length; for (int i = 0; i < res.size(); i++) res.set(i, (res.get(i) - means[i]) / sigmas[i]); return res; } StandardScalerPreprocessor(double[] means, double[] sigmas,
                                      Preprocessor<K, V> basePreprocessor); @Override LabeledVector apply(K k, V v); double[] getMeans(); double[] getSigmas(); @Override List<Object> getDependencies();  }
@Test public void testApply() { double[][] inputData = new double[][] { {0, 2., 4., .1}, {0, 1., -18., 2.2}, {1, 4., 10., -.1}, {1, 0., 22., 1.3} }; double[] means = new double[] {0.5, 1.75, 4.5, 0.875}; double[] sigmas = new double[] {0.5, 1.47901995, 14.51723114, 0.93374247}; final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 1, 2, 3).labeled(0); StandardScalerPreprocessor<Integer, Vector> preprocessor = new StandardScalerPreprocessor<>( means, sigmas, vectorizer ); double[][] expectedData = new double[][] { {-1., 0.16903085, -0.03444183, -0.82999331}, {-1., -0.50709255, -1.54988233, 1.41902081}, {1., 1.52127766, 0.37886012, -1.04418513}, {1., -1.18321596, 1.20546403, 0.45515762} }; for (int i = 0; i < inputData.length; i++) assertArrayEquals(expectedData[i], preprocessor.apply(i, VectorUtils.of(inputData[i])).features().asArray(), 1e-8); }