Finding frequent itemsets with Apriori Algorithm?

Multi tool use
Finding frequent itemsets with Apriori Algorithm?
I am trying to code the apriori algorithm in Java but am facing a problem regarding frequent itemsets.
My mininum support is 1% -> each of the itemsets must be in one or more transactions.
In order to understand the algorithm, ive started with cardinality 1.
Now the more transactions I compute the less correct my implementation gets though it works with less than 4 transactions.
private static void checkForFrequentItemSet() {
ArrayList<String> transactions = collectTransactions();
ArrayList<String> items = createItemList(); // First Iteration actually, C1
int k = 1;
// while(!items.isEmpty())
// {
items = generateCandidates(items, k);
System.out.println("There are exactly " + items.size() + " frequent itemsets containing " + k + " item(s):");
System.out.println(items);// Remember this is c1 at first iteration
k++;
ArrayList<String> finalItems = new ArrayList<String>();
for (String t : items) // For every candidate
{
int threshold = 0;
int counter = 0;
String singleContents = t.split(" "); // get single candidates
List<String> singleItem = Arrays.asList(singleContents);
System.out.println(singleItem);
for (String i : transactions)
{
String singleTransa = i.split(" ");// and single transaction elements
List<String> singleTransaList = Arrays.asList(singleTransa);
for (String c : singleTransaList)
{
if (singleItem.contains(c))
{
counter++;
}
}
}
if (counter == singleItem.size()) // If Transaction i contains all elements of of one itemset
{threshold++; System.out.println(t+":"+counter);}
if (threshold >= min_sup) // iff the treshold is greater than a min sup
{finalItems.add(t); System.out.println(t);}
else
{
System.out.println("Not found: "+t);
}
}
items = finalItems;
System.out.println(items.size());
// }
}
I use this method to check if the transaction contains the candidate and if so, to add the candidate to the list.
Since it does work with 3 or less transactions, I think the code is okay-ish.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.