(self)
return self.response_df
Use an apriori algorithm to uncover association rules between items.
Use an apriori algorithm to uncover association rules between items.
Use an apriori algorithm to uncover association rules between items .
def association_rules(self):
        """
        Use an apriori algorithm to uncover association rules between items.
        """

        # Interpret the request data based on the expected row and column structure
        self._initiate(row_template = ['strData', 'strData', 'strData'],  col_headers = ['group', 'item', 'kwargs'])

        # Create a list of items for each group
        transactions = []

        # Iterate over each group and add a tuple of items to the list
        for group in self.request_df['group'].unique():
            transactions.append(tuple(self.request_df.item[self.request_df.group == group]))

        # Get the item sets and association rules from the apriori algorithm
        _, rules = apriori(transactions, **self.pass_on_kwargs)

        # Prepare the response
        response = []

        # for each rule get the left hand side and right hand side together with support, confidence and lift 
        for rule in sorted(rules, key=lambda rule: rule.lift, reverse=True):
            lhs = ", ".join(map(str, rule.lhs))
            rhs = ", ".join(map(str, rule.rhs))
            desc = "{0} -> {1}".format(lhs, rhs)
            response.append((desc, lhs, rhs, rule.support, rule.confidence, rule.lift))

        # if no association rules were found the parameters may need to be adjusted
        if len(response) == 0:
            err = "No association rules could be found. You may get results by lowering the limits imposed by " + \
                "the min_support and min_confidence parameters.\ne.g. by passing min_support=0.2|float in the arguments."
            raise Exception(err) 

        self.response_df = pd.DataFrame(response, columns=['rule', 'rule_lhs', 'rule_rhs', 'support', 'confidence', 'lift'])

        # Print the response dataframe to the logs
        if self.debug:
            self._print_log(4)

        # Send the reponse table description to Qlik
        self._send_table_description("apriori")
        
        return self.response_df
def association_rules ( self ) : # Interpret the request data based on the expected row and column structure self . _initiate ( row_template = [ 'strData' , 'strData' , 'strData' ] , col_headers = [ 'group' , 'item' , 'kwargs' ] ) # Create a list of items for each group transactions = [ ] # Iterate over each group and add a tuple of items to the list for group in self . request_df [ 'group' ] . unique ( ) : transactions . append ( tuple ( self . request_df . item [ self . request_df . group == group ] ) ) # Get the item sets and association rules from the apriori algorithm _ , rules = apriori ( transactions , * * self . pass_on_kwargs ) # Prepare the response response = [ ] # for each rule get the left hand side and right hand side together with support, confidence and lift  for rule in sorted ( rules , key = lambda rule : rule . lift , reverse = True ) : lhs = ", " . join ( map ( str , rule . lhs ) ) rhs = ", " . join ( map ( str , rule . rhs ) ) desc = "{0} -> {1}" . format ( lhs , rhs ) response . append ( ( desc , lhs , rhs , rule . support , rule . confidence , rule . lift ) ) # if no association rules were found the parameters may need to be adjusted if len ( response ) == 0 : err = "No association rules could be found. You may get results by lowering the limits imposed by " + "the min_support and min_confidence parameters.\ne.g. by passing min_support=0.2|float in the arguments." raise Exception ( err ) self . response_df = pd . DataFrame ( response , columns = [ 'rule' , 'rule_lhs' , 'rule_rhs' , 'support' , 'confidence' , 'lift' ] ) # Print the response dataframe to the logs if self . debug : self . _print_log ( 4 ) # Send the reponse table description to Qlik self . _send_table_description ( "apriori" ) return self . response_df