Implementing Apriori in Swift
What will follow is a simplified version of a code that can be found in supplementary materials. We will skip some of the less important parts here.
The main method, which returns association rules with the given support and confidence, is as follows:
public func associationRules(minSupport: Double, minConfidence: Double) -> [Rule] { var rules = [Rule]() let frequent = frequentItemSets(minSupport: minSupport) for itemSet in frequent { for (ifPart, thenPart) in nonOverlappingSubsetPairs(itemSet) { if confidence(ifPart, thenPart) >= minConfidence { let rule = Rule(ifPart: convertIndexesToItems(ifPart), thenPart: convertIndexesToItems(thenPart)) rules.append(rule) } } } return rules } func nonOverlappingSubsetPairs(_ itemSet: ItemSet) -> [(ItemSet, ItemSet)] { var result = [(ItemSet, ItemSet)]() let ifParts = Subsets(itemSet) ...