Github user tillrohrmann commented on a diff in the pull request: https://github.com/apache/flink/pull/798#discussion_r31894783 --- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/preprocessing/MinMaxScaler.scala --- @@ -0,0 +1,255 @@ +/* + * 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 breeze.linalg +import org.apache.flink.api.common.typeinfo.TypeInformation +import org.apache.flink.api.scala._ +import org.apache.flink.ml._ +import org.apache.flink.ml.common.{LabeledVector, Parameter, ParameterMap} +import org.apache.flink.ml.math.Breeze._ +import org.apache.flink.ml.math.{BreezeVectorConverter, Vector} +import org.apache.flink.ml.pipeline.{FitOperation, TransformOperation, Transformer} +import org.apache.flink.ml.preprocessing.MinMaxScaler.{Max, Min} + +import scala.reflect.ClassTag + +/** Scales observations, so that all features are in a user-specified range. + * By default for [[MinMaxScaler]] transformer range = (0,1). + * + * This transformer takes a subtype of [[Vector]] of values and maps it to a + * scaled subtype of [[Vector]] such that each feature lies between a user-specified range. + * + * This transformer can be prepended to all [[Transformer]] and + * [[org.apache.flink.ml.pipeline.Predictor]] implementations which expect as input a subtype + * of [[Vector]]. + * + * @example + * {{{ + * val trainingDS: DataSet[Vector] = env.fromCollection(data) + * val transformer = MinMaxScaler().setMin(-1.0).setMax(1.0) + * + * transformer.fit(trainingDS) + * val transformedDS = transformer.transform(trainingDS) + * }}} + * + * =Parameters= + * + * - [[Min]]: The minimum value of the range of the transformed data set; by default equal to 0 + * - [[Max]]: The maximum value of the range of the transformed data set; by default + * equal to 1 + */ +class MinMaxScaler extends Transformer[MinMaxScaler] { + + var metricsOption: Option[DataSet[(linalg.Vector[Double], linalg.Vector[Double])]] = None + + /** Sets the minimum for the range of the transformed data + * + * @param min the user-specified minimum value. + * @return the MinMaxScaler instance with its minimum value set to the user-specified value. + */ + def setMin(min: Double): MinMaxScaler = { + parameters.add(Min, min) + this + } + + /** Sets the maximum for the range of the transformed data + * + * @param max the user-specified maximum value. + * @return the MinMaxScaler instance with its minimum value set to the user-specified value. + */ + def setMax(max: Double): MinMaxScaler = { + parameters.add(Max, max) + this + } +} + +object MinMaxScaler { + + // ====================================== Parameters ============================================= + + case object Min extends Parameter[Double] { + override val defaultValue: Option[Double] = Some(0.0) + } + + case object Max extends Parameter[Double] { + override val defaultValue: Option[Double] = Some(1.0) + } + + // ==================================== Factory methods ========================================== + + def apply(): MinMaxScaler = { + new MinMaxScaler() + } + + // ====================================== Operations ============================================= + + /** Trains the [[org.apache.flink.ml.preprocessing.MinMaxScaler]] by learning the minimum and + * maximum of each feature of the training data. These values are used in the transform step + * to transform the given input data. + * + * @tparam T Input data type which is a subtype of [[Vector]] + * @return + */ + implicit def fitVectorMinMaxScaler[T <: Vector] = new FitOperation[MinMaxScaler, T] { + override def fit(instance: MinMaxScaler, fitParameters: ParameterMap, input: DataSet[T]) + : Unit = { + val metrics = extractFeatureMinMaxVectors(input) + + instance.metricsOption = Some(metrics) + } + } + + /** Trains the [[MinMaxScaler]] by learning the minimum and maximum of the features of the + * training data which is of type [[LabeledVector]]. The minimum and maximum are used to + * transform the given input data. + * + */ + implicit val fitLabeledVectorMinMaxScaler = { + new FitOperation[MinMaxScaler, LabeledVector] { + override def fit( + instance: MinMaxScaler, + fitParameters: ParameterMap, + input: DataSet[LabeledVector]) + : Unit = { + val vectorDS = input.map(_.vector) + val metrics = extractFeatureMinMaxVectors(vectorDS) + + instance.metricsOption = Some(metrics) + } + } + } + + /** Calculates in one pass over the data the features' minimum and maximum values. + * + * --- End diff -- two line breaks.
--- 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. ---