You must have noticed that while online shopping, a set of items related to the item you are viewing or ordering are shown with tags like “Frequently bought together”. So how do they figure out which items are bought together? Association rules attempt to answer these kinds of questions.
In basic terms, association rules present relations between items. They are statements that help to discover relationships between data in a database. An association rule can be defined as an implication of the form A → B. Here ‘A’ is called premise, which represents a condition that must be true for ‘B’ to hold. ‘B’ is a conclusion that happens when ‘A’ is true. ‘A’ is called antecedent and ‘B’ is called consequent. An antecedent is an element found in data whereas a consequent is found in combination with the antecedent. The rule A → B can be interpreted as: If A happens, B happens. This is a very general interpretation; the accurate interpretation depends on the domain.
Let us try to understand this concept better with the help of a few examples. Suppose you are the owner of a stationary shop. Now you get a rule: Book → Pen. This can be interpreted as: If a customer bought a book, he will also buy a pen. Let us look at another simple example. Suppose you are analysing an online streaming website of TV series and see a rule Friends → How I met your mother. It means that people who have watched ‘Friends’ have also watched ‘How I met your mother’.
Now let us look at how association rules help. The interpretations of association rules can help to improve or build a system. Taking the stationary shop example again. We know that a customer is likely to buy a pen if he buys a book, so we move books and pens together so that the sale improves. Similarly, taking the example of streaming website, if we keep the titles ‘Friends’ and ‘How I met your mother’ together, the viewership will increase.
But when you are using the association rules to take an action, you have to be careful about the situation. For example, suppose you are the owner of a supermarket shop and you find the rule: Beer → Diapers. This a very unexpected association and as the owner, you should not move these products together on the shelves.
For applying association rules, we need data to be in the form of transactions. Transaction means a logical group of items in this context. These transactions are unrelated to database transactions. The transaction can be a group of grocery items, a list of movies, etc.
Association rules in Data Science
In data mining, the interpretation of association rules simply depends on what you are mining. Let us have an example to understand how association rule help in data mining. We will use the typical market basket analysis example. In this example, a transaction would mean the contents of a basket. Each transaction, therefore, gives us the contents of a basket of a customer. The table below is the data.
We have data of four customers and the contents of their market basket. Looking at the data, we notice that transactions 1, 2 and 4 contain bread and milk. This can be converted as out first association rule, that is, Bread → Milk. In a similar way of thinking, we can find out that milk and butter are present in transactions 1, 2, and 3. This leads to another association rule, Milk → Butter.
However, there is a drawback. We have not figured out one thing – which rule is better? It is impossible to compare them. To overcome this problem, we can use several classifiers. These classifiers will show the strength of the rule. They are also called as ‘interestingness measures’, because the strength of the rule is comparable with its interestingness.
In case of association rules, there are two classical measures – support and confidence. Support gives a measure of how often did the rule apply. Confidence is a percentage of all transaction, which contain items on the left and on the right side of the rule.
Let us look at an example to have a better understanding of support and confidence. We will consider the classical market basket case again.
Let us try to analyse the data in the table above. Bread and mayo are both in the baskets of transactions 1, 2 and 6. The total number of transactions is six. So, the support for the rules ‘Bread→Mayo’ and ‘Mayo→Bread’ will be 3/6 = 50%. The support is calculated as the number of transactions where both items are present; that is, 3 divided by the total number of transactions which is 6. For calculating confidence, we have to look at the number of transactions where mayo and bread were present in the basket.
Now consider only ‘Bread → Mayo’ rule. Bread is present in baskets 1, 2, 3, 4 and 6. So 5 baskets. Out of these, mayo is present in baskets 1, 2 and 6. So the confidence of the rule ‘Bread → Mayo’ is 3/5 = 60%.
So, we can now calculate the support and confidence of association rules. These can be easily used to filter out uninteresting rules by setting thresholds, such as minimum support should be 50% and minimum confidence should be 75% to consider the rule as interesting. Setting the values of these measures will determine the number of rules that will be interesting. So, if the values are too low, there might be too many rules classified as interesting and vice-versa. So appropriate values should be assigned.
In conclusion, we can say that there are various control parameters in data mining using association rules which can be used to get the desired output depending on the case. The most important question is what you are mining. Apart from support and confidence, many other interestingness measures are there for data mining using association rules that can be used and that may work better in specific cases. Data mining using association rules has applications in web usage mining, market basket analysis, bioinformatics, healthcare, continuous flow process, etc. and therefore is an interesting emerging concept that can help improve efficiency.