Mining the Top-K Class Association Rules (rules with a fixed consequent) (SPMF documentation)

This example explains how to run the TopKClassRules algorithm using the SPMF open-source data mining library.

How to run this example?

What is TopKClassRules?

TopKClassRules is an algorithm for discovering the top-k class association rules appearing in a transaction database. It is a variation of the TopKRules algorithm (Fournier-Viger, 2012), which has been adapted to find association rules with a consequent containing a single item chosen from a set of items allowed by the user. This algorithm is more simple than TopKRules as it is designed to solve a special case of the top-k association rule mining problem. However, the output is very useful for some applications as the user may be interested in only finding rules with some specific items in the consequent.

Why is it useful to discover top-k class association rules? Because other association rule mining algorithms requires to set a minimum support (minsup) parameter that is hard to set (usually users set it by trial and error, which is time consuming). TopKRules solves this problem by letting users directly indicate k, the number of rules to be discovered instead of using minsup. The output is the k most frequent class association rules in a database.

What is the input of TopKRules ?

TopKClass Rules takes four parameters as input:

A transaction database is a set of transactions. Each transaction is a set of items. For example, consider the following transaction database. It contains 6 transactions (t1, t2, ..., t5, t6) and 5 items (1,2, 3, 4, 5). For example, the first transaction represents the set of items 1, 2, 4 and 5. This database is provided as the file contextIGB.txt in the SPMF distribution. It is important to note that an item is not allowed to appear twice in the same transaction and that items are assumed to be sorted by lexicographical order in a transaction.

Transaction id Items
t1 {1, 2, 4, 5}
t2 {2, 3, 5}
t3 {1, 2, 4, 5}
t4 {1, 2, 3, 5}
t5 {1, 2, 3, 4, 5}
t6 {2, 3, 4}

What is the output of TopKClassRules ?

TopKClassRules outputs the top-k class association rules.

To explain what are top-k class association rules, it is necessary to review some definitions. An itemset is a set of distinct items. The support of an itemset is the number of times that it appears in the database divided by the total number of transactions in the database. For example, the itemset {1 3} has a support of 33 % because it appears in 2 out of 6 transactions from the database.

An association rule X--> Y is an association between two itemsets X and Y that are disjoint. The support of an association rule is the number of transactions that contains X and Y divided by the total number of transactions. The confidence of an association rule is the number of transactions that contains X and Y divided by the number of transactions that contains X. A class association rule X --> Y is a special type of association rule where the itemset Y contains a single item that must be chosen from a set allowedItems specified by the user.

The top-k class association rules are the k most frequent class association rules in the database having a confidence higher or equal to minconf.

For example, if we run TopKClassRules with k =7 , minconf = 0.8, and allowedItems = 1,2 , we obtain the top-7 rules in the database having a confidence higher or equals to 80 %.

For instance, the rule 1 ==>2 means that if item1 appears, it is likely to be associated with item 2 with a confidence of 100% in a transaction. Moreover, this rule has a support of 4 because it appears in five transactions (t1, t3, t4, t5) out of the six transactions contained in this database.

It is important to note that for some values of k, the algorithm may return slightly more than k rules. This can happen if several rules have exactly the same support.

Input file format

The input file format is a text file containing transactions. Each lines represents a transaction. The items in the transaction are listed on the corresponding line. An item is represented by a positive integer. Each item is separated from the following item by a space. It is assumed that items are sorted according to a total order and that no item can appear twice in the same transaction. For example, for the previous example, the input file is defined as follows:

1 2 4 5
2 3 5
1 2 4 5
1 2 3 5
1 2 3 4 5
2 3 4

Consider the first line. It means that the first transaction is the itemset {1, 2, 4 and 5}. The following lines follow the same format.

Note that it is also possible to use the ARFF format as an alternative to the default input format. The specification of the ARFF format can be found here. Most features of the ARFF format are supported except that (1) the character "=" is forbidden and (2) escape characters are not considered. Note that when the ARFF format is used, the performance of the data mining algorithms will be slightly less than if the native SPMF file format is used because a conversion of the input file will be automatically performed before launching the algorithm and the result will also have to be converted. This cost however should be small.

Output file format

The output file format is defined as follows. It is a text file, where each line represents a class association rule. On each line, the items of the rule antecedent are first listed. Each item is represented by a positive integer, followed by a single space. After, that the keyword "==>" appears followed by a space. Then, the items of the rule consequent are listed. Each item is represented by an integer, followed by a single space. Then, the keyword " #SUP: " appears followed by the support of the rule represented by an integer (a number of transactions). Then, the keyword " #CONF: " appears followed by the confidence of the rule represented by a double value (a value between 0 and 1, inclusively). For example, here is a few lines from the output file if we run TopKClassRules on contextIGB.txt with k=2 and minconf=0.8 (80 %):

1 ==> 2 #SUP: 4 #CONF: 1.0
5 ==> 1 #SUP: 4 #CONF: 0.8
3 ==> 2 #SUP: 4 #CONF: 1.0
4 ==> 2 #SUP: 4 #CONF: 1.0
2 5 ==> 1 #SUP: 4 #CONF: 0.8
1 5 ==> 2 #SUP: 4 #CONF: 1.0
5 ==> 2 #SUP: 5 #CONF: 1.0

For example, the first line indicates that the association rule {1} --> {2} has a support of 4 transactions and a confidence of 100 %. The other lines follow the same format.

Note that if the ARFF format is used as input instead of the default input format, the output format will be the same except that items will be represented by strings instead of integers.

Optional feature: giving names to items

Some users have requested the feature of given names to items instead of using numbers. This feature is offered in the user interface of SPMF and in the command line of SPMF. To use this feature, your file must include @CONVERTED_FROM_TEXT as first line and then several lines to define the names of items in your file. For example, consider the example database "contextIGB.txt". Here we have modified the file to give names to the items: 

@CONVERTED_FROM_TEXT
@ITEM=1=apple
@ITEM=2=orange
@ITEM=3=tomato
@ITEM=4=milk
@ITEM=5=bread
1 2 4 5
2 3 5
1 2 4 5
1 2 3 5
1 2 3 4 5
2 3 4

In this file, the first line indicates, that it is a file where names are given to items. Then, the second line indicates that the item 1 is called "apple". The third line indicates that the item 2 is called "orange". Then the following lines define transactions in the SPMF format.

Then, if we apply the algorithm using this file using the user interface of SPMF or the command line, the output file contains several patterns, including the following ones:

bread ==> apple #SUP: 4 #CONF: 0.8
orange bread ==> apple #SUP: 4 #CONF: 0.8
apple bread ==> orange #SUP: 4 #CONF: 1.0

Note that this feature could be also used from the source code of SPMF using the ResultConverter class. However, there is currently no example provided for using it from the source code.

Performance

TopKClassRules is a special case of the TopKRules algorithm, a very efficient algorithm for mining the top-k association rules.

TopKRules/TopKClassrules provides the benefits that they are very intuitive to use.

Besides, note that there is a variation of TopKRules named TNR that is available in SPMF. The improvement in TNR is that it eliminates some association rules that are deemed "redundant" (rules that are included in other rules having the same support and confidence - see the TNR example for the formal definition). Using TNR is more costly than using TopKRules but it brings the benefit of eliminating a type of redundancy in results. But TNR is not designed to find class association rules.

Where can I get more information about this algorithm?

TopKClassRules is a simple variation of the the TopKRules algorithm, which was proposed in this paper:

Fournier-Viger, P., Wu, C.-W., Tseng, V. S. (2012). Mining Top-K Association Rules. Proceedings of the 25th Canadian Conf. on Artificial Intelligence (AI 2012), Springer, LNAI 7310, pp. 61-73.

For a good overview of itemset mining and association rule mining, you may read this survey paper.