this works correctlybut is very slow better implementation has more infrastructure so you can avoid having to loop over all the datasets to get the count (c_newsetin particularwe keep track of which shopping baskets have which frequent itemsets this accelerates the loop but makes the code harder to follow thereforewe will not show it here as usualyou can find both implementations on the book' companion website the code there is also wrapped into function that can be applied to other datasets the apriori algorithm returns frequent itemsetsthat issmall baskets that are not in any specific quantity (minsupport in the codeassociation rule mining frequent itemsets are not very useful by themselves the next step is to build association rules because of this final goalthe whole field of basket analysis is sometimes called association rule mining an association rule is statement of the "if then yformfor exampleif customer bought war and peacethey will buy anna karenina note that the rule is not deterministic (not all customers who buy will buy )but it is rather cumbersome to always spell it out so if customer bought xhe is more likely to buy according to the baselinethuswe say if then ybut we mean it in probabilistic sense interestinglythe antecedent and conclusion may contain multiple objectscostumers who bought xyand also bought aband multiple antecedents may allow you to make more specific predictions than are possible from single item you can get from frequent set to rule by just trying all possible combinations of implies it is easy to generate many of these rules howeveryou only want to have valuable rules thereforewe need to measure the value of rule commonly used measure is called the lift the lift is the ratio between the probability obtained by applying the rule and the baselinein the preceding formulap(yis the fraction of all transactions that include while ( |xis the fraction of transactions that include and both using the lift helps you avoid the problem of recommending bestsellersfor bestsellerboth (yand ( |ywill be large thereforethe lift will be close to one and the rule will be deemed not very relevant in practicewe wish to have at least perhaps even values of lift