Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do non-random Dataset splitting on Apache Spark?

I know I can do random splitting with randomSplit method:

val splittedData: Array[Dataset[Row]] = 
        preparedData.randomSplit(Array(0.5, 0.3, 0.2))

Can I split the data into consecutive parts with some 'nonRandomSplit method'?

Apache Spark 2.0.1. Thanks in advance.

UPD: data order is important, I'm going to train my model on data with 'smaller IDs' and test it on data with 'larger IDs'. So I want to split data into consecutive parts without shuffling.

e.g.

my dataset = (0,1,2,3,4,5,6,7,8,9)
desired splitting = (0.8, 0.2)
splitting = (0,1,2,3,4,5,6,7), (8,9)

The only solution I can think of is to use count and limit, but there probably is a better one.

like image 995
Anton Avatar asked Dec 02 '16 14:12

Anton


1 Answers

This is the solution I've implemented: Dataset -> Rdd -> Dataset.

I'm not sure whether it is the most effective way to do it, so I'll be glad to accept a better solution.

val count = allData.count()
val trainRatio = 0.6  
val trainSize = math.round(count * trainRatio).toInt
val dataSchema = allData.schema

// Zipping with indices and skipping rows with indices > trainSize.
// Could have possibly used .limit(n) here
val trainingRdd =
  allData
    .rdd
    .zipWithIndex()
    .filter { case (_, index) => index < trainSize }
    .map { case (row, _) => row }

// Can't use .limit() :(
val testRdd =
allData
  .rdd
  .zipWithIndex()
  .filter { case (_, index) => index >= trainSize }
  .map { case (row, _) => row }

val training = MySession.createDataFrame(trainingRdd, dataSchema)
val test = MySession.createDataFrame(testRdd, dataSchema)
like image 150
Anton Avatar answered Sep 26 '22 07:09

Anton