-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelperRules.py
63 lines (52 loc) · 1.96 KB
/
HelperRules.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
import pandas, warnings
warnings.filterwarnings("ignore")
url = "/assets/AprioriFinal.csv"
usecols = ["TransactionID", "ItemCode"]
def OpenTable(url, **cols):
"""
Open Pandas Table
"""
table = pandas.read_table(url, sep=",", **cols)
return table
def TraTable(table, columns):
"""
Perform Transaction Encoder, Table Fit Transform on Encoder Table
"""
transactions = table[columns].str.split(',').apply(lambda x: [item.strip() for item in x]).tolist()
tranencoder = TransactionEncoder()
te_array = tranencoder.fit(transactions).transform(transactions)
encoder_table = pandas.DataFrame(te_array, columns=tranencoder.columns_)
return encoder_table
def Helper(table, itemsets, sorting):
"""
Helper to Maintain Result on Proper Format
"""
for item in itemsets:
table[item] = table[item].apply(lambda val : ", ".join(list(val)))
table = table.round(5)
table = table.sort_values(sorting, ascending=False)
return table
def GetApriori(itemsets, confidence):
"""
Apriori Rules
"""
rules = association_rules(itemsets, metric="confidence", min_threshold=confidence, num_itemsets=1)
rules = rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']]
rules = Helper(rules, ['antecedents', 'consequents'], "confidence")
rules = rules.to_dict(orient="records")
return rules
def HelperGetRules(support, confidence):
"""
Helper Final Rules Output
"""
if not (0.01 <= support <= 0.1):
raise ValueError("Support is Must be Between 0.01 & 0.1")
if not (0.01 <= confidence <= 0.1):
raise ValueError("Confidence is Must be Between 0.01 & 0.1")
encoder_table = TraTable(OpenTable(url, usecols=usecols), "ItemCode")
itemsets = apriori(encoder_table, min_support=support, use_colnames=True)
itemsets = itemsets.reindex(columns=itemsets.columns[::-1])
rules = GetApriori(itemsets, confidence)
return rules