text_plain_output_1.png
from mlxtend.frequent_patterns import apriori, association_rules,fpgrowth,fpmax

from mlxtend.preprocessing import TransactionEncoder

from mlxtend.preprocessing import TransactionEncoder

import pandas as pd

import time

data = pd.read_csv('../input/groceries-dataset/Groceries_dataset.csv')

data.shape

all_transactions = [transaction[1]['itemDescription'].tolist() for transaction in list(data.groupby(['Member_number', 'Date']))]

trans_encoder = TransactionEncoder()
trans_encoder_matrix = trans_encoder.fit(all_transactions).transform(all_transactions)
trans_encoder_matrix = pd.DataFrame(trans_encoder_matrix, columns=trans_encoder.columns_)

def perform_rule_calculation(transact_items_matrix, rule_type, min_support=0.001):
    """
    excution time for the corresponding algorithm
    """
    start_time = 0
    total_execution = 0
    if rule_type == 'fpmax':
        start_time = time.time()
        rule_items = fpmax(transact_items_matrix, min_support=min_support, use_colnames=True)
        total_execution = time.time() - start_time
    if rule_type == 'apriori':
        start_time = time.time()
        rule_items = apriori(transact_items_matrix, min_support=min_support, use_colnames=True)
        total_execution = time.time() - start_time
    if rule_type == 'Fpgrowth':
        start_time = time.time()
        rule_items = fpgrowth(transact_items_matrix, min_support=min_support, use_colnames=True)
        total_execution = time.time() - start_time
    rule_items['number_of_items'] = rule_items['itemsets'].apply(lambda x: len(x))
    return (rule_items, total_execution)

fpgrowth_matrix, fp_growth_exec_time = perform_rule_calculation(trans_encoder_matrix, rule_type='Fpgrowth')
print('Fp Growth execution took: {} seconds'.format(fp_growth_exec_time))