1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
/*
* 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.feature.Instance
import org.apache.spark.ml.param.ParamsSuite
import org.apache.spark.ml.util.MLTestingUtils
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.linalg.{Vector, DenseVector, Vectors}
import org.apache.spark.mllib.util.{LinearDataGenerator, MLlibTestSparkContext}
import org.apache.spark.mllib.util.TestingUtils._
import org.apache.spark.sql.{DataFrame, Row}
class LinearRegressionSuite extends SparkFunSuite with MLlibTestSparkContext {
private val seed: Int = 42
@transient var datasetWithDenseFeature: DataFrame = _
@transient var datasetWithDenseFeatureWithoutIntercept: DataFrame = _
@transient var datasetWithSparseFeature: DataFrame = _
/*
In `LinearRegressionSuite`, we will make sure that the model trained by SparkML
is the same as the one trained by R's glmnet package. The following instruction
describes how to reproduce the data in R.
import org.apache.spark.mllib.util.LinearDataGenerator
val data =
sc.parallelize(LinearDataGenerator.generateLinearInput(6.3, Array(4.7, 7.2),
Array(0.9, -1.3), Array(0.7, 1.2), 10000, 42, 0.1), 2)
data.map(x=> x.label + ", " + x.features(0) + ", " + x.features(1)).coalesce(1)
.saveAsTextFile("path")
*/
override def beforeAll(): Unit = {
super.beforeAll()
datasetWithDenseFeature = sqlContext.createDataFrame(
sc.parallelize(LinearDataGenerator.generateLinearInput(
intercept = 6.3, weights = Array(4.7, 7.2), xMean = Array(0.9, -1.3),
xVariance = Array(0.7, 1.2), nPoints = 10000, seed, eps = 0.1), 2))
/*
datasetWithoutIntercept is not needed for correctness testing but is useful for illustrating
training model without intercept
*/
datasetWithDenseFeatureWithoutIntercept = sqlContext.createDataFrame(
sc.parallelize(LinearDataGenerator.generateLinearInput(
intercept = 0.0, weights = Array(4.7, 7.2), xMean = Array(0.9, -1.3),
xVariance = Array(0.7, 1.2), nPoints = 10000, seed, eps = 0.1), 2))
val r = new Random(seed)
// When feature size is larger than 4096, normal optimizer is choosed
// as the solver of linear regression in the case of "auto" mode.
val featureSize = 4100
datasetWithSparseFeature = sqlContext.createDataFrame(
sc.parallelize(LinearDataGenerator.generateLinearInput(
intercept = 0.0, weights = Seq.fill(featureSize)(r.nextDouble).toArray,
xMean = Seq.fill(featureSize)(r.nextDouble).toArray,
xVariance = Seq.fill(featureSize)(r.nextDouble).toArray, nPoints = 200,
seed, eps = 0.1, sparsity = 0.7), 2))
}
test("params") {
ParamsSuite.checkParams(new LinearRegression)
val model = new LinearRegressionModel("linearReg", Vectors.dense(0.0), 0.0)
ParamsSuite.checkParams(model)
}
test("linear regression: default params") {
val lir = new LinearRegression
assert(lir.getLabelCol === "label")
assert(lir.getFeaturesCol === "features")
assert(lir.getPredictionCol === "prediction")
assert(lir.getRegParam === 0.0)
assert(lir.getElasticNetParam === 0.0)
assert(lir.getFitIntercept)
assert(lir.getStandardization)
assert(lir.getSolver == "auto")
val model = lir.fit(datasetWithDenseFeature)
// copied model must have the same parent.
MLTestingUtils.checkCopy(model)
model.transform(datasetWithDenseFeature)
.select("label", "prediction")
.collect()
assert(model.getFeaturesCol === "features")
assert(model.getPredictionCol === "prediction")
assert(model.intercept !== 0.0)
assert(model.hasParent)
val numFeatures = datasetWithDenseFeature.select("features").first().getAs[Vector](0).size
assert(model.numFeatures === numFeatures)
}
test("linear regression with intercept without regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = new LinearRegression().setSolver(solver)
// The result should be the same regardless of standardization without regularization
val trainer2 = (new LinearRegression).setStandardization(false).setSolver(solver)
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
Using the following R code to load the data and train the model using glmnet package.
library("glmnet")
data <- read.csv("path", header=FALSE, stringsAsFactors=FALSE)
features <- as.matrix(data.frame(as.numeric(data$V2), as.numeric(data$V3)))
label <- as.numeric(data$V1)
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0, lambda = 0))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 6.298698
as.numeric.data.V2. 4.700706
as.numeric.data.V3. 7.199082
*/
val interceptR = 6.298698
val coefficientsR = Vectors.dense(4.700706, 7.199082)
assert(model1.intercept ~== interceptR relTol 1E-3)
assert(model1.coefficients ~= coefficientsR relTol 1E-3)
assert(model2.intercept ~== interceptR relTol 1E-3)
assert(model2.coefficients ~= coefficientsR relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction").collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
test("linear regression without intercept without regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setFitIntercept(false).setSolver(solver)
// Without regularization the results should be the same
val trainer2 = (new LinearRegression).setFitIntercept(false).setStandardization(false)
.setSolver(solver)
val model1 = trainer1.fit(datasetWithDenseFeature)
val modelWithoutIntercept1 = trainer1.fit(datasetWithDenseFeatureWithoutIntercept)
val model2 = trainer2.fit(datasetWithDenseFeature)
val modelWithoutIntercept2 = trainer2.fit(datasetWithDenseFeatureWithoutIntercept)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0, lambda = 0,
intercept = FALSE))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data.V2. 6.995908
as.numeric.data.V3. 5.275131
*/
val coefficientsR = Vectors.dense(6.995908, 5.275131)
assert(model1.intercept ~== 0 absTol 1E-3)
assert(model1.coefficients ~= coefficientsR relTol 1E-3)
assert(model2.intercept ~== 0 absTol 1E-3)
assert(model2.coefficients ~= coefficientsR relTol 1E-3)
/*
Then again with the data with no intercept:
> coefficientsWithourIntercept
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data3.V2. 4.70011
as.numeric.data3.V3. 7.19943
*/
val coefficientsWithourInterceptR = Vectors.dense(4.70011, 7.19943)
assert(modelWithoutIntercept1.intercept ~== 0 absTol 1E-3)
assert(modelWithoutIntercept1.coefficients ~= coefficientsWithourInterceptR relTol 1E-3)
assert(modelWithoutIntercept2.intercept ~== 0 absTol 1E-3)
assert(modelWithoutIntercept2.coefficients ~= coefficientsWithourInterceptR relTol 1E-3)
}
}
test("linear regression with intercept with L1 regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setElasticNetParam(1.0).setRegParam(0.57)
.setSolver(solver)
val trainer2 = (new LinearRegression).setElasticNetParam(1.0).setRegParam(0.57)
.setSolver(solver).setStandardization(false)
// Normal optimizer is not supported with only L1 regularization case.
if (solver == "normal") {
intercept[IllegalArgumentException] {
trainer1.fit(datasetWithDenseFeature)
trainer2.fit(datasetWithDenseFeature)
}
} else {
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
coefficients <- coef(glmnet(features, label, family="gaussian",
alpha = 1.0, lambda = 0.57 ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 6.24300
as.numeric.data.V2. 4.024821
as.numeric.data.V3. 6.679841
*/
val interceptR1 = 6.24300
val coefficientsR1 = Vectors.dense(4.024821, 6.679841)
assert(model1.intercept ~== interceptR1 relTol 1E-3)
assert(model1.coefficients ~= coefficientsR1 relTol 1E-3)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 1.0,
lambda = 0.57, standardize=FALSE ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 6.416948
as.numeric.data.V2. 3.893869
as.numeric.data.V3. 6.724286
*/
val interceptR2 = 6.416948
val coefficientsR2 = Vectors.dense(3.893869, 6.724286)
assert(model2.intercept ~== interceptR2 relTol 1E-3)
assert(model2.coefficients ~= coefficientsR2 relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction")
.collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
}
test("linear regression without intercept with L1 regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setElasticNetParam(1.0).setRegParam(0.57)
.setFitIntercept(false).setSolver(solver)
val trainer2 = (new LinearRegression).setElasticNetParam(1.0).setRegParam(0.57)
.setFitIntercept(false).setStandardization(false).setSolver(solver)
// Normal optimizer is not supported with only L1 regularization case.
if (solver == "normal") {
intercept[IllegalArgumentException] {
trainer1.fit(datasetWithDenseFeature)
trainer2.fit(datasetWithDenseFeature)
}
} else {
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 1.0,
lambda = 0.57, intercept=FALSE ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data.V2. 6.299752
as.numeric.data.V3. 4.772913
*/
val interceptR1 = 0.0
val coefficientsR1 = Vectors.dense(6.299752, 4.772913)
assert(model1.intercept ~== interceptR1 absTol 1E-3)
assert(model1.coefficients ~= coefficientsR1 relTol 1E-3)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 1.0,
lambda = 0.57, intercept=FALSE, standardize=FALSE ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data.V2. 6.232193
as.numeric.data.V3. 4.764229
*/
val interceptR2 = 0.0
val coefficientsR2 = Vectors.dense(6.232193, 4.764229)
assert(model2.intercept ~== interceptR2 absTol 1E-3)
assert(model2.coefficients ~= coefficientsR2 relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction")
.collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
}
test("linear regression with intercept with L2 regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setElasticNetParam(0.0).setRegParam(2.3)
.setSolver(solver)
val trainer2 = (new LinearRegression).setElasticNetParam(0.0).setRegParam(2.3)
.setStandardization(false).setSolver(solver)
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.0, lambda = 2.3))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 5.269376
as.numeric.data.V2. 3.736216
as.numeric.data.V3. 5.712356)
*/
val interceptR1 = 5.269376
val coefficientsR1 = Vectors.dense(3.736216, 5.712356)
assert(model1.intercept ~== interceptR1 relTol 1E-3)
assert(model1.coefficients ~= coefficientsR1 relTol 1E-3)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.0, lambda = 2.3,
standardize=FALSE))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 5.791109
as.numeric.data.V2. 3.435466
as.numeric.data.V3. 5.910406
*/
val interceptR2 = 5.791109
val coefficientsR2 = Vectors.dense(3.435466, 5.910406)
assert(model2.intercept ~== interceptR2 relTol 1E-3)
assert(model2.coefficients ~= coefficientsR2 relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction").collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
test("linear regression without intercept with L2 regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setElasticNetParam(0.0).setRegParam(2.3)
.setFitIntercept(false).setSolver(solver)
val trainer2 = (new LinearRegression).setElasticNetParam(0.0).setRegParam(2.3)
.setFitIntercept(false).setStandardization(false).setSolver(solver)
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.0, lambda = 2.3,
intercept = FALSE))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data.V2. 5.522875
as.numeric.data.V3. 4.214502
*/
val interceptR1 = 0.0
val coefficientsR1 = Vectors.dense(5.522875, 4.214502)
assert(model1.intercept ~== interceptR1 absTol 1E-3)
assert(model1.coefficients ~= coefficientsR1 relTol 1E-3)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.0, lambda = 2.3,
intercept = FALSE, standardize=FALSE))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data.V2. 5.263704
as.numeric.data.V3. 4.187419
*/
val interceptR2 = 0.0
val coefficientsR2 = Vectors.dense(5.263704, 4.187419)
assert(model2.intercept ~== interceptR2 absTol 1E-3)
assert(model2.coefficients ~= coefficientsR2 relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction").collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
test("linear regression with intercept with ElasticNet regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setElasticNetParam(0.3).setRegParam(1.6)
.setSolver(solver)
val trainer2 = (new LinearRegression).setElasticNetParam(0.3).setRegParam(1.6)
.setStandardization(false).setSolver(solver)
// Normal optimizer is not supported with non-zero elasticnet parameter.
if (solver == "normal") {
intercept[IllegalArgumentException] {
trainer1.fit(datasetWithDenseFeature)
trainer2.fit(datasetWithDenseFeature)
}
} else {
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.3,
lambda = 1.6 ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 6.324108
as.numeric.data.V2. 3.168435
as.numeric.data.V3. 5.200403
*/
val interceptR1 = 5.696056
val coefficientsR1 = Vectors.dense(3.670489, 6.001122)
assert(model1.intercept ~== interceptR1 relTol 1E-3)
assert(model1.coefficients ~= coefficientsR1 relTol 1E-3)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.3, lambda = 1.6
standardize=FALSE))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) 6.114723
as.numeric.data.V2. 3.409937
as.numeric.data.V3. 6.146531
*/
val interceptR2 = 6.114723
val coefficientsR2 = Vectors.dense(3.409937, 6.146531)
assert(model2.intercept ~== interceptR2 relTol 1E-3)
assert(model2.coefficients ~= coefficientsR2 relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction")
.collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
}
test("linear regression without intercept with ElasticNet regularization") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer1 = (new LinearRegression).setElasticNetParam(0.3).setRegParam(1.6)
.setFitIntercept(false).setSolver(solver)
val trainer2 = (new LinearRegression).setElasticNetParam(0.3).setRegParam(1.6)
.setFitIntercept(false).setStandardization(false).setSolver(solver)
// Normal optimizer is not supported with non-zero elasticnet parameter.
if (solver == "normal") {
intercept[IllegalArgumentException] {
trainer1.fit(datasetWithDenseFeature)
trainer2.fit(datasetWithDenseFeature)
}
} else {
val model1 = trainer1.fit(datasetWithDenseFeature)
val model2 = trainer2.fit(datasetWithDenseFeature)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.3,
lambda = 1.6, intercept=FALSE ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.dataM.V2. 5.673348
as.numeric.dataM.V3. 4.322251
*/
val interceptR1 = 0.0
val coefficientsR1 = Vectors.dense(5.673348, 4.322251)
assert(model1.intercept ~== interceptR1 absTol 1E-3)
assert(model1.coefficients ~= coefficientsR1 relTol 1E-3)
/*
coefficients <- coef(glmnet(features, label, family="gaussian", alpha = 0.3,
lambda = 1.6, intercept=FALSE, standardize=FALSE ))
> coefficients
3 x 1 sparse Matrix of class "dgCMatrix"
s0
(Intercept) .
as.numeric.data.V2. 5.477988
as.numeric.data.V3. 4.297622
*/
val interceptR2 = 0.0
val coefficientsR2 = Vectors.dense(5.477988, 4.297622)
assert(model2.intercept ~== interceptR2 absTol 1E-3)
assert(model2.coefficients ~= coefficientsR2 relTol 1E-3)
model1.transform(datasetWithDenseFeature).select("features", "prediction")
.collect().foreach {
case Row(features: DenseVector, prediction1: Double) =>
val prediction2 =
features(0) * model1.coefficients(0) + features(1) * model1.coefficients(1) +
model1.intercept
assert(prediction1 ~== prediction2 relTol 1E-5)
}
}
}
}
test("linear regression model training summary") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer = new LinearRegression().setSolver(solver)
val model = trainer.fit(datasetWithDenseFeature)
val trainerNoPredictionCol = trainer.setPredictionCol("")
val modelNoPredictionCol = trainerNoPredictionCol.fit(datasetWithDenseFeature)
// Training results for the model should be available
assert(model.hasSummary)
assert(modelNoPredictionCol.hasSummary)
// Schema should be a superset of the input dataset
assert((datasetWithDenseFeature.schema.fieldNames.toSet + "prediction").subsetOf(
model.summary.predictions.schema.fieldNames.toSet))
// Validate that we re-insert a prediction column for evaluation
val modelNoPredictionColFieldNames
= modelNoPredictionCol.summary.predictions.schema.fieldNames
assert((datasetWithDenseFeature.schema.fieldNames.toSet).subsetOf(
modelNoPredictionColFieldNames.toSet))
assert(modelNoPredictionColFieldNames.exists(s => s.startsWith("prediction_")))
// Residuals in [[LinearRegressionResults]] should equal those manually computed
val expectedResiduals = datasetWithDenseFeature.select("features", "label")
.map { case Row(features: DenseVector, label: Double) =>
val prediction =
features(0) * model.coefficients(0) + features(1) * model.coefficients(1) +
model.intercept
label - prediction
}
.zip(model.summary.residuals.map(_.getDouble(0)))
.collect()
.foreach { case (manualResidual: Double, resultResidual: Double) =>
assert(manualResidual ~== resultResidual relTol 1E-5)
}
/*
Use the following R code to generate model training results.
predictions <- predict(fit, newx=features)
residuals <- label - predictions
> mean(residuals^2) # MSE
[1] 0.009720325
> mean(abs(residuals)) # MAD
[1] 0.07863206
> cor(predictions, label)^2# r^2
[,1]
s0 0.9998749
*/
assert(model.summary.meanSquaredError ~== 0.00972035 relTol 1E-5)
assert(model.summary.meanAbsoluteError ~== 0.07863206 relTol 1E-5)
assert(model.summary.r2 ~== 0.9998749 relTol 1E-5)
// Normal solver uses "WeightedLeastSquares". This algorithm does not generate
// objective history because it does not run through iterations.
if (solver == "l-bfgs") {
// Objective function should be monotonically decreasing for linear regression
assert(
model.summary
.objectiveHistory
.sliding(2)
.forall(x => x(0) >= x(1)))
} else {
// To clalify that the normal solver is used here.
assert(model.summary.objectiveHistory.length == 1)
assert(model.summary.objectiveHistory(0) == 0.0)
}
}
}
test("linear regression model testset evaluation summary") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val trainer = new LinearRegression().setSolver(solver)
val model = trainer.fit(datasetWithDenseFeature)
// Evaluating on training dataset should yield results summary equal to training summary
val testSummary = model.evaluate(datasetWithDenseFeature)
assert(model.summary.meanSquaredError ~== testSummary.meanSquaredError relTol 1E-5)
assert(model.summary.r2 ~== testSummary.r2 relTol 1E-5)
model.summary.residuals.select("residuals").collect()
.zip(testSummary.residuals.select("residuals").collect())
.forall { case (Row(r1: Double), Row(r2: Double)) => r1 ~== r2 relTol 1E-5 }
}
}
test("linear regression with weighted samples") {
Seq("auto", "l-bfgs", "normal").foreach { solver =>
val (data, weightedData) = {
val activeData = LinearDataGenerator.generateLinearInput(
6.3, Array(4.7, 7.2), Array(0.9, -1.3), Array(0.7, 1.2), 500, 1, 0.1)
val rnd = new Random(8392)
val signedData = activeData.map { case p: LabeledPoint =>
(rnd.nextGaussian() > 0.0, p)
}
val data1 = signedData.flatMap {
case (true, p) => Iterator(p, p)
case (false, p) => Iterator(p)
}
val weightedSignedData = signedData.flatMap {
case (true, LabeledPoint(label, features)) =>
Iterator(
Instance(label, weight = 1.2, features),
Instance(label, weight = 0.8, features)
)
case (false, LabeledPoint(label, features)) =>
Iterator(
Instance(label, weight = 0.3, features),
Instance(label, weight = 0.1, features),
Instance(label, weight = 0.6, features)
)
}
val noiseData = LinearDataGenerator.generateLinearInput(
2, Array(1, 3), Array(0.9, -1.3), Array(0.7, 1.2), 500, 1, 0.1)
val weightedNoiseData = noiseData.map {
case LabeledPoint(label, features) => Instance(label, weight = 0, features)
}
val data2 = weightedSignedData ++ weightedNoiseData
(sqlContext.createDataFrame(sc.parallelize(data1, 4)),
sqlContext.createDataFrame(sc.parallelize(data2, 4)))
}
val trainer1a = (new LinearRegression).setFitIntercept(true)
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(true).setSolver(solver)
val trainer1b = (new LinearRegression).setFitIntercept(true).setWeightCol("weight")
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(true).setSolver(solver)
// Normal optimizer is not supported with non-zero elasticnet parameter.
val model1a0 = trainer1a.fit(data)
val model1a1 = trainer1a.fit(weightedData)
val model1b = trainer1b.fit(weightedData)
assert(model1a0.coefficients !~= model1a1.coefficients absTol 1E-3)
assert(model1a0.intercept !~= model1a1.intercept absTol 1E-3)
assert(model1a0.coefficients ~== model1b.coefficients absTol 1E-3)
assert(model1a0.intercept ~== model1b.intercept absTol 1E-3)
val trainer2a = (new LinearRegression).setFitIntercept(true)
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(false).setSolver(solver)
val trainer2b = (new LinearRegression).setFitIntercept(true).setWeightCol("weight")
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(false).setSolver(solver)
val model2a0 = trainer2a.fit(data)
val model2a1 = trainer2a.fit(weightedData)
val model2b = trainer2b.fit(weightedData)
assert(model2a0.coefficients !~= model2a1.coefficients absTol 1E-3)
assert(model2a0.intercept !~= model2a1.intercept absTol 1E-3)
assert(model2a0.coefficients ~== model2b.coefficients absTol 1E-3)
assert(model2a0.intercept ~== model2b.intercept absTol 1E-3)
val trainer3a = (new LinearRegression).setFitIntercept(false)
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(true).setSolver(solver)
val trainer3b = (new LinearRegression).setFitIntercept(false).setWeightCol("weight")
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(true).setSolver(solver)
val model3a0 = trainer3a.fit(data)
val model3a1 = trainer3a.fit(weightedData)
val model3b = trainer3b.fit(weightedData)
assert(model3a0.coefficients !~= model3a1.coefficients absTol 1E-3)
assert(model3a0.coefficients ~== model3b.coefficients absTol 1E-3)
val trainer4a = (new LinearRegression).setFitIntercept(false)
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(false).setSolver(solver)
val trainer4b = (new LinearRegression).setFitIntercept(false).setWeightCol("weight")
.setElasticNetParam(0.0).setRegParam(0.21).setStandardization(false).setSolver(solver)
val model4a0 = trainer4a.fit(data)
val model4a1 = trainer4a.fit(weightedData)
val model4b = trainer4b.fit(weightedData)
assert(model4a0.coefficients !~= model4a1.coefficients absTol 1E-3)
assert(model4a0.coefficients ~== model4b.coefficients absTol 1E-3)
}
}
test("linear regression model with l-bfgs with big feature datasets") {
val trainer = new LinearRegression().setSolver("auto")
val model = trainer.fit(datasetWithSparseFeature)
// Training results for the model should be available
assert(model.hasSummary)
// When LBFGS is used as optimizer, objective history can be restored.
assert(
model.summary
.objectiveHistory
.sliding(2)
.forall(x => x(0) >= x(1)))
}
}
|