Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing rules generated by Apriori

I'm working with some large transactions data. I've been using read.transactions and apriori (parts of the arules package) to mine for frequent item pairings.

My problem is this: when rules are generated (using "inspect()") I can easily view them in the R console. Right now I'm manually copying the results into a text file, then saving and opening in excel. I'd like to just save the generated rules using write.csv, or something similar, but when I try, I receive an error that the data cannot be coerced into data.frame.

Does anyone have experience doing this successfully in R?

like image 392
user2432675 Avatar asked May 29 '13 13:05

user2432675


People also ask

What are rules in Apriori?

The Apriori algorithm calculates rules that express probabilistic relationships between items in frequent itemsets For example, a rule derived from frequent itemsets containing A, B, and C might state that if A and B are included in a transaction, then C is likely to also be included.

How does Apriori algorithm create association rules?

Apriori algorithm uses frequent itemsets to generate association rules. It is based on the concept that a subset of a frequent itemset must also be a frequent itemset. Frequent Itemset is an itemset whose support value is greater than a threshold value(support).

How are association rules generated from frequent Itemsets?

Association Rules find all sets of items (itemsets) that have support greater than the minimum support and then using the large itemsets to generate the desired rules that have confidence greater than the minimum confidence.

Is Apriori algorithm a association rule mining?

Apriori Algorithm is one of the algorithm used for transaction data in Association Rule Learning. It allows us to mine the frequent itemset in order to generate association rule between them.


2 Answers

I know I'm answering my own question, but I found out that the solution is to use as() to convert the rules into a data frame. [I'm new to R, so I missed this my first time searching for a solution.] From there, it can easily be manipulated in any way you'd like (sub setting, sorting, exporting, etc.).

> mba = read.transactions(file="Book2.csv",rm.duplicates=FALSE, format="single", sep=",",cols=c(1,2));

> rules_1 <- apriori(mba,parameter = list(sup = 0.001, conf = 0.01, target="rules"));

> as(rules_1, "data.frame");
like image 181
user2432675 Avatar answered Oct 17 '22 07:10

user2432675


Another way to achieve that would be:

write(rules_1,
      file = "association_rules.csv",
      sep = ",",
      quote = TRUE,
      row.names = FALSE)
like image 31
RDRR Avatar answered Oct 17 '22 05:10

RDRR