In [6]:
#Note Remove This Line to Get the Figure in Seperate Window\n",
#%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import itertools
from math import sqrt,pow
np.random.seed(4)
In [7]:
class AllNeeded:
def __init__(self,value):
self.f11=float(value[0])
self.f10=float(value[1])
self.f01=float(value[2])
self.f00=float(value[3])
self.f1plus= self.f11+self.f10
self.f0plus= self.f01+self.f00
self.fplus1= self.f11+self.f01
self.fplus0=self.f10+self.f00
self.N =float(sum(value))
self.all_confidence = min(self.f11/self.f1plus,self.f11/self.fplus1)
self.kappa = (self.N*self.f11+ self.N*self.f00-self.f1plus*self.fplus1-self.f0plus*self.fplus0)/(pow(self.N,2)-self.f1plus*self.fplus1-self.f0plus*self.fplus0)
self.interest =(self.N*self.f11)/(self.f1plus*self.fplus1)
self.cosine= (self.f11)/(sqrt(self.f1plus*self.fplus1))
self.Jaccard = self.f11/(self.f1plus+self.fplus1-self.f11)
#Upgrade for Question Three (5 more measurements )
self.laplace = (self.f11+1)/(self.f1plus+2)
self.conviction = (self.f1plus*self.fplus0)/self.isZero(self.N*self.f10)
self.certinfactor=(self.f11/self.N - self.fplus1/self.N)/(1-(self.fplus1/self.N))
def isZero(self,value):
return 1 if value==0 else value
def __str__(self):
return 'f11:{},f10:{},f01:{},f00:{},f1+:{},f0+:{},f+1:{},f+0:{}'.format(self.f11,self.f10,self.f01,
self.f00,self.f1plus,self.f0plus,
self.fplus1,self.fplus0)
To generate 4 numbers sum to 10000 I used
#result =np.random.dirichlet(np.ones(4),size=1)
#result = result *10000
result =np.array( result,dtype=int)
result[0,np.random.randint(4)]+=2
In [8]:
lst = []
total =np.array(np.random.dirichlet(np.ones(4),size=10000)*10000,dtype=int)
for result in total:
result[np.random.randint(4,size=1)]+=(10000-np.sum(result))
lst.append(AllNeeded(result))
In [9]:
allconf = sorted(lst,key=lambda x:x.all_confidence,reverse=True)[:10]
print '=========All Confidence=============='
for element in allconf:
print element,',all_confidence :',element.all_confidence
print '========= kappa =============='
allconf = sorted(lst,key=lambda x:x.kappa,reverse=True)[:10]
for element in allconf:
print element,',kappa :',element.kappa
print '========= interest =============='
allconf = sorted(lst,key=lambda x:x.interest,reverse=True)[:10]
for element in allconf:
print element,',interest :',element.interest
print '========= cosine =============='
allconf = sorted(lst,key=lambda x:x.cosine,reverse=True)[:10]
for element in allconf:
print element,',cosine :',element.cosine
print '========= Jaccard =============='
allconf = sorted(lst,key=lambda x:x.Jaccard,reverse=True)[:10]
for element in allconf:
print element,',Jaccard :',element.Jaccard
In [10]:
x = range(1000)
y1 = [AllNeeded((i,250,250,250)) for i in x]
In [13]:
labels = []
plotHandles = []
#for i in range(1, num_plots + 1):
t, = plt.plot(x, [y.all_confidence for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('All Confidence')
t, = plt.plot(x, [y.kappa for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('kappa')
t, = plt.plot(x, [y.interest for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('interest')
t, = plt.plot(x, [y.cosine+0.1 for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('cosine + 0.1')
t, = plt.plot(x, [y.Jaccard for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('Jaccard')
plt.legend(plotHandles, labels, 'upper left',loc=1)
plt.show()
plt.savefig('f11.png')
In [ ]: