<jupyter_start><jupyter_text>**Apriori Algorithm using the Groceries Dataset**<jupyter_code>#External package need to install
!pip install apyori
#import all required packages..
import pandas as pd
import numpy as np
from apyori import apriori
df = pd.read_csv('/content/drive/MyDrive/Datasets/groceries.csv', header= None)
df
#replacing empty value with 0.
df.fillna(0,inplace=True)
df.head()
df.info()
#for using aprori need to convert data in list format..
# transaction = [['apple','almonds'],['apple'],['banana','apple']]....
transactions = []
for i in range(0,len(df)):
    transactions.append([str(df.values[i,j]) for j in range(1,32) if str(df.values[i,j])!='0'])
transactions[0]
#Call apriori function which requires minimum support, confidance and lift, min length is combination of item default is 2".
rules = apriori(transactions,min_support=0.003,min_confidance=0.2,min_lift=3,min_length=2)
rules
# # all rules need to be converted in a list..
# Results = list(rules)
# Results

# Store rules in result variable
results = list(rules)

# See the items that were bought together with their support
# results_list = []
# for i in range(0, len(results)):
#     results_list.append('RULE:\t' + str(results[i][0]) + '\nSUPPORT:\t' + str(results[i][1]))
def inspect(results):
    lhs         = [tuple(result[2][0][0])[0] for result in results]
    rhs         = [tuple(result[2][0][1])[0] for result in results]
    supports    = [result[1] for result in results]
    confidences = [result[2][0][2] for result in results]
    lifts       = [result[2][0][3] for result in results]
    return list(zip(lhs, rhs, supports, confidences, lifts))
resultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Left Hand Side', 'Right Hand Side', 'Support', 'Confidence', 'Lift'])

'''Sortted by Support'''

resultsinDataFrame.sort_values('Support', ascending=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14775725593667546, 'fraction_numerical': 0.018469656992084433, 'mean_word_length': 5.196078431372549, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '438540', 'license': 'MIT', 'n_tokens_mistral': 639, 'n_tokens_neox': 627, 'n_words': 183, 'path': 'Associative Rule Learning/Apriori_Algorithm_using_the_Groceries_Dataset.ipynb', 'repo_name': 'ekramasif/Basic-Machine-Learning', 'size': '1895'}