Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/18#discussion_r11457753
--- Diff:
mllib/src/test/scala/org/apache/spark/mllib/util/MLUtilsSuite.scala ---
@@ -106,4 +109,40 @@ class MLUtilsSuite extends FunSuite with
LocalSparkContext {
case t: Throwable =>
}
}
+
+ test("kFold") {
+ val data = sc.parallelize(1 to 100, 2)
+ val collectedData = data.collect().sorted
+ val twoFoldedRdd = MLUtils.kFold(data, 2, 1)
+ assert(twoFoldedRdd(0)._1.collect().sorted ===
twoFoldedRdd(1)._2.collect().sorted)
+ assert(twoFoldedRdd(0)._2.collect().sorted ===
twoFoldedRdd(1)._1.collect().sorted)
+ for (folds <- 2 to 10) {
+ for (seed <- 1 to 5) {
+ val foldedRdds = MLUtils.kFold(data, folds, seed)
+ assert(foldedRdds.size === folds)
+ foldedRdds.map{case (test, train) =>
+ val result = test.union(train).collect().sorted
+ val testSize = test.collect().size.toFloat
+ assert(testSize > 0, "Non empty test data")
+ val p = 1 / folds.toFloat
+ // Within 3 standard deviations of the mean
+ val range = 3 * math.sqrt(100 * p * (1-p))
+ val expected = 100 * p
+ val lowerBound = expected - range
+ val upperBound = expected + range
+ assert(testSize > lowerBound,
+ "Test data (" + testSize + ") smaller than expected (" +
lowerBound +")" )
+ assert(testSize < upperBound,
+ "Test data (" + testSize + ") larger than expected (" +
upperBound +")" )
+ assert(train.collect().size > 0, "Non empty training data")
+ assert(result === collectedData,
+ "Each training+test set combined contains all of the data")
--- End diff --
ditto. "Each training+validation set combined should contain all of the
data." reads better.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---