|
a |
|
b/assoc_rules/AssocFPGrowth.java |
|
|
1 |
package assoc_rules; |
|
|
2 |
|
|
|
3 |
import java.io.File; |
|
|
4 |
import java.io.FileWriter; |
|
|
5 |
import java.time.LocalDateTime; |
|
|
6 |
|
|
|
7 |
import weka.associations.Apriori; |
|
|
8 |
import weka.associations.FPGrowth; |
|
|
9 |
import weka.core.Instances; |
|
|
10 |
import weka.core.converters.ConverterUtils.DataSource; |
|
|
11 |
|
|
|
12 |
/** |
|
|
13 |
* This class implements FP growth algorithm to obtain association rule from clean, UMLS mapped and filtered FAERS data |
|
|
14 |
* @author zhengc |
|
|
15 |
* |
|
|
16 |
*/ |
|
|
17 |
public class AssocFPGrowth{ |
|
|
18 |
public static void main(String args[]) throws Exception{ |
|
|
19 |
//load dataset |
|
|
20 |
|
|
|
21 |
String dataset = args[0]; |
|
|
22 |
System.out.println("Algorithm starts: " + LocalDateTime.now()); |
|
|
23 |
System.out.println(); |
|
|
24 |
|
|
|
25 |
DataSource source = new DataSource(dataset); |
|
|
26 |
//get instances object |
|
|
27 |
Instances data = source.getDataSet(); |
|
|
28 |
int numAttr = data.numAttributes(); |
|
|
29 |
int numIns = data.numInstances(); |
|
|
30 |
|
|
|
31 |
System.out.println("Number of attributes is " + numAttr); |
|
|
32 |
System.out.println("Number of instances is " + numIns); |
|
|
33 |
|
|
|
34 |
// lift-based algorithm |
|
|
35 |
String[] opts = new String[10]; |
|
|
36 |
opts[0] = "-T"; opts[1] = "1"; |
|
|
37 |
opts[2] = "-C"; opts[3] = "1"; |
|
|
38 |
opts[4] = "-M"; opts[5] = "0.000002"; |
|
|
39 |
opts[6] = "-N"; opts[7] ="200000"; |
|
|
40 |
opts[8] = "-I"; opts[9] = "3"; |
|
|
41 |
|
|
|
42 |
model.setOptions(opts); |
|
|
43 |
//build model |
|
|
44 |
model.buildAssociations(data); |
|
|
45 |
|
|
|
46 |
//print out and save the extracted rules to file |
|
|
47 |
String results = model.toString(); |
|
|
48 |
System.out.println(results); |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
FileWriter fw = new FileWriter(new File(resultfile)); |
|
|
52 |
fw.write(results); |
|
|
53 |
fw.close(); |
|
|
54 |
|
|
|
55 |
System.out.println("Algorithm ends: " + LocalDateTime.now()); |
|
|
56 |
} |
|
|
57 |
} |