DataUtil { public static double[] unitScale(double[] data) { val summaryStats = Arrays.stream(data).summaryStatistics(); val min = summaryStats.getMin(); val max = summaryStats.getMax(); val range = max - min; val denom = (range == 0.0 ? 1.0 : range); return Arrays.stream(data) .map(value -> (value - min) / denom) .toArray(); }  static double[] unitScale(double[] data);  }
@Test public void testUnitScale_constant() { val data = new double[] { 3.0, 3.0 }; val scaled = DataUtil.unitScale(data); assertArrayEquals(new double[] { 0.0, 0.0 }, scaled, TOLERANCE); }
@Test public void testUnitScale_binary() { val data = new double[] { 4.0, 6.5 }; val scaled = DataUtil.unitScale(data); assertArrayEquals(new double[] { 0.0, 1.0 }, scaled, TOLERANCE); }
@Test public void testUnitScale() { val data0 = new double[] { -3.0, 6.0, 0.0 }; val scaled0 = DataUtil.unitScale(data0); assertArrayEquals(new double[] { 0.0, 1.0, 0.333 }, scaled0, TOLERANCE); val data1 = new double[] { 25.0, 50.0, 75.0, 0.0, 100.0 }; val scaled1 = DataUtil.unitScale(data1); assertArrayEquals(new double[] { 0.25, 0.50, 0.75, 0.0, 1.0 }, scaled1, TOLERANCE); }