Disha Khattri
Disha Khattri

Reputation: 11

Apriori Algorithm - not getting the rules in python

enter image description here

Here is my code and I have given an image of my dataset "Market_Basket_Optimisation". I have made list of lists transaction to give the input in apriori algorithm.But I am not getting the rules. I am new to machine learning and I am not able to find out the error.

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Data Preprocessing
dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)
transactions = []
for i in range(0, 7501):
    transactions.append([str(dataset.values[i,j]) for j in range(0, 20)])

# Training Apriori on the dataset
from apyori import apriori
rules = apriori(transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2)

# Visualising the results
results = list(rules)

Upvotes: 0

Views: 885

Answers (2)

Raad Shahamat
Raad Shahamat

Reputation: 1

I also faced the same problem. but then i got that this was happening because of the 'nan' values in the transactions list. so i changed the code as below and got the expected result you can modify the code as below:

dataset=pd.read_csv("Market_Basket_Optimisation.csv",header=None)
transactions=[]
for i in range(0,7501):
    transactions.append([str(dataset.values[i,j]) for j in range(0,20) if str(dataset.values[i,j]) !='nan'])
print(transactions)

Upvotes: 0

user2597586
user2597586

Reputation: 21

It is not clear from your question if you are using jupyter notebook or an IDE such as Spyder. If you are using an IDE such as Spyder, you are not likely to see the result unless you use a print statement. I suggest adding another line as follows: print(resuult) You should see the rules list. This is the same issue I had and using the print statement solved the problem for me. You will still need to define a function to output the result in a tabular format that makes sense.

Upvotes: 0

Related Questions