我有从python3的pyfpgrowth包中获得的关联规则。
[In] AssociationRules = pyfpgrowth.generate_association_rules(FrequentPattern, 0.8)
[In] AssociationRules
[Out]
{('Longitude 22', 'Magnitude 97'): (('Depth 108', 'Latitude 77'), 1.0),
('Latitude 123',): (('Longitude 5', 'Region Name 20'), 0.8),
('Latitude 147',): (('Longitude 36', 'Region Name 57'), 0.8),
('Longitude 36',): (('Latitude 147', 'Region Name 57'), 0.8),
('Longitude 41', 'Region Name 16'): (('Latitude 149',), 1.0),
('Longitude 23', 'Magnitude 39'): (('Region Name 46',), 1.0),
('Region Name 20',): (('Longitude 5',), 0.8571428571428571),
('Depth 17', 'Longitude 23'): (('Region Name 46',), 1.0),
('Latitude 119', 'Longitude 17'): (('Region Name 37',), 0.8),
('Depth 17', 'Latitude 119', 'Longitude 17'): (('Region Name 37',), 1.0),
('Longitude 4',): (('Region Name 35',), 0.9),
('Longitude 4', 'Region Name 35'): (('Latitude 187',), 1.0),
('Depth 17', 'Longitude 4'): (('Region Name 35',), 1.0),
('Depth 17', 'Longitude 4', 'Region Name 35'): (('Latitude 187',), 1.0)}
根据协会规则,我尝试使用以下代码查找提升值:
def lift_measure(FreqItems, N):
lift = []
cnt = 0
for items, support in FreqItems.items():
if(len(items) > 1):
for i in range(1,len(items)):
lst = list(items)
antecedent = lst[:len(lst) - i]
consequent = lst[-i:]
conf = float(FreqItems[frozenset(items)]/FreqItems[frozenset(antecedent)])
if (conf >= confidence):
hasil = float((FreqItems[frozenset(items)]/N)/
((FreqItems[frozenset(antecedent)]/N)*
(FreqItems[frozenset(consequent)]/N)))
lift.append((antecedent,consequent,support,conf,hasil))
return lift
之后,我运行该函数:
[In] lift_measure(AssociationRules, 0.2)
然后我得到这样的错误:
[Out] frozenset({'Longitude 22', 'Magnitude 97'})
非常感谢!