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

    https://github.com/apache/spark/pull/2607#discussion_r19637468
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/tree/EnsembleTestHelper.scala ---
    @@ -0,0 +1,94 @@
    +/*
    + * 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.spark.mllib.tree
    +
    +import org.apache.spark.mllib.linalg.Vectors
    +import org.apache.spark.mllib.regression.LabeledPoint
    +import org.apache.spark.mllib.tree.model.WeightedEnsembleModel
    +import org.apache.spark.util.StatCounter
    +
    +import scala.collection.mutable
    +
    +object EnsembleTestHelper {
    +
    +  /**
    +   * Aggregates all values in data, and tests whether the empirical mean 
and stddev are within
    +   * epsilon of the expected values.
    +   * @param data  Every element of the data should be an i.i.d. sample 
from some distribution.
    +   */
    +  def testRandomArrays(
    +      data: Array[Array[Double]],
    +      numCols: Int,
    +      expectedMean: Double,
    +      expectedStddev: Double,
    +      epsilon: Double) {
    +    val values = new mutable.ArrayBuffer[Double]()
    +    data.foreach { row =>
    +      assert(row.size == numCols)
    +      values ++= row
    +    }
    +    val stats = new StatCounter(values)
    +    assert(math.abs(stats.mean - expectedMean) < epsilon)
    +    assert(math.abs(stats.stdev - expectedStddev) < epsilon)
    +  }
    +
    +  def validateClassifier(
    +      model: WeightedEnsembleModel,
    +      input: Seq[LabeledPoint],
    +      requiredAccuracy: Double) {
    +    val predictions = input.map(x => model.predict(x.features))
    +    val numOffPredictions = predictions.zip(input).count { case 
(prediction, expected) =>
    +      prediction != expected.label
    +    }
    +    val accuracy = (input.length - numOffPredictions).toDouble / 
input.length
    +    assert(accuracy >= requiredAccuracy,
    +      s"validateClassifier calculated accuracy $accuracy but required 
$requiredAccuracy.")
    +  }
    +
    +  def validateRegressor(
    +      model: WeightedEnsembleModel,
    +      input: Seq[LabeledPoint],
    +      requiredMSE: Double) {
    +    val predictions = input.map(x => model.predict(x.features))
    +    val squaredError = predictions.zip(input).map { case (prediction, 
expected) =>
    +      val err = prediction - expected.label
    +      err * err
    +    }.sum
    +    val mse = squaredError / input.length
    +    assert(mse <= requiredMSE, s"validateRegressor calculated MSE $mse but 
required $requiredMSE.")
    +  }
    +
    +  def generateOrderedLabeledPoints(numFeatures: Int, numInstances: Int): 
Array[LabeledPoint] = {
    +    val arr = new Array[LabeledPoint](numInstances)
    +    for (i <- 0 until numInstances) {
    +      val label = if (i < numInstances / 10) {
    +        0.0
    +      } else if (i < numInstances / 2) {
    +        1.0
    +      } else if (i < numInstances * 0.9) {
    +        0.0
    +      } else {
    +        1.0
    +      }
    +      val features = Array.fill[Double](numFeatures)(i.toDouble)
    +      arr(i) = new LabeledPoint(label, Vectors.dense(features))
    +    }
    +    arr
    +  }
    +
    +}
    --- End diff --
    
    newline


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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to