In [1]:
#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 [2]:
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 6 (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 [3]:
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 [6]:
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 [5]:
x = range(1000)
In [12]:
plt.plot([x in [, [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()
In [13]:
labels = []
plotHandles = []
y1 = [AllNeeded((250,i,250,250)) for i in x]
#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 for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('cosine')
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()
In [14]:
labels = []
plotHandles = []
y1 = [AllNeeded((250,250,i,250)) for i in x]
#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 for y in y1]) #need the ',' per ** below
plotHandles.append(t)
labels.append('cosine')
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()
In [15]:
labels = []
plotHandles = []
y1 = [AllNeeded((250,250,250,i)) for i in x]
#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()
In [67]:
def doregularthings(xlab,ylab,title,ymin,ymax):
plt.xlim(0,10000)
# plt.ylim(ymin,ymax)
plt.ylim(-30,0)
plt.title(title)
plt.ylabel(ylab)
plt.xlabel(xlab)
In [36]:
plt.figure(1)
###Laplace#####
#F11
plt.subplot(221)
plt.scatter([x.f11 for x in lst],[x.laplace for x in lst])
doregularthings('F11','laplace','Laplace vs F11',0,1)
#F10
plt.subplot(222)
plt.scatter([x.f10 for x in lst],[x.laplace for x in lst])
doregularthings('F10','laplace','Laplace vs F10',0,1)
#F01
plt.subplot(223)
plt.scatter([x.f01 for x in lst],[x.laplace for x in lst])
doregularthings('F01','laplace','Laplace vs F01',0,1)
#F00
plt.subplot(224)
plt.scatter([x.f00 for x in lst],[x.laplace for x in lst])
doregularthings('F00','laplace','Laplace vs F00',0,1)
plt.show()
In [56]:
plt.figure(2)
###conviction#####
#F11
plt.subplot(221)
plt.scatter([x.f11 for x in lst],[x.conviction for x in lst])
doregularthings('F11','conviction','conviction vs F11',0,20)
#F10
plt.subplot(222)
plt.scatter([x.f10 for x in lst],[x.conviction for x in lst])
doregularthings('F10','conviction','conviction vs F10',0,20)
#F01
plt.subplot(223)
plt.scatter([x.f01 for x in lst],[x.conviction for x in lst])
doregularthings('F01','conviction','conviction vs F01',0,20)
#F00
plt.subplot(224)
plt.scatter([x.f00 for x in lst],[x.conviction for x in lst])
doregularthings('F00','conviction','conviction vs F00',0,20)
plt.show()
In [68]:
plt.figure(3)
###Certain Factor#####
#F11
plt.subplot(221)
plt.scatter([x.f11 for x in lst],[x.certinfactor for x in lst])
doregularthings('F11','Certain Factor','Certain Factor vs F11',0,1)
#F10
plt.subplot(222)
plt.scatter([x.f10 for x in lst],[x.certinfactor for x in lst])
doregularthings('F10','Certain Factor','Certain Factor vs F10',0,1)
#F01
plt.subplot(223)
plt.scatter([x.f01 for x in lst],[x.certinfactor for x in lst])
doregularthings('F01','Certain Factor','Certain Factor vs F01',0,1)
#F00
plt.subplot(224)
plt.scatter([x.f00 for x in lst],[x.certinfactor for x in lst])
doregularthings('F00','Certain Factor','Certain Factor vs F00',0,1)
plt.show()
In [ ]:
self. = (self.f1plus*self.fplus0)/self.isZero(self.N*self.f10)
self.certinfacto