Github user tillrohrmann commented on a diff in the pull request:

    https://github.com/apache/flink/pull/1898#discussion_r60737947
  
    --- Diff: 
flink-libraries/flink-ml/src/main/scala/org/apache/flink/ml/preprocessing/Splitter.scala
 ---
    @@ -0,0 +1,215 @@
    +/*
    + * 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 org.apache.flink.api.common.typeinfo.{TypeInformation, 
BasicTypeInfo}
    +import org.apache.flink.api.java.Utils
    +import org.apache.flink.api.scala. DataSet
    +import org.apache.flink.api.scala.utils._
    +
    +import org.apache.flink.ml.common.{FlinkMLTools, ParameterMap, 
WithParameters}
    +import _root_.scala.reflect.ClassTag
    +
    +object Splitter {
    +
    +  case class TrainTestDataSet[T: TypeInformation : ClassTag](training: 
DataSet[T],
    +                                                             testing: 
DataSet[T])
    +
    +  case class TrainTestHoldoutDataSet[T: TypeInformation : 
ClassTag](training: DataSet[T],
    +                                                                    
testing: DataSet[T],
    +                                                                    
holdout: DataSet[T])
    +  // 
--------------------------------------------------------------------------------------------
    +  //  randomSplit
    +  // 
--------------------------------------------------------------------------------------------
    +  /**
    +   * Split a DataSet by the probability fraction of each element.
    +   *
    +   * @param input           DataSet to be split
    +   * @param fraction        Probability that each element is chosen, 
should be [0,1] without
    +   *                        replacement, and [0, ∞) with replacement. 
While fraction is larger
    +   *                        than 1, the elements are expected to be 
selected multi times into
    +   *                        sample on average. This fraction refers to the 
first element in the
    +   *                        resulting array.
    +   * @param precise         Sampling by default is random and can result 
in slightly lop-sided
    +   *                        sample sets. When precise is true, equal 
sample set size are forced,
    +   *                        however this is somewhat less efficient.
    +   * @param seed            Random number generator seed.
    +   * @return An array of two datasets
    +   */
    +
    +  def randomSplit[T: TypeInformation : ClassTag]( input: DataSet[T],
    +                                                  fraction: Double,
    +                                                  precise: Boolean = false,
    +                                                  seed: Long = 
Utils.RNG.nextLong())
    +  : Array[DataSet[T]] = {
    +    import org.apache.flink.api.scala._
    +
    +    val indexedInput: DataSet[(Long, T)] = input.zipWithIndex
    +
    +    val leftSplit: DataSet[(Long, T)] = precise match {
    +      case false => indexedInput.sample(false, fraction, seed)
    +      case true => {
    +        val count = indexedInput.count()
    +        val numOfSamples = math.round(fraction * count).toInt
    +        indexedInput.sampleWithSize(false, numOfSamples, seed)
    +      }
    +    }
    +
    +    val rightSplit: DataSet[(Long, T)] = indexedInput.leftOuterJoin[(Long, 
T)](leftSplit)
    +      .where(0)
    +      .equalTo(0) {
    +        (full: (Long,T) , left: (Long, T)) =>  (if (left == null) full 
else null)
    +      }
    +      .filter( o => o != null )
    +    Array(leftSplit.map(o => o._2), rightSplit.map(o => o._2))
    +  }
    +
    +  // 
--------------------------------------------------------------------------------------------
    +  //  multiRandomSplit
    +  // 
--------------------------------------------------------------------------------------------
    +  /**
    +   * Split a DataSet by the probability fraction of each element of a 
vector.
    +   *
    +   * @param input           DataSet to be split
    +   * @param fracArray       An array of PROPORTIONS for splitting the 
DataSet. Unlike the
    +   *                        randomSplit function, number greater than 1 do 
not lead to over
    +   *                        sampling. The number of splits is dictated by 
the length of this array.
    +   *                        The number are normalized, eg. Array(1.0, 2.0) 
would yield
    +   *                        two data sets with a 33/66% split.
    +   * @param precise         Sampling by default is random and can result 
in slightly lop-sided
    +   *                        sample sets. When precise is true, equal 
sample set size are forced,
    +   *                        however this is somewhat less efficient.
    +   * @param seed            Random number generator seed.
    +   * @return An array of DataSets whose length is equal to the length of 
fracArray
    +   */
    +  def multiRandomSplit[T: TypeInformation : ClassTag](input: DataSet[T],
    +                        fracArray: Array[Double],
    +                        precise: Boolean = false,
    +                        seed: Long = Utils.RNG.nextLong())
    +  : Array[DataSet[T]] = {
    +    val splits = fracArray.length
    +    val output = new Array[DataSet[T]](splits)
    +    val aggs = fracArray.scanRight((0.0))( _ + _ )
    +    val fracs = fracArray.zip(aggs).map( o => o._1 / o._2)
    +
    +    ////
    +    var tempDS = input
    +    for (k <- 0 to splits-2){
    +      println( (splits -k))
    +      var temp = Splitter.randomSplit(tempDS, fracs(k), true)
    +      output(k) = temp(0)
    +      tempDS = temp(1)
    +    }
    --- End diff --
    
    I'm not so sure, whether this scales so well. The reason is that we're 
constructing some really long pipelines with this for loop where each iteration 
contains an outer join and is the input for the next iteration. I'm wondering 
whether we cannot simply achieve the assignment of elements to splits in one 
iteration. We could for example generate for each element a random number and 
see in which bin it falls (indicated by the normalized fraction array). Then we 
could apply a number of filters to the result with the assigned splits in order 
to obtain the different splits.


---
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.
---

Reply via email to