PDA

View Full Version : Apriori in Java application



fedetallo
06-10-2008, 10:11 AM
Hello,
I'm using Weka inside a Java Application, calling algorithm methods from java code.
I use the "Apriori" class, is it possible to get, after the "buildAssociations", the rules found? I can't see a method which do that I need.

Thanks

Mark
06-10-2008, 06:11 PM
Yes. If you are using the development version of Weka (3.5.x), you can call the method getAllTheRules(). This method returns a three (or up to six if the measure selected is not confidence) element array of FastVectors. The first FastVector contains all the antecedent large itemsets (i.e. left-hand sides of the rules) and the second FastVector contains all the consequent large itemsets (i.e. right-hand sides of the rules). The third FastVector contains the confidence scores (wrapped up in Double classes). The remaining three FastVectors (if measure other than confidence is selected) contain lift, leverage and conviction scores (wrapped in Doubles) respectively. The elements of the FastVectors are ordered in terms of support and metric in question.

HTH.

Cheers,
Mark.

fedetallo
06-11-2008, 05:31 AM
thank you very much, I was using the last stable version (3.4), I will upgrade to the development version!

fedetallo
06-11-2008, 10:57 AM
But, how can I get the content of the AprioriItemset object?

FastVector[] rules = apriori.getAllTheRules();
AprioriItemset antecedents = (AprioriItemset)rules[0].getElementAt(0);

here is the itemset of the antecedent of the first rule. Now, how can I get the text describing the antecedent?

Thanks

Mark
06-11-2008, 05:23 PM
Just call the toString() method. In fact, if all you were after was the textual output of Apriori, then you can just call toString() on Apriori :-) I was assuming that you wanted to do something else with the item sets.

Cheers,
Mark.

msdyinoz
07-01-2008, 12:31 AM
Just call the toString() method. In fact, if all you were after was the textual output of Apriori, then you can just call toString() on Apriori :-) I was assuming that you wanted to do something else with the item sets.

Cheers,
Mark.

I am calling weka.associations.Apriori from Matlab. I got an error when I call the toString(Instance) method to get the contents of an item set:
Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: 6

at weka.associations.ItemSet.toString(Unknown Source)

Any idea?

Thank you.

Mark
07-01-2008, 04:49 AM
Are you passing in an Instances object to the toString method that has the same structure as that was used to generate the item sets? From a quick look at the toString method in ItemSet, the only reason I can see for an ArrayIndexOutOfBounds exception is if the Instances passed in has a different structure to the training set (i.e different number of attributes or different number of values for a given attribute).

Cheers,
Mark.

msdyinoz
07-01-2008, 05:12 AM
Yes, I passed the same nstances object to the toString method but still got this error. I tested the support method which gave the correct result, but don't know why toString doesn't work.