#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Confidence calculation in association rule


    supportData = {('ELF'): 0.75, ('CAT'): 0.75, ('BAT', 'CAT', 'ELF'): 0.5, ('ARK', 'BAT'): 0.25, ('ARK', 'ELF'): 0.25, ('CAT', 'ELF'): 0.5, ('DOG'): 0.25, ('BAT', 'CAT'): 0.5, ('BAT', 'ELF'): 0.75, ('ARK'): 0.5, ('ARK', 'CAT'): 0.5, ('BAT'): 0.75}

    L = [('ARK'), ('CAT'), ('CAT'), ('ELF'),('ARK', 'CAT'), ('BAT', 'ELF'), ('BAT', 'CAT'), ('CAT', 'ELF'),('BAT', 'CAT', 'ELF')]



    Code:
    for freqSet in L:
        H =  list(freqSet)
    
        if len(H) == 1:
            pass
        else:
            for conseq in H:
                freqsetlist = list(freqSet)
                freqsetlist.remove(conseq)
                if len(freqsetlist) == 1:
                   conf = supportData[freqSet]/supportData[tuple(freqsetlist)[0]]
                   if conf >= 0.1:
                      print freqsetlist,'-->',conseq,'conf:',conf
                else:
                   conf = supportData[freqSet]/supportData[tuple(freqsetlist)[:]]
                   if conf >= 0.1:
                      print freqsetlist,'-->',conseq,'conf:',conf
    
    
    KeyError: ('R','K')
    Can someone point out why I am getting this error? It seems the error occur when len(freqsetlist) is > 1. That is when calculating tuple with 3 element
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Someone asked a similar question "earlier". Was it you? Whatever. Today I see the answer.

    ('ELF') is the string 'ELF' in parentheses.

    ('ELF',) is a length 1 tuple with a single element, 'ELF'.

    Look through my myriad python answers in this forum, if you can stand it, you'll see that I often write lists, tuples, and argument lists with a trailing comma. You'll also see that I'm nowhere near fully consistent.

    Code:
    f(a,b,c,)
    
    (a,b,c,) = range(3)  # or range(3,)
    
    
    for (i,obj,) in enumerate(a_list): # or enumerate(a_list,)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Someone asked a similar question "earlier". Was it you? Whatever. Today I see the answer.

    ('ELF') is the string 'ELF' in parentheses.

    ('ELF',) is a length 1 tuple with a single element, 'ELF'.

    Look through my myriad python answers in this forum, if you can stand it, you'll see that I often write lists, tuples, and argument lists with a trailing comma. You'll also see that I'm nowhere near fully consistent.

    Code:
    f(a,b,c,)
    
    (a,b,c,) = range(3)  # or range(3,)
    
    
    for (i,obj,) in enumerate(a_list): # or enumerate(a_list,)
    Code:
    #else:
                   #conf = supportData[freqSet]/supportData[tuple(freqsetlist)[:]]
                   #if conf >= 0.1:
                      #print freqsetlist,'-->',conseq,'conf:',conf
    
    If you comment these lines out, you get this output:
    
    ['CAT'] --> ARK conf: 0.666666666667
    ['ARK'] --> CAT conf: 1.0
    ['ELF'] --> BAT conf: 1.0
    ['BAT'] --> ELF conf: 1.0
    ['CAT'] --> BAT conf: 0.666666666667
    ['BAT'] --> CAT conf: 0.666666666667
    ['ELF'] --> CAT conf: 0.666666666667
    ['CAT'] --> ELF conf: 0.666666666667
    This means the code works well except when it comes to those lines
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Code:
    L = [('ARK',), ('CAT',), ('CAT',), ('ELF',),('ARK', 'CAT'), ('BAT', 'ELF'), ('BAT', 'CAT'), ('CAT', 'ELF'),('BAT', 'CAT', 'ELF')]
    I changed L as above, ran your program and there were no problems.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    L = [('ARK',), ('CAT',), ('CAT',), ('ELF',),('ARK', 'CAT'), ('BAT', 'ELF'), ('BAT', 'CAT'), ('CAT', 'ELF'),('BAT', 'CAT', 'ELF')]
    I changed L as above, ran your program and there were no problems.

    So how do I just bye pass tuples with one element in L
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    [obj for obj in L if len(obj) != 1]
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Code:
    supportData = {('ELF'): 0.75, ('CAT'): 0.75, ('BAT', 'CAT', 'ELF'): 0.5, ('ARK', 'BAT'): 0.25, ('ARK', 'ELF'): 0.25, ('CAT', 'ELF'): 0.5, ('DOG'): 0.25, ('BAT', 'CAT'): 0.5, ('BAT', 'ELF'): 0.75, ('ARK'): 0.5, ('ARK', 'CAT'): 0.5, ('BAT'): 0.75}
    
    L = [('ARK'), ('CAT'), ('CAT'), ('ELF'),('ARK', 'CAT'), ('BAT', 'ELF'), ('BAT', 'CAT'), ('CAT', 'ELF'),('BAT', 'CAT', 'ELF')]
    #L = [('ARK',), ('CAT',), ('CAT',), ('ELF',),('ARK', 'CAT'), ('BAT', 'ELF'), ('BAT', 'CAT'), ('CAT', 'ELF'),('BAT', 'CAT', 'ELF')]
    
    
    
    
    [freqSet for freqSet in L if len(freqSet) != 1]
    
    H =  list(freqSet)
    for conseq in H:
                freqsetlist = list(freqSet)
                freqsetlist.remove(conseq)
                if len(freqsetlist) == 1:
                   conf = supportData[freqSet]/supportData[tuple(freqsetlist)[0]]
                   if conf >= 0.1:
                      print freqsetlist,'-->',conseq,'conf:',conf
                else:
                   conf = supportData[freqSet]/supportData[tuple(freqsetlist)[:]]
                   if conf >= 0.1:
                      print freqsetlist,'-->',conseq,'conf:',conf
    
    Output
    ['CAT', 'ELF'] --> BAT conf: 1.0
    ['BAT', 'ELF'] --> CAT conf: 0.666666666667
    ['BAT', 'CAT'] --> ELF conf: 1.0
    It seems the the tuple with two elements are not included, and their length is more than 1
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Read the python tutorial interactively, try additional experiments. If you still have questions post the input and the output you expect.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo