|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.ml.regression |
| 19 | + |
| 20 | +import org.scalatest.FunSuite |
| 21 | + |
| 22 | +import org.apache.spark.ml.LabeledPoint |
| 23 | +import org.apache.spark.mllib.classification.LogisticRegressionSuite.generateLogisticInput |
| 24 | +import org.apache.spark.mllib.linalg.Vector |
| 25 | +import org.apache.spark.mllib.util.MLlibTestSparkContext |
| 26 | +import org.apache.spark.mllib.util.TestingUtils._ |
| 27 | +import org.apache.spark.sql.{Row, SQLContext, SchemaRDD} |
| 28 | + |
| 29 | +class LinearRegressionSuite extends FunSuite with MLlibTestSparkContext { |
| 30 | + |
| 31 | + @transient var sqlContext: SQLContext = _ |
| 32 | + @transient var dataset: SchemaRDD = _ |
| 33 | + |
| 34 | + override def beforeAll(): Unit = { |
| 35 | + super.beforeAll() |
| 36 | + sqlContext = new SQLContext(sc) |
| 37 | + dataset = sqlContext.createSchemaRDD( |
| 38 | + sc.parallelize(generateLogisticInput(1.0, 1.0, nPoints = 100, seed = 42), 2)) |
| 39 | + } |
| 40 | + |
| 41 | + test("linear regression: default params") { |
| 42 | + val sqlContext = this.sqlContext |
| 43 | + import sqlContext._ |
| 44 | + val lr = new LinearRegression |
| 45 | + assert(lr.getLabelCol == "label") |
| 46 | + val model = lr.fit(dataset) |
| 47 | + model.transform(dataset) |
| 48 | + .select('label, 'prediction) |
| 49 | + .collect() |
| 50 | + // Check defaults |
| 51 | + assert(model.getFeaturesCol == "features") |
| 52 | + assert(model.getPredictionCol == "prediction") |
| 53 | + } |
| 54 | + |
| 55 | + test("linear regression with setters") { |
| 56 | + // Set params, train, and check as many as we can. |
| 57 | + val sqlContext = this.sqlContext |
| 58 | + import sqlContext._ |
| 59 | + val lr = new LinearRegression() |
| 60 | + .setMaxIter(10) |
| 61 | + .setRegParam(1.0) |
| 62 | + val model = lr.fit(dataset) |
| 63 | + assert(model.fittingParamMap.get(lr.maxIter) === Some(10)) |
| 64 | + assert(model.fittingParamMap.get(lr.regParam) === Some(1.0)) |
| 65 | + |
| 66 | + // Call fit() with new params, and check as many as we can. |
| 67 | + val model2 = lr.fit(dataset, lr.maxIter -> 5, lr.regParam -> 0.1, lr.predictionCol -> "thePred") |
| 68 | + assert(model2.fittingParamMap.get(lr.maxIter) === Some(5)) |
| 69 | + assert(model2.fittingParamMap.get(lr.regParam) === Some(0.1)) |
| 70 | + assert(model2.getPredictionCol == "thePred") |
| 71 | + } |
| 72 | + |
| 73 | + test("linear regression: Predictor, Regressor methods") { |
| 74 | + val sqlContext = this.sqlContext |
| 75 | + import sqlContext._ |
| 76 | + val lr = new LinearRegression |
| 77 | + |
| 78 | + // fit() vs. train() |
| 79 | + val model1 = lr.fit(dataset) |
| 80 | + val rdd = dataset.select('label, 'features).map { case Row(label: Double, features: Vector) => |
| 81 | + LabeledPoint(label, features) |
| 82 | + } |
| 83 | + val features = rdd.map(_.features) |
| 84 | + val model2 = lr.train(rdd) |
| 85 | + assert(model1.intercept == model2.intercept) |
| 86 | + assert(model1.weights.equals(model2.weights)) |
| 87 | + |
| 88 | + // transform() vs. predict() |
| 89 | + val trans = model1.transform(dataset).select('prediction) |
| 90 | + val preds = model1.predict(rdd.map(_.features)) |
| 91 | + trans.zip(preds).collect().foreach { case (Row(pred1: Double), pred2: Double) => |
| 92 | + assert(pred1 == pred2) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments