Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 18th, 2012, 06:53 PM
 nasicode
Registered User

Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 39 m 24 sec
Reputation 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
November 18th, 2012, 09:33 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
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!

#3
November 18th, 2012, 09:44 PM
 nasicode
Registered User

Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 39 m 24 sec
Reputation Power: 0
Quote:
 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

#4
November 18th, 2012, 09:58 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
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.

#5
November 18th, 2012, 10:53 PM
 nasicode
Registered User

Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 39 m 24 sec
Reputation Power: 0
Quote:
 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

#6
November 18th, 2012, 11:42 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
[obj for obj in L if len(obj) != 1]

#7
November 19th, 2012, 06:00 AM
 nasicode
Registered User

Join Date: Nov 2012
Posts: 7
Time spent in forums: 1 h 39 m 24 sec
Reputation 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

#8
November 19th, 2012, 08:38 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
Read the python tutorial interactively, try additional experiments. If you still have questions post the input and the output you expect.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Confidence calculation in association rule