Github user tillrohrmann commented on a diff in the pull request: https://github.com/apache/flink/pull/579#discussion_r28412749 --- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/preprocessing/StandardScaler.scala --- @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.ml.preprocessing + +import java.lang.Iterable +import breeze.linalg +import breeze.linalg.DenseVector +import breeze.numerics.sqrt +import breeze.numerics.sqrt._ +import org.apache.flink.api.common.functions._ +import org.apache.flink.api.scala._ +import org.apache.flink.configuration.Configuration +import org.apache.flink.ml.common.{Parameter, ParameterMap, Transformer} +import org.apache.flink.ml.math.Breeze._ +import org.apache.flink.ml.math.Vector +import org.apache.flink.ml.preprocessing.StandardScaler.{Mean, Std} +import org.apache.flink.util.Collector + +/** Scales observations, so that all features have mean equal to zero + * and standard deviation equal to one + * + * This transformer takes a a Vector of values and maps it into the + * scaled Vector that each feature has a user-specified mean and standard deviation. + * + * This transformer can be prepended to all [[Transformer]] and + * [[org.apache.flink.ml.common.Learner]] implementations which expect an input of + * [[Vector]]. + * + * @example + * {{{ + * val trainingDS: DataSet[Vector] = env.fromCollection(data) + * + * val transformer = StandardScaler().setMean(10.0).setStd(2.0) + * + * transformer.transform(trainingDS) + * }}} + * + * =Parameters= + * + * - [[StandardScaler.Mean]]: The mean value of transformed data set; by default equal to 0 + * - [[StandardScaler.Std]]: The standard deviation of the transformed data set; by default + * equal to 1 + */ +class StandardScaler extends Transformer[Vector, Vector] with Serializable { + + def setMean(mu: Double): StandardScaler = { + parameters.add(Mean, mu) + this + } + + def setStd(std: Double): StandardScaler = { + parameters.add(Std, std) + this + } + + override def transform(input: DataSet[Vector], parameters: ParameterMap): + DataSet[Vector] = { + val resultingParameters = this.parameters ++ parameters + val mean = resultingParameters(Mean) + val std = resultingParameters(Std) + + val featureMetrics = extractFeatureMetrics(input) + + input.map(new RichMapFunction[Vector, Vector]() { + + var broadcastMeanSet: Vector = null + var broadcastStdSet: Vector = null + + override def open(parameters: Configuration): Unit = { + val broadcastedMetrics = getRuntimeContext().getBroadcastVariable[(Vector, + Vector)]("broadcastedMetrics").get(0) + broadcastMeanSet = broadcastedMetrics._1 + broadcastStdSet = broadcastedMetrics._2 + } + + override def map(vector: Vector): Vector = { + var myVector = vector.asBreeze + + myVector :-= broadcastMeanSet.asBreeze + myVector :/= broadcastStdSet.asBreeze + myVector = (myVector :* std) :+ mean + return myVector.fromBreeze + } + }).withBroadcastSet(featureMetrics, "broadcastedMetrics") + } + + /** Calculates in one pass over the data the features' mean and standard deviation. + * For the calculation of the Standard deviation with one pas over the data, + * the Youngs & Cramer algorithm was used + * + * @param dataSet The data set for which we want to calculate mean and variance + * @return DataSet of Tuple2<featuresMeanVector,featuresStdVector> + */ + private def extractFeatureMetrics(dataSet: DataSet[Vector]) = { --- End diff -- By adding explicitly the return type of Scala methods, here ```DataSet[(Vector, Vector)]```, you can check that the method is actually returning this type.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---