@@ -20,6 +20,7 @@ package org.apache.spark.mllib.regression
20
20
import org .scalatest .{Matchers , FunSuite }
21
21
22
22
import org .apache .spark .mllib .util .MLlibTestSparkContext
23
+ import org .apache .spark .mllib .util .TestingUtils ._
23
24
24
25
class IsotonicRegressionSuite extends FunSuite with MLlibTestSparkContext with Matchers {
25
26
@@ -55,80 +56,67 @@ class IsotonicRegressionSuite extends FunSuite with MLlibTestSparkContext with M
55
56
56
57
test(" increasing isotonic regression" ) {
57
58
val model = runIsotonicRegression(Seq (1 , 2 , 3 , 3 , 1 , 6 , 17 , 16 , 17 , 18 ), true )
58
-
59
59
assert(model.predictions === Array (1 , 2 , 7d / 3 , 7d / 3 , 7d / 3 , 6 , 16.5 , 16.5 , 17 , 18 ))
60
60
}
61
61
62
62
test(" isotonic regression with size 0" ) {
63
63
val model = runIsotonicRegression(Seq (), true )
64
-
65
64
assert(model.predictions === Array ())
66
65
}
67
66
68
67
test(" isotonic regression with size 1" ) {
69
68
val model = runIsotonicRegression(Seq (1 ), true )
70
-
71
69
assert(model.predictions === Array (1.0 ))
72
70
}
73
71
74
72
test(" isotonic regression strictly increasing sequence" ) {
75
73
val model = runIsotonicRegression(Seq (1 , 2 , 3 , 4 , 5 ), true )
76
-
77
74
assert(model.predictions === Array (1 , 2 , 3 , 4 , 5 ))
78
75
}
79
76
80
77
test(" isotonic regression strictly decreasing sequence" ) {
81
78
val model = runIsotonicRegression(Seq (5 , 4 , 3 , 2 , 1 ), true )
82
-
83
79
assert(model.predictions === Array (3 , 3 , 3 , 3 , 3 ))
84
80
}
85
81
86
82
test(" isotonic regression with last element violating monotonicity" ) {
87
83
val model = runIsotonicRegression(Seq (1 , 2 , 3 , 4 , 2 ), true )
88
-
89
84
assert(model.predictions === Array (1 , 2 , 3 , 3 , 3 ))
90
85
}
91
86
92
87
test(" isotonic regression with first element violating monotonicity" ) {
93
88
val model = runIsotonicRegression(Seq (4 , 2 , 3 , 4 , 5 ), true )
94
-
95
89
assert(model.predictions === Array (3 , 3 , 3 , 4 , 5 ))
96
90
}
97
91
98
92
test(" isotonic regression with negative labels" ) {
99
93
val model = runIsotonicRegression(Seq (- 1 , - 2 , 0 , 1 , - 1 ), true )
100
-
101
94
assert(model.predictions === Array (- 1.5 , - 1.5 , 0 , 0 , 0 ))
102
95
}
103
96
104
97
test(" isotonic regression with unordered input" ) {
105
98
val trainRDD = sc.parallelize(generateIsotonicInput(Seq (1 , 2 , 3 , 4 , 5 )).reverse).cache()
106
99
val model = new IsotonicRegression ().run(trainRDD)
107
-
108
100
assert(model.predictions === Array (1 , 2 , 3 , 4 , 5 ))
109
101
}
110
102
111
103
test(" weighted isotonic regression" ) {
112
104
val model = runIsotonicRegression(Seq (1 , 2 , 3 , 4 , 2 ), Seq (1 , 1 , 1 , 1 , 2 ), true )
113
-
114
105
assert(model.predictions === Array (1 , 2 , 2.75 , 2.75 ,2.75 ))
115
106
}
116
107
117
108
test(" weighted isotonic regression with weights lower than 1" ) {
118
109
val model = runIsotonicRegression(Seq (1 , 2 , 3 , 2 , 1 ), Seq (1 , 1 , 1 , 0.1 , 0.1 ), true )
119
-
120
110
assert(model.predictions.map(round) === Array (1 , 2 , 3.3 / 1.2 , 3.3 / 1.2 , 3.3 / 1.2 ))
121
111
}
122
112
123
113
test(" weighted isotonic regression with negative weights" ) {
124
114
val model = runIsotonicRegression(Seq (1 , 2 , 3 , 2 , 1 ), Seq (- 1 , 1 , - 3 , 1 , - 5 ), true )
125
-
126
115
assert(model.predictions === Array (1.0 , 10.0 / 6 , 10.0 / 6 , 10.0 / 6 , 10.0 / 6 ))
127
116
}
128
117
129
118
test(" weighted isotonic regression with zero weights" ) {
130
119
val model = runIsotonicRegression(Seq [Double ](1 , 2 , 3 , 2 , 1 ), Seq [Double ](0 , 0 , 0 , 1 , 0 ), true )
131
-
132
120
assert(model.predictions === Array (1 , 2 , 2 , 2 , 2 ))
133
121
}
134
122
@@ -186,4 +174,33 @@ class IsotonicRegressionSuite extends FunSuite with MLlibTestSparkContext with M
186
174
assert(model.predict(3 ) === 4 )
187
175
assert(model.predict(10 ) === 1 )
188
176
}
189
- }
177
+
178
+ test(" model construction" ) {
179
+ val model = new IsotonicRegressionModel (Array (0.0 , 1.0 ), Array (1.0 , 2.0 ), isotonic = true )
180
+ assert(model.predict(- 0.5 ) === 1.0 )
181
+ assert(model.predict(0.0 ) === 1.0 )
182
+ assert(model.predict(0.5 ) ~== 1.5 absTol 1e-14 )
183
+ assert(model.predict(1.0 ) === 2.0 )
184
+ assert(model.predict(1.5 ) === 2.0 )
185
+
186
+ intercept[IllegalArgumentException ] {
187
+ // different array sizes.
188
+ new IsotonicRegressionModel (Array (0.0 , 1.0 ), Array (1.0 ), isotonic = true )
189
+ }
190
+
191
+ intercept[IllegalArgumentException ] {
192
+ // unordered boundaries
193
+ new IsotonicRegressionModel (Array (1.0 , 0.0 ), Array (1.0 , 2.0 ), isotonic = true )
194
+ }
195
+
196
+ intercept[IllegalArgumentException ] {
197
+ // unordered predictions (isotonic)
198
+ new IsotonicRegressionModel (Array (0.0 , 1.0 ), Array (2.0 , 1.0 ), isotonic = true )
199
+ }
200
+
201
+ intercept[IllegalArgumentException ] {
202
+ // unordered predictions (antitonic)
203
+ new IsotonicRegressionModel (Array (0.0 , 1.0 ), Array (1.0 , 2.0 ), isotonic = false )
204
+ }
205
+ }
206
+ }
0 commit comments