StandardScalerTrainer implements PreprocessingTrainer<K, V> { @Override public StandardScalerPreprocessor<K, V> fit(LearningEnvironmentBuilder envBuilder, DatasetBuilder<K, V> datasetBuilder, Preprocessor<K, V> basePreprocessor) { StandardScalerData standardScalerData = computeSum(envBuilder, datasetBuilder, basePreprocessor); int n = standardScalerData.sum.length; long cnt = standardScalerData.cnt; double[] mean = new double[n]; double[] sigma = new double[n]; for (int i = 0; i < n; i++) { mean[i] = standardScalerData.sum[i] / cnt; double variance = (standardScalerData.squaredSum[i] - Math.pow(standardScalerData.sum[i], 2) / cnt) / cnt; sigma[i] = Math.sqrt(variance); } return new StandardScalerPreprocessor<>(mean, sigma, basePreprocessor); }  @Override StandardScalerPreprocessor<K, V> fit(LearningEnvironmentBuilder envBuilder,
        DatasetBuilder<K, V> datasetBuilder,
        Preprocessor<K, V> basePreprocessor);  }
@Test public void testCalculatesCorrectMeans() { double[] expectedMeans = new double[] {0.5, 1.75, 4.5, 0.875}; final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<>(0, 1, 2, 3); StandardScalerPreprocessor<Integer, Vector> preprocessor = (StandardScalerPreprocessor<Integer, Vector>)standardizationTrainer.fit( TestUtils.testEnvBuilder(), datasetBuilder, vectorizer ); assertArrayEquals(expectedMeans, preprocessor.getMeans(), 1e-8); }
@Test public void testCalculatesCorrectStandardDeviations() { double[] expectedSigmas = new double[] {0.5, 1.47901995, 14.51723114, 0.93374247}; final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<>(0, 1, 2, 3); StandardScalerPreprocessor<Integer, Vector> preprocessor = (StandardScalerPreprocessor<Integer, Vector>)standardizationTrainer.fit( TestUtils.testEnvBuilder(), datasetBuilder, vectorizer ); assertArrayEquals(expectedSigmas, preprocessor.getSigmas(), 1e-8); }