Commit abd3a5f5 authored by Linus Jahn's avatar Linus Jahn 🍙

apriori: add more comments (very important)

parent be5415d5
......@@ -33,16 +33,29 @@ transactions = [
["Saft", "Wasser"]
]
def containsItems(items, transaction):
if type(items) == tuple:
for item in items:
## Checks whether an item or a combination of items is contained in a
## transaction
#
# @param combination A single item or a tuple of items
# @param transaction The transation (list) of items to check against
# @returns True in case all items are included in the transaction
def containsItems(combination, transaction):
if type(combination) == tuple:
for item in combination:
if not item in transaction:
return False
else:
if not items in transaction:
if not combination in transaction:
return False
return True
## Returns the frequency of one combination
#
# @param combination The item or tuple of items to get the frequency of
# @param transactions The list of transactions
# @returns frequency as float (0.0 <-> 1.0)
def combinationFrequency(combination, transactions):
count = 0
for transaction in transactions:
......@@ -51,6 +64,7 @@ def combinationFrequency(combination, transactions):
return count / len(transactions)
def apriori(transactions, threshold = 0.5):
# get initial candidates
candidates = []
for transaction in transactions:
for t in transaction:
......@@ -58,22 +72,26 @@ def apriori(transactions, threshold = 0.5):
candidates.append(t)
loesungsmenge = []
# loop until there are no new candidates left
len_candidates = len(candidates)
while len_candidates > 0:
newCandidates = []
acceptedCandidates = []
for combination in candidates:
# check threshold of combination
if combinationFrequency(combination, transactions) >= threshold:
loesungsmenge.append(combination)
newCandidates.append(combination)
acceptedCandidates.append(combination)
candidates = []
for x in itertools.combinations(newCandidates, 2):
for x in itertools.combinations(acceptedCandidates, 2):
if type(x[0]) == tuple:
x = x[0] + x[1]
x = tuple(set(x))
candidates.append(x)
candidates = tuple(set(candidates))
len_candidates = len(candidates)
return loesungsmenge
# only return actual combinations (no single items)
return [x for x in loesungsmenge if type(x) == tuple]
print(apriori(transactions))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment