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 .mllib .fpm ;
19
+
20
+ import java .io .Serializable ;
21
+ import java .util .ArrayList ;
22
+ import java .util .List ;
23
+
24
+ import org .junit .After ;
25
+ import org .junit .Before ;
26
+ import org .junit .Test ;
27
+ import static org .junit .Assert .*;
28
+
29
+ import com .google .common .collect .Lists ;
30
+
31
+ import org .apache .spark .api .java .JavaRDD ;
32
+ import org .apache .spark .api .java .JavaSparkContext ;
33
+
34
+ public class JavaFPGrowthSuite implements Serializable {
35
+ private transient JavaSparkContext sc ;
36
+
37
+ @ Before
38
+ public void setUp () {
39
+ sc = new JavaSparkContext ("local" , "JavaFPGrowth" );
40
+ }
41
+
42
+ @ After
43
+ public void tearDown () {
44
+ sc .stop ();
45
+ sc = null ;
46
+ }
47
+
48
+ @ Test
49
+ public void runFPGrowth () {
50
+ JavaRDD <ArrayList <String >> rdd = sc .parallelize (Lists .newArrayList (
51
+ Lists .newArrayList ("r z h k p" .split (" " )),
52
+ Lists .newArrayList ("z y x w v u t s" .split (" " )),
53
+ Lists .newArrayList ("s x o n r" .split (" " )),
54
+ Lists .newArrayList ("x z y m t s q e" .split (" " )),
55
+ Lists .newArrayList ("z" .split (" " )),
56
+ Lists .newArrayList ("x z y r q t p" .split (" " ))), 2 );
57
+
58
+ FPGrowth fpg = new FPGrowth ();
59
+
60
+ /*
61
+ FPGrowthModel model6 = fpg
62
+ .setMinSupport(0.9)
63
+ .setNumPartitions(1)
64
+ .run(rdd);
65
+ assert(model6.javaFreqItemsets().count() == 0);
66
+
67
+ FPGrowthModel model3 = fpg
68
+ .setMinSupport(0.5)
69
+ .setNumPartitions(2)
70
+ .run(rdd);
71
+ val freqItemsets3 = model3.freqItemsets.collect().map { case (items, count) =>
72
+ (items.toSet, count)
73
+ }
74
+ val expected = Set(
75
+ (Set("s"), 3L), (Set("z"), 5L), (Set("x"), 4L), (Set("t"), 3L), (Set("y"), 3L),
76
+ (Set("r"), 3L),
77
+ (Set("x", "z"), 3L), (Set("t", "y"), 3L), (Set("t", "x"), 3L), (Set("s", "x"), 3L),
78
+ (Set("y", "x"), 3L), (Set("y", "z"), 3L), (Set("t", "z"), 3L),
79
+ (Set("y", "x", "z"), 3L), (Set("t", "x", "z"), 3L), (Set("t", "y", "z"), 3L),
80
+ (Set("t", "y", "x"), 3L),
81
+ (Set("t", "y", "x", "z"), 3L))
82
+ assert(freqItemsets3.toSet === expected)
83
+
84
+ val model2 = fpg
85
+ .setMinSupport(0.3)
86
+ .setNumPartitions(4)
87
+ .run[String](rdd)
88
+ assert(model2.freqItemsets.count() == 54)
89
+
90
+ val model1 = fpg
91
+ .setMinSupport(0.1)
92
+ .setNumPartitions(8)
93
+ .run[String](rdd)
94
+ assert(model1.freqItemsets.count() == 625) */
95
+ }
96
+ }
0 commit comments