I am trying to incorporate the apriori algorithm in a python program, but I have a TypeError for the line ‘te_ary = te.fit(dataset).transform(dataset)’. I believe it has something to do with the fact that I am reading my dataset from my computer, as opposed to manually typing it into jupyter notebook. I thought it might have dealt with my variables in the line where I declared ‘frequent_itemsets’, but the error is from line 3?
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from apyori import apriori
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
filename = '/Users/emitsch/Documents/Database 1.csv'
#loading the excel spreadsheet file with my database
dataset = pd.read_csv(filename, header = None)
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
And this is the error:
TypeError Traceback (most recent call last)
<ipython-input-19-ff180148a5c5> in <module>
1 te = TransactionEncoder()
----> 2 te_ary = te.fit(dataset).transform(dataset)
3 df = pd.DataFrame(te_ary, columns=te.columns_)
4 frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
//anaconda3/lib/python3.7/site-packages/mlxtend/preprocessing/transactionencoder.py in fit(self, X)
54 unique_items = set()
55 for transaction in X:
---> 56 for item in transaction:
57 unique_items.add(item)
58 self.columns_ = sorted(unique_items)
TypeError: 'int' object is not iterable
Here is a simple example with a tiny transactions dataset (which has 5 items with itemids 1 to 5, and 4 transactions):
TransactionEncoderaccepts a list of list as dataset, so preprocessFinally, fit a
TransactionEncoderon the dataset and runapriorialgorithm to compute frequent itemsets: