spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rotationsymmetry <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-8518] [ML] Log-linear models for surviv...
Date Wed, 16 Sep 2015 19:27:17 GMT
Github user rotationsymmetry commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8611#discussion_r39674163
  
    --- Diff: mllib/src/test/scala/org/apache/spark/ml/regression/AFTSurvivalRegressionSuite.scala
---
    @@ -0,0 +1,283 @@
    +/*
    + * 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.ml.regression
    +
    +import scala.util.Random
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.ml.param.ParamsSuite
    +import org.apache.spark.ml.util.MLTestingUtils
    +import org.apache.spark.mllib.linalg.{DenseVector, Vector, Vectors}
    +import org.apache.spark.mllib.random.{ExponentialGenerator, WeibullGenerator}
    +import org.apache.spark.mllib.util.TestingUtils._
    +import org.apache.spark.mllib.util.MLlibTestSparkContext
    +import org.apache.spark.sql.{Row, DataFrame}
    +
    +private[ml] case class AFTPoint(features: Vector, censored: Double, label: Double)
    +
    +class AFTSurvivalRegressionSuite extends SparkFunSuite with MLlibTestSparkContext {
    +
    +  @transient var datasetUnivariate: DataFrame = _
    +  @transient var datasetMultivariate: DataFrame = _
    +
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +    datasetUnivariate = sqlContext.createDataFrame(
    +      sc.parallelize(generateAFTInput(
    +        1, Array(5.5), Array(0.8), 1000, 42, 1.0, 2.0, 2.0)))
    +    datasetMultivariate = sqlContext.createDataFrame(
    +      sc.parallelize(generateAFTInput(
    +        2, Array(0.9, -1.3), Array(0.7, 1.2), 1000, 42, 1.5, 2.5, 2.0)))
    +  }
    +
    +  test("params") {
    +    ParamsSuite.checkParams(new AFTSurvivalRegression)
    +    val model = new AFTSurvivalRegressionModel("aftSurvReg", Vectors.dense(0.0), 0.0,
0.0)
    +    ParamsSuite.checkParams(model)
    +  }
    +
    +  test("aft survival regression: default params") {
    +    val aftr = new AFTSurvivalRegression
    +    assert(aftr.getLabelCol === "label")
    +    assert(aftr.getFeaturesCol === "features")
    +    assert(aftr.getPredictionCol === "prediction")
    +    assert(aftr.getCensorCol === "censored")
    +    assert(aftr.getFitIntercept)
    +    assert(aftr.getMaxIter === 100)
    +    assert(aftr.getTol === 1E-6)
    +    val model = aftr.fit(datasetUnivariate)
    +
    +    // copied model must have the same parent.
    +    MLTestingUtils.checkCopy(model)
    +
    +    model.transform(datasetUnivariate)
    +      .select("label", "prediction")
    +      .collect()
    +    assert(model.getFeaturesCol === "features")
    +    assert(model.getPredictionCol === "prediction")
    +    assert(model.intercept !== 0.0)
    +    assert(model.hasParent)
    +  }
    +
    +  def generateAFTInput(
    +      numFeatures: Int,
    +      xMean: Array[Double],
    +      xVariance: Array[Double],
    +      nPoints: Int,
    +      seed: Int,
    +      weibullShape: Double,
    +      weibullScale: Double,
    +      exponentialMean: Double): Seq[AFTPoint] = {
    +
    +    def censored(x: Double, y: Double): Double = { if (x <= y) 1.0 else 0.0 }
    +
    +    val weibull = new WeibullGenerator(weibullShape, weibullScale)
    +    weibull.setSeed(seed)
    +
    +    val exponential = new ExponentialGenerator(exponentialMean)
    +    exponential.setSeed(seed)
    +
    +    val rnd = new Random(seed)
    +    val x = Array.fill[Array[Double]](nPoints)(Array.fill[Double](numFeatures)(rnd.nextDouble()))
    +
    +    x.foreach { v =>
    +      var i = 0
    +      val len = v.length
    +      while (i < len) {
    +        v(i) = (v(i) - 0.5) * math.sqrt(12.0 * xVariance(i)) + xMean(i)
    +        i += 1
    +      }
    +    }
    +    val y = (1 to nPoints).map { i => (weibull.nextValue(), exponential.nextValue())
}
    +
    +    y.zip(x).map { p => AFTPoint(Vectors.dense(p._2), censored(p._1._1, p._1._2),
p._1._1) }
    +  }
    +
    +  test("aft survival regression with univariate") {
    +    val trainer = new AFTSurvivalRegression
    +    val model = trainer.fit(datasetUnivariate)
    +
    +    /*
    +       Using the following R code to load the data and train the model using survival
package.
    +
    +       > library("survival")
    +       > data <- read.csv("path", header=FALSE, stringsAsFactors=FALSE)
    +       > features <- as.matrix(data.frame(as.numeric(data$V1)))
    +       > censored <- as.numeric(data$V2)
    +       > label <- as.numeric(data$V3)
    +       > sr.fit <- survreg(Surv(label, censored)~features, dist='weibull')
    +       > summary(sr.fit)
    +
    +       survreg(formula = Surv(label, censored) ~ features, dist = "weibull")
    +                    Value Std. Error      z        p
    +       (Intercept)  1.759     0.4141  4.247 2.16e-05
    +       features    -0.039     0.0735 -0.531 5.96e-01
    +       Log(scale)   0.344     0.0379  9.073 1.16e-19
    +
    +       Scale= 1.41
    +
    +       Weibull distribution
    +       Loglik(model)= -1152.2   Loglik(intercept only)= -1152.3
    +           Chisq= 0.28 on 1 degrees of freedom, p= 0.6
    +       Number of Newton-Raphson Iterations: 5
    +       n= 1000
    +     */
    +    val weightsR = Vectors.dense(-0.039)
    +    val interceptR = 1.759
    +    val scaleR = 1.41
    +
    +    assert(model.intercept ~== interceptR relTol 1E-3)
    +    assert(model.weights ~= weightsR relTol 1E-3)
    --- End diff --
    
    just curious why use `~=` instead `~==`?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message