asaini / Apriori

Python Implementation of Apriori Algorithm for finding Frequent sets and Association Rules
MIT License
771 stars 435 forks source link

wrong syntax #13

Open albertoarturovergani opened 8 years ago

albertoarturovergani commented 8 years ago

hi, I run your code but I have received this message

File "apriori.py", line 118 for item, support in sorted(items, key=lambda (item, support): support): ^ SyntaxError: invalid syntax

Where is the error?

maxawad commented 8 years ago

Still gives me an error on for item, support in sorted(items,key=support:items[1]): .......................................................................^ (on the colon)

Fan-Feng commented 8 years ago

try to replace this line with for item, support in sorted(items, key=lambda support: support[1])

boris314159 commented 7 years ago

Thanks faithfeng, that works for printing the item sets. I have a related issue however printing the rules, in Python 3 ("tuple unpacking not supported in Python 3").

How to apply this pattern to the line below to print the rules?

for rule, confidence in sorted(rules, key=lambda (rule, confidence): confidence):

thomasjhuang commented 7 years ago

For python 3, I found a fix that worked for me on both prints: Change your sorted to sorted(items, key=operator.itemgetter(1)) and it should sort by values and then print correctly. Add reverse=True argument to change the order.

import operator

def printResults(items, rules):
    for item, support in sorted(items, key=operator.itemgetter(1)):
        print("item: %s , %.3f" % (str(item), support))
    print("\n------------------------ RULES:")
    for rule, confidence in sorted(rules, key=operator.itemgetter(1)):
        pre, post = rule
        print("Rule: %s ==> %s , %.3f" % (str(pre), str(post), confidence))
leleabc commented 7 years ago

hello faithefeng, I also have the error issue, I replaced the line by "for item, support in sorted(items, key=lambda support: support[1])" could you advise?

thx

hichembahloul79 commented 6 years ago
for key, value in largeSet.items()[1:]:

TypeError: 'dict_items' object is not subscriptable

smitapeter commented 6 years ago

for key, value in largeSet.items()[1:]:

TypeError: 'dict_items' object is not subscriptable

please tell me how can i resolve this

smitapeter commented 6 years ago

Please help me solve the above problem

Vipinsing27 commented 6 years ago

@smitapeter use it for key, value in list(largeSet.items())[1:]:

TheIdanLapid commented 6 years ago

@Vipinsing27 Thank you, that worked for me!