-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApriori_Algorithm.py
More file actions
42 lines (35 loc) · 1.21 KB
/
Apriori_Algorithm.py
File metadata and controls
42 lines (35 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import pandas as pd
import numpy as np
from itertools import combinations
df = pd.read_csv("Transaction.csv",header=None)
Candidate_set = []
Frequent_set = []
items = pd.unique(df.values.ravel('K'))
items = items[~pd.isnull(items)]
min_support = int(input("Enter Min Support")) #Input from user
for iterno in range(1,len(items)): #Max iterations is equal to length of max set possible i.e. #items in the dataset
Count = {}
intermediate = []
if iterno==1:
Candidate_set.append(items)
for txn in Candidate_set[iterno-1]:
ctr=0
for val in df.values:
if txn in val:
ctr+=1
Count[txn] = ctr
else:
Candidate_set.append(list(combinations(np.unique(np.array(Frequent_set[iterno-2]).ravel('K')),iterno)))
for txn in Candidate_set[iterno-1]:
ctr = 0
for val in df.values:
if all(i in val for i in txn):
ctr+=1
Count[txn] = ctr
for k in Count.keys():
if Count[k] >= min_support:
intermediate.append(k)
if intermediate == []:
print(Frequent_set)
break
Frequent_set.append(intermediate)