Reputation: 57
So I've been using Apyori to get the minimum support from a dataset of Titanic survivors.
rules = apriori(titanic, min_support = 0.1, min_confidence = 1.0)
print(list(rules))
Here's an example of one of the elements I'd get as output
[RelationRecord(items=frozenset({'Crew', 'Adult'}),
support=0.4020899591094957, ordered_statistics=
[OrderedStatistic(items_base=frozenset({'Crew'}),
items_add=frozenset({'Adult'}), confidence=1.0, lift=1.0521032504780115)])
However, I'm trying to sort by lift, and I'm not sure how to approach this problem, since the lift element seems to be inside a tuple, but I'm not sure exactly on how I'd go about sorting it.
Any help is greatly appreciated.
Thanks.
Upvotes: 3
Views: 3097
Reputation: 1
Here's an easier method I found while looking for the exact same answer.
rules = apriori(titanic, min_support = 0.1, min_confidence = 1.0)
rules = rules.sort_values(['confidence', 'support'], ascending =[False, False])
print(list(rules))
Upvotes: 0
Reputation: 4149
You can put your output in Pandas DataFrame and then sort by lift
You have this:
rules = apriori(titanic, min_support = 0.1, min_confidence = 1.0)
And let create results
results = list(rules)
Now we want to write our result into Pandas DataFrame and then sort by Lift
df = pd.DataFrame(columns=('Items','Antecedent','Consequent','Support','Confidence','Lift'))
Support =[]
Confidence = []
Lift = []
Items = []
Antecedent = []
Consequent=[]
for RelationRecord in results:
for ordered_stat in RelationRecord.ordered_statistics:
Support.append(RelationRecord.support)
Items.append(RelationRecord.items)
Antecedent.append(ordered_stat.items_base)
Consequent.append(ordered_stat.items_add)
Confidence.append(ordered_stat.confidence)
Lift.append(ordered_stat.lift)
df['Items'] = list(map(set, Items))
df['Antecedent'] = list(map(set, Antecedent))
df['Consequent'] = list(map(set, Consequent))
df['Support'] = Support
df['Confidence'] = Confidence
df['Lift']= Lift
Sort resulted dataframe by Lift
df.sort_values(by ='Lift', ascending = False, inplace = True)
Upvotes: 12