How to make association rules for grocery items using apriori algorithm in python?

Description

To implement apriori algorithm for grocery items in python.

Input

Grocery data.

Output

 Association between the items.

  Import the library.

  Load the data set.

  Build the model.

  Fit the grocery data.

  Make the association rules.

  Print the rules.

#import the library
import numpy as np
import pandas as pd
from apyori import apriori

#load the data
store_data = pd.read_csv(‘……/grocery.csv’)

#initialize a empty list
records = []
for i in range(0, 19):
records.append([str(store_data.values[i,j]) for j in range(0, 4)])

#applying apirori
association_rules = apriori(records, min_support=0.03, min_confidence=0.2, min_lift=3, min_length=2)

#make it as a data frame
df = pd.DataFrame(association_rules)

#create a excel file for results
final = df.to_excel(‘…/asso_rules.xlsx’)

for i in range(0,16):
print(df.loc[i])

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit