17
17
18
18
package org .apache .spark .mllib .regression
19
19
20
- import org .apache .spark .mllib .linalg .Vectors
21
- import org .apache .spark .mllib .util .{LocalClusterSparkContext , MLlibTestSparkContext }
22
20
import org .scalatest .{Matchers , FunSuite }
23
- import scala .util .Random
21
+
22
+ import org .apache .spark .mllib .util .{LocalClusterSparkContext , MLlibTestSparkContext }
24
23
import org .apache .spark .mllib .util .IsotonicDataGenerator ._
25
24
26
25
class IsotonicRegressionSuite
@@ -32,26 +31,34 @@ class IsotonicRegressionSuite
32
31
Math .round(d * 100 ).toDouble / 100
33
32
34
33
test(" increasing isotonic regression" ) {
35
- val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 3 , 3 , 1 , 6 , 7 , 8 , 11 , 9 , 10 , 12 , 14 , 15 , 17 , 16 , 17 , 18 , 19 , 20 )).cache()
34
+ val trainRDD = sc.parallelize(
35
+ generateIsotonicInput(
36
+ 1 , 2 , 3 , 3 , 1 , 6 , 7 , 8 , 11 , 9 , 10 , 12 , 14 , 15 , 17 , 16 , 17 , 18 , 19 , 20 )).cache()
36
37
37
- val alg = new PoolAdjacentViolators
38
+ val alg = new IsotonicRegression
38
39
val model = alg.run(trainRDD, true )
39
40
40
- model.predictions should be(generateIsotonicInput(1 , 2 , 7d / 3 , 7d / 3 , 7d / 3 , 6 , 7 , 8 , 10 , 10 , 10 , 12 , 14 , 15 , 16.5 , 16.5 , 17 , 18 , 19 , 20 ))
41
+ model.predictions should be(
42
+ generateIsotonicInput(
43
+ 1 , 2 , 7d / 3 , 7d / 3 , 7d / 3 , 6 , 7 , 8 , 10 , 10 , 10 , 12 , 14 , 15 , 16.5 , 16.5 , 17 , 18 , 19 , 20 ))
41
44
}
42
45
43
46
test(" increasing isotonic regression using api" ) {
44
- val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 3 , 3 , 1 , 6 , 7 , 8 , 11 , 9 , 10 , 12 , 14 , 15 , 17 , 16 , 17 , 18 , 19 , 20 )).cache()
47
+ val trainRDD = sc.parallelize(
48
+ generateIsotonicInput(
49
+ 1 , 2 , 3 , 3 , 1 , 6 , 7 , 8 , 11 , 9 , 10 , 12 , 14 , 15 , 17 , 16 , 17 , 18 , 19 , 20 )).cache()
45
50
46
51
val model = IsotonicRegression .train(trainRDD, true )
47
52
48
- model.predictions should be(generateIsotonicInput(1 , 2 , 7d / 3 , 7d / 3 , 7d / 3 , 6 , 7 , 8 , 10 , 10 , 10 , 12 , 14 , 15 , 16.5 , 16.5 , 17 , 18 , 19 , 20 ))
53
+ model.predictions should be(
54
+ generateIsotonicInput(
55
+ 1 , 2 , 7d / 3 , 7d / 3 , 7d / 3 , 6 , 7 , 8 , 10 , 10 , 10 , 12 , 14 , 15 , 16.5 , 16.5 , 17 , 18 , 19 , 20 ))
49
56
}
50
57
51
58
test(" isotonic regression with size 0" ) {
52
59
val trainRDD = sc.parallelize(List [(Double , Double , Double )]()).cache()
53
60
54
- val alg = new PoolAdjacentViolators
61
+ val alg = new IsotonicRegression
55
62
val model = alg.run(trainRDD, true )
56
63
57
64
model.predictions should be(List ())
@@ -60,7 +67,7 @@ class IsotonicRegressionSuite
60
67
test(" isotonic regression with size 1" ) {
61
68
val trainRDD = sc.parallelize(generateIsotonicInput(1 )).cache()
62
69
63
- val alg = new PoolAdjacentViolators
70
+ val alg = new IsotonicRegression
64
71
val model = alg.run(trainRDD, true )
65
72
66
73
model.predictions should be(generateIsotonicInput(1 ))
@@ -69,7 +76,7 @@ class IsotonicRegressionSuite
69
76
test(" isotonic regression strictly increasing sequence" ) {
70
77
val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 3 , 4 , 5 )).cache()
71
78
72
- val alg = new PoolAdjacentViolators
79
+ val alg = new IsotonicRegression
73
80
val model = alg.run(trainRDD, true )
74
81
75
82
model.predictions should be(generateIsotonicInput(1 , 2 , 3 , 4 , 5 ))
@@ -78,7 +85,7 @@ class IsotonicRegressionSuite
78
85
test(" isotonic regression strictly decreasing sequence" ) {
79
86
val trainRDD = sc.parallelize(generateIsotonicInput(5 , 4 , 3 , 2 , 1 )).cache()
80
87
81
- val alg = new PoolAdjacentViolators
88
+ val alg = new IsotonicRegression
82
89
val model = alg.run(trainRDD, true )
83
90
84
91
model.predictions should be(generateIsotonicInput(3 , 3 , 3 , 3 , 3 ))
@@ -87,7 +94,7 @@ class IsotonicRegressionSuite
87
94
test(" isotonic regression with last element violating monotonicity" ) {
88
95
val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 3 , 4 , 2 )).cache()
89
96
90
- val alg = new PoolAdjacentViolators
97
+ val alg = new IsotonicRegression
91
98
val model = alg.run(trainRDD, true )
92
99
93
100
model.predictions should be(generateIsotonicInput(1 , 2 , 3 , 3 , 3 ))
@@ -96,7 +103,7 @@ class IsotonicRegressionSuite
96
103
test(" isotonic regression with first element violating monotonicity" ) {
97
104
val trainRDD = sc.parallelize(generateIsotonicInput(4 , 2 , 3 , 4 , 5 )).cache()
98
105
99
- val alg = new PoolAdjacentViolators
106
+ val alg = new IsotonicRegression
100
107
val model = alg.run(trainRDD, true )
101
108
102
109
model.predictions should be(generateIsotonicInput(3 , 3 , 3 , 4 , 5 ))
@@ -105,7 +112,7 @@ class IsotonicRegressionSuite
105
112
test(" isotonic regression with negative labels" ) {
106
113
val trainRDD = sc.parallelize(generateIsotonicInput(- 1 , - 2 , 0 , 1 , - 1 )).cache()
107
114
108
- val alg = new PoolAdjacentViolators
115
+ val alg = new IsotonicRegression
109
116
val model = alg.run(trainRDD, true )
110
117
111
118
model.predictions should be(generateIsotonicInput(- 1.5 , - 1.5 , 0 , 0 , 0 ))
@@ -114,45 +121,48 @@ class IsotonicRegressionSuite
114
121
test(" isotonic regression with unordered input" ) {
115
122
val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 3 , 4 , 5 ).reverse).cache()
116
123
117
- val alg = new PoolAdjacentViolators
124
+ val alg = new IsotonicRegression
118
125
val model = alg.run(trainRDD, true )
119
126
120
127
model.predictions should be(generateIsotonicInput(1 , 2 , 3 , 4 , 5 ))
121
128
}
122
129
123
130
test(" weighted isotonic regression" ) {
124
- val trainRDD = sc.parallelize(generateWeightedIsotonicInput(Seq (1 , 2 , 3 , 4 , 2 ), Seq (1 , 1 , 1 , 1 , 2 ))).cache()
131
+ val trainRDD = sc.parallelize(
132
+ generateWeightedIsotonicInput(Seq (1 , 2 , 3 , 4 , 2 ), Seq (1 , 1 , 1 , 1 , 2 ))).cache()
125
133
126
- val alg = new PoolAdjacentViolators
134
+ val alg = new IsotonicRegression
127
135
val model = alg.run(trainRDD, true )
128
136
129
- model.predictions should be(generateWeightedIsotonicInput(Seq (1 , 2 , 2.75 , 2.75 ,2.75 ), Seq (1 , 1 , 1 , 1 , 2 )))
137
+ model.predictions should be(
138
+ generateWeightedIsotonicInput(Seq (1 , 2 , 2.75 , 2.75 ,2.75 ), Seq (1 , 1 , 1 , 1 , 2 )))
130
139
}
131
140
132
141
test(" weighted isotonic regression with weights lower than 1" ) {
133
- val trainRDD = sc.parallelize(generateWeightedIsotonicInput(Seq (1 , 2 , 3 , 2 , 1 ), Seq (1 , 1 , 1 , 0.1 , 0.1 ))).cache()
142
+ val trainRDD = sc.parallelize(
143
+ generateWeightedIsotonicInput(Seq (1 , 2 , 3 , 2 , 1 ), Seq (1 , 1 , 1 , 0.1 , 0.1 ))).cache()
134
144
135
- val alg = new PoolAdjacentViolators
145
+ val alg = new IsotonicRegression
136
146
val model = alg.run(trainRDD, true )
137
147
138
- model.predictions.map(p => p.copy(_1 = round(p._1))) should be
139
- ( generateWeightedIsotonicInput(Seq (1 , 2 , 3.3 / 1.2 , 3.3 / 1.2 , 3.3 / 1.2 ), Seq (1 , 1 , 1 , 0.1 , 0.1 )))
148
+ model.predictions.map(p => p.copy(_1 = round(p._1))) should be(
149
+ generateWeightedIsotonicInput(Seq (1 , 2 , 3.3 / 1.2 , 3.3 / 1.2 , 3.3 / 1.2 ), Seq (1 , 1 , 1 , 0.1 , 0.1 )))
140
150
}
141
151
142
152
test(" weighted isotonic regression with negative weights" ) {
143
153
val trainRDD = sc.parallelize(generateWeightedIsotonicInput(Seq (1 , 2 , 3 , 2 , 1 ), Seq (- 1 , 1 , - 3 , 1 , - 5 ))).cache()
144
154
145
- val alg = new PoolAdjacentViolators
155
+ val alg = new IsotonicRegression
146
156
val model = alg.run(trainRDD, true )
147
157
148
- model.predictions.map(p => p.copy(_1 = round(p._1))) should be
149
- ( generateWeightedIsotonicInput(Seq (1 , 10 / 6 , 10 / 6 , 10 / 6 , 10 / 6 ), Seq (- 1 , 1 , - 3 , 1 , - 5 )))
158
+ model.predictions should be(
159
+ generateWeightedIsotonicInput(Seq (1.0 , 10.0 / 6 , 10.0 / 6 , 10.0 / 6 , 10.0 / 6 ), Seq (- 1 , 1 , - 3 , 1 , - 5 )))
150
160
}
151
161
152
162
test(" weighted isotonic regression with zero weights" ) {
153
163
val trainRDD = sc.parallelize(generateWeightedIsotonicInput(Seq (1 , 2 , 3 , 2 , 1 ), Seq (0 , 0 , 0 , 1 , 0 ))).cache()
154
164
155
- val alg = new PoolAdjacentViolators
165
+ val alg = new IsotonicRegression
156
166
val model = alg.run(trainRDD, true )
157
167
158
168
model.predictions should be(generateWeightedIsotonicInput(Seq (1 , 2 , 2 , 2 , 2 ), Seq (0 , 0 , 0 , 1 , 0 )))
@@ -161,7 +171,7 @@ class IsotonicRegressionSuite
161
171
test(" isotonic regression prediction" ) {
162
172
val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 7 , 1 , 2 )).cache()
163
173
164
- val alg = new PoolAdjacentViolators
174
+ val alg = new IsotonicRegression
165
175
val model = alg.run(trainRDD, true )
166
176
167
177
model.predict(0 ) should be(1 )
@@ -172,18 +182,18 @@ class IsotonicRegressionSuite
172
182
173
183
test(" isotonic regression RDD prediction" ) {
174
184
val trainRDD = sc.parallelize(generateIsotonicInput(1 , 2 , 7 , 1 , 2 )).cache()
175
- val testRDD = sc.parallelize(List (0d , 2d , 3d , 10d )).cache()
185
+ val testRDD = sc.parallelize(List (0.0 , 2.0 , 3.0 , 10.0 )).cache()
176
186
177
- val alg = new PoolAdjacentViolators
187
+ val alg = new IsotonicRegression
178
188
val model = alg.run(trainRDD, true )
179
189
180
- model.predict(testRDD).collect() should be(Array (1 , 2 , 10d / 3 , 10d / 3 ))
190
+ model.predict(testRDD).collect() should be(Array (1 , 2 , 10.0 / 3 , 10.0 / 3 ))
181
191
}
182
192
183
193
test(" antitonic regression prediction" ) {
184
194
val trainRDD = sc.parallelize(generateIsotonicInput(7 , 5 , 3 , 5 , 1 )).cache()
185
195
186
- val alg = new PoolAdjacentViolators
196
+ val alg = new IsotonicRegression
187
197
val model = alg.run(trainRDD, false )
188
198
189
199
model.predict(0 ) should be(7 )
0 commit comments