13
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
- */ /*
17
-
16
+ */
18
17
19
18
package org .apache .spark .mllib .regression ;
20
19
21
20
import org .apache .spark .api .java .JavaPairRDD ;
22
21
import org .apache .spark .api .java .JavaRDD ;
23
22
import org .apache .spark .api .java .JavaSparkContext ;
24
23
import org .apache .spark .api .java .function .Function ;
25
- import org.apache.spark.mllib.linalg.Vector;
26
- import org.apache.spark.mllib.linalg.Vectors;
27
24
import org .apache .spark .mllib .util .IsotonicDataGenerator ;
28
25
import org .junit .After ;
29
26
import org .junit .Assert ;
30
27
import org .junit .Before ;
31
28
import org .junit .Test ;
32
29
import scala .Tuple2 ;
33
- import scala.Tuple3;
34
30
35
31
import java .io .Serializable ;
36
- import java.util.Arrays;
37
32
import java .util .List ;
38
33
39
34
public class JavaIsotonicRegressionSuite implements Serializable {
@@ -50,52 +45,26 @@ public void tearDown() {
50
45
sc = null ;
51
46
}
52
47
53
- double difference(List<Tuple3<Double, Double, Double>> expected, IsotonicRegressionModel model) {
48
+ double difference (List <Tuple2 < Double , Double >> expected , IsotonicRegressionModel model ) {
54
49
double diff = 0 ;
55
50
56
51
for (int i = 0 ; i < model .predictions ().length (); i ++) {
57
- Tuple3<Double, Double, Double> exp = expected.get(i);
52
+ Tuple2 < Double , Double > exp = expected .get (i );
58
53
diff += Math .abs (model .predict (exp ._2 ()) - exp ._1 ());
59
54
}
60
55
61
56
return diff ;
62
57
}
63
58
64
- */
65
- /*@Test
66
- public void runIsotonicRegressionUsingConstructor() {
67
- JavaRDD<Tuple3<Double, Double, Double>> testRDD = sc.parallelize(IsotonicDataGenerator
68
- .generateIsotonicInputAsList(
69
- new double[]{1, 2, 3, 3, 1, 6, 7, 8, 11, 9, 10, 12})).cache();
70
-
71
- IsotonicRegressionAlgorithm isotonicRegressionAlgorithm = new PoolAdjacentViolators();
72
- IsotonicRegressionModel model = isotonicRegressionAlgorithm.run(testRDD.rdd(), true);
73
-
74
- List<Tuple3<Double, Double, Double>> expected = IsotonicDataGenerator
75
- .generateIsotonicInputAsList(
76
- new double[] {1, 2, 7d/3, 7d/3, 7d/3, 6, 7, 8, 10, 10, 10, 12});
77
-
78
- Assert.assertTrue(difference(expected, model) == 0);
79
- }*/ /*
80
-
81
-
82
59
@ Test
83
60
public void runIsotonicRegressionUsingStaticMethod () {
84
- */
85
- /*JavaRDD<Tuple3<Double, Double, Double>> testRDD = sc.parallelize(IsotonicDataGenerator
86
- .generateIsotonicInputAsList(
87
- new double[] {1, 2, 3, 3, 1, 6, 7, 8, 11, 9, 10, 12})).cache();*/ /*
88
-
89
-
90
- */
91
- /*JavaRDD<Tuple3<Double, Double, Double>> testRDD = sc.parallelize(Arrays.asList(new Tuple3(1.0, 1.0, 1.0)));*/ /*
61
+ JavaPairRDD <Double , Double > trainRDD = sc .parallelizePairs (
62
+ IsotonicDataGenerator .generateIsotonicInputAsList (
63
+ new double []{1 , 2 , 3 , 3 , 1 , 6 , 7 , 8 , 11 , 9 , 10 , 12 })).cache ();
92
64
65
+ IsotonicRegressionModel model = IsotonicRegression .train (trainRDD , true );
93
66
94
- JavaPairRDD<Double, Double> testRDD = sc.parallelizePairs(Arrays.asList(new Tuple2<Double, Double>(1.0, 1.0)));
95
-
96
- IsotonicRegressionModel model = IsotonicRegression.train(testRDD.rdd(), true);
97
-
98
- List<Tuple3<Double, Double, Double>> expected = IsotonicDataGenerator
67
+ List <Tuple2 <Double , Double >> expected = IsotonicDataGenerator
99
68
.generateIsotonicInputAsList (
100
69
new double [] {1 , 2 , 7d /3 , 7d /3 , 7d /3 , 6 , 7 , 8 , 10 , 10 , 10 , 12 });
101
70
@@ -104,23 +73,23 @@ public void runIsotonicRegressionUsingStaticMethod() {
104
73
105
74
@ Test
106
75
public void testPredictJavaRDD () {
107
- JavaRDD<Tuple3< Double, Double, Double>> testRDD = sc.parallelize(IsotonicDataGenerator
108
- .generateIsotonicInputAsList(
109
- new double[] {1, 2, 3, 3, 1, 6, 7, 8, 11, 9, 10, 12})).cache();
76
+ JavaPairRDD < Double , Double > trainRDD = sc .parallelizePairs (
77
+ IsotonicDataGenerator .generateIsotonicInputAsList (
78
+ new double []{1 , 2 , 3 , 3 , 1 , 6 , 7 , 8 , 11 , 9 , 10 , 12 })).cache ();
110
79
111
- IsotonicRegressionModel model = IsotonicRegression.train(testRDD.rdd() , true);
80
+ IsotonicRegressionModel model = IsotonicRegression .train (trainRDD , true );
112
81
113
- JavaRDD<Vector> vectors = testRDD .map(new Function<Tuple3 <Double, Double, Double >, Vector >() {
82
+ JavaRDD <Double > testRDD = trainRDD .map (new Function <Tuple2 <Double , Double >, Double >() {
114
83
@ Override
115
- public Vector call(Tuple3<Double, Double, Double> v) throws Exception {
116
- return Vectors.dense( v._2() );
84
+ public Double call (Tuple2 < Double , Double > v ) throws Exception {
85
+ return v ._2 ();
117
86
}
118
87
});
119
88
120
- List< Double> predictions = model.predict(vectors ).collect();
89
+ Double [] predictions = model .predict (testRDD ).collect ();
121
90
122
- Assert.assertTrue(predictions.get(0) == 1d);
123
- Assert.assertTrue(predictions.get(11) == 12d);
91
+ Assert .assertTrue (predictions [ 0 ] == 1d );
92
+ Assert .assertTrue (predictions [ 11 ] == 12d );
124
93
}
125
94
}
126
- */
95
+
0 commit comments