-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAprioriImpl.java
More file actions
315 lines (290 loc) · 8.95 KB
/
AprioriImpl.java
File metadata and controls
315 lines (290 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.datamining.algo;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;
/**
* Changes : Added candidate pruning based on support count
* Class for Apriori algorithm implementation and data set analysis
* @author sandeep
*/
public class AprioriImpl {
public static double MINSUP = 0.05;
public static String FILEPATH = "/home/sandeep/DM/data-2016.csv";
public static int TOTAL_TRANS;
/**
* Utility method to read File
* @param filePath :Specify absolute file path
* @return Stream<String>
*/
public static Stream<String> readFile(String filePath) {
Stream<String> lines = null;
try {
lines = Files.lines(Paths.get(filePath));
} catch (IOException e) {
System.err.println("File not found at" + filePath);
}
return lines;
}
/**
* Utility method for course ID extraction
* @param lines
* @return {@link ArrayList<Integer>}
*/
public static ArrayList<Integer> getCourseIdList(Stream<String> lines) {
ArrayList<Integer> courses = new ArrayList();
lines.forEach(line -> {
String[] columns = line.split(" ");
for (int i = 2; i < columns.length; i += 5) {
int cid = Integer.parseInt(columns[i]);
if (!courses.contains(cid)) {
courses.add(cid);
}
}
});
TOTAL_TRANS = courses.size();
return courses;
}
/**
* Generate One frequent item set Sort in order
* @param courseIdList
* @param transactionsMap
* @return ArrayList<ArrayList<Integer>>
*/
public static ArrayList<ArrayList<Integer>> getOneItemset(ArrayList<Integer> courseIdList,
HashMap<Integer, ArrayList<ArrayList<Integer>>> transactionsMap) {
ArrayList<ArrayList<Integer>> oneItemsets = new ArrayList();
for (int id : courseIdList) {
ArrayList<Integer> item = new ArrayList();
item.add(id);
if (calculateSupport(item, transactionsMap) >= MINSUP) {
oneItemsets.add(item);
}
}
Comparator<ArrayList<Integer>> comparator = (ArrayList<Integer> o1, ArrayList<Integer> o2) -> {
for (int i = 0; i < o1.size(); i++) {
if (o1.get(i) > o2.get(i)) {
return 1;
} else if (o2.get(i) > o1.get(i)) {
return -1;
}
}
return 0;
};
Collections.sort(oneItemsets, comparator);
return oneItemsets;
}
/**
* This method creates transactions from the given data set
* @param lines {@link Stream<String>}
* @return {@link ArrayList<ArrayList<Integer>>}
*/
public static ArrayList<ArrayList<Integer>> createTransactions(Stream<String> lines) {
Stream<Student> studentStream = lines.map(Student::createStudent);
ArrayList<ArrayList<Integer>> transactions = new ArrayList();
studentStream.forEach(s -> {
ArrayList<Integer> courseCodes = new ArrayList();
for (CourseTranscript ct : s.courseTranscriptList) {
courseCodes.add(ct.courseCode);
}
transactions.add(courseCodes);
});
return transactions;
}
/**
* Initialize the support for all item sets in the transaction
*
* @param transactions
* @param courses
* @return
*/
public static HashMap<Integer, ArrayList<ArrayList<Integer>>> initSupportForTransaction(
ArrayList<ArrayList<Integer>> transactions, ArrayList<Integer> courses) {
HashMap<Integer, ArrayList<ArrayList<Integer>>> map = new HashMap();
for (int id : courses) {
ArrayList<ArrayList<Integer>> idTransactions = new ArrayList();
for (ArrayList<Integer> t : transactions) {
if (t.contains(id)) {
idTransactions.add(t);
}
}
map.put(id, idTransactions);
}
return map;
}
/**
* This method calculated Support for the given candidate and transactions
*
* @param candidate
* @param transactions
* @return {@link Double}
*/
public static double calculateSupport(ArrayList<Integer> candidate,
HashMap<Integer, ArrayList<ArrayList<Integer>>> transactions) {
if (candidate.size() == 1) {
ArrayList<ArrayList<Integer>> candidateItem = transactions.get(candidate.get(0));
return (double) candidateItem.size()/TOTAL_TRANS;
}
ArrayList<ArrayList<Integer>> firstCandidate = (ArrayList<ArrayList<Integer>>) transactions
.get(candidate.get(0)).clone();
for (int i = 1; i < candidate.size(); i++) {
ArrayList<ArrayList<Integer>> candidiateEach = transactions.get(candidate.get(i));
firstCandidate.retainAll(candidiateEach);
if (firstCandidate.size() <= 0) {
return 0.0;
}
}
return (double) firstCandidate.size()/TOTAL_TRANS;
}
/**
* Find frequent Item sets
*
* @param itemsets
* @param transactions
*/
public static void calculateFrequentItemsets(ArrayList<ArrayList<Integer>> itemsets,
HashMap<Integer, ArrayList<ArrayList<Integer>>> transactions) {
if (itemsets.size() < 1) {
return;
}
int candidateCount = Integer.MAX_VALUE;
while (candidateCount > 0) {
int itemsetSize = itemsets.get(0).size() + 1;
ArrayList<ArrayList<Integer>> candidates = generate(itemsets);
ArrayList<ArrayList<Integer>> newCandidates = new ArrayList();
for (ArrayList<Integer> candidate : candidates) {
if (!checkPruneStatus(candidate, itemsets)) {
double support = calculateSupport(candidate, transactions);
if (support >= MINSUP) {
newCandidates.add(candidate);
}
}
}
candidateCount = newCandidates.size();
itemsets = newCandidates;
}
}
/**
* Check candidate is pruned or not
*
* @param candidate
* @param itemsets
* @return
*/
public static boolean checkPruneStatus(ArrayList<Integer> candidate, ArrayList<ArrayList<Integer>> itemsets) {
Comparator<ArrayList<Integer>> comparator = (ArrayList<Integer> o1, ArrayList<Integer> o2) -> {
for (int i = 0; i < o1.size(); i++) {
if (o1.get(i) > o2.get(i)) {
return 1;
} else if (o2.get(i) > o1.get(i)) {
return -1;
}
}
return 0;
};
for (int i = 0; i < candidate.size(); i++) {
ArrayList<Integer> itemsubSet = new ArrayList();
for (int j = 0; j < candidate.size(); j++) {
if (i != j) {
itemsubSet.add(candidate.get(j));
}
}
int index = Collections.binarySearch(itemsets, itemsubSet, comparator);
if (index < 0) {
return true;
}
}
return false;
}
/**
* Merges given two item sets
*
* @param list1
* @param list2
* @return
*/
public static ArrayList<Integer> merge(ArrayList<Integer> list1, ArrayList<Integer> list2) {
Set<Integer> union = new TreeSet<Integer>();
union.addAll(list1);
union.addAll(list2);
return new ArrayList<Integer>(union);
}
/**
* Generate all candidates
*
* @param itemSet
* @return
*/
public static ArrayList<ArrayList<Integer>> generate(ArrayList<ArrayList<Integer>> itemSet) {
ArrayList<ArrayList<Integer>> response = new ArrayList();
if (itemSet.size() <= 1) {
return response;
}
int prev = 0;
while (prev < itemSet.size() - 1) {
ArrayList<Integer> prevList = itemSet.get(prev);
int cur = prev + 1;
while (cur < itemSet.size()) {
ArrayList<Integer> curList = itemSet.get(cur);
if (prevList.size() == 1 || curList.size() == 1) {
response.add(merge(prevList, curList));
} else {
if (prevList.subList(0, prevList.size() - 1).equals(curList.subList(0, curList.size() - 1))) {
response.add(merge(prevList, curList));
}
}
cur++;
}
prev++;
}
return response;
}
public static class Student {
int startingYear;
ArrayList<CourseTranscript> courseTranscriptList;
static Student createStudent(String line) {
String[] columns = line.split(" ");
Student s = new Student();
s.startingYear = Integer.parseInt(columns[0]);
s.courseTranscriptList = CourseTranscript.createCourseTranscriptList(line);
return s;
}
}
public static class CourseTranscript {
int startingMonth;
String courseName;
int courseCode;
int grade;
double creditPoint;
static ArrayList<CourseTranscript> createCourseTranscriptList(String line) {
String[] columns = line.split(" ");
ArrayList<CourseTranscript> courseTranscriptList = new ArrayList<>();
for (int i = 1; i + 4 < columns.length; i += 5) {
CourseTranscript c = new CourseTranscript();
c.startingMonth = Integer.parseInt(columns[i].replace("-", ""));
c.courseCode = Integer.parseInt(columns[i + 1]);
c.courseName = columns[i + 2];
c.creditPoint = Double.parseDouble(columns[i + 3]);
c.grade = Integer.parseInt(columns[i + 4]);
courseTranscriptList.add(c);
}
return courseTranscriptList;
}
}
public static void main(String[] args) {
Stream<String> lines = readFile(FILEPATH);
ArrayList<Integer> courseIdList = getCourseIdList(lines);
lines = readFile(FILEPATH);
ArrayList<ArrayList<Integer>> transactions = createTransactions(lines);
HashMap<Integer, ArrayList<ArrayList<Integer>>> transactionSupportTrackMap = initSupportForTransaction(
transactions, courseIdList);
ArrayList<ArrayList<Integer>> oneItemsets = getOneItemset(courseIdList, transactionSupportTrackMap);
calculateFrequentItemsets(oneItemsets, transactionSupportTrackMap);
}
}