# Changeset 3695:4e8391daacbc in orange

Ignore:
Timestamp:
05/27/07 18:40:47 (7 years ago)
Branch:
default
Convert:
27273f78a4c78e0570da8f85387ee7831ac1b583
Message:

numeric -> numpy

File:
1 edited

Unmodified
Removed
• ## orange/orngContingency.py

 r1411 import Numeric, orange import numpy, orange from random import * _log2 = 1.0/Numeric.log(2) _log2e = Numeric.log(2) _log2 = 1.0/numpy.log(2) _log2e = numpy.log(2) def Flatten(m): if len(Numeric.shape(m)) > 1: v = Numeric.ravel(m) if len(numpy.shape(m)) > 1: v = numpy.ravel(m) else: v = m return v def Probabilities(m): t = Numeric.sum(Flatten(m)) t = numpy.sum(Flatten(m)) if t == 0: return 0 v = Flatten(m) pv = Probabilities(v) lv = Numeric.log(Numeric.clip(pv,1e-6,1.0))*_log2 return -Numeric.dot(pv,lv) lv = numpy.log(numpy.clip(pv,1e-6,1.0))*_log2 return -numpy.dot(pv,lv) def IPF(self,tolerance=1e-6,maxiterations=100): d = Numeric.shape(self.m) d = numpy.shape(self.m) p = self.pm px = self.pab pzz = self.pc v = 1.0/(d[0]*d[1]*d[2]) model = Numeric.ones(d,Numeric.Float)*v model = numpy.ones(d,numpy.float)*v iterations = 0 for c in range(3): if c == 0: mx = Probabilities(Numeric.sum(model,axis=2)) mx = Probabilities(numpy.sum(model,axis=2)) elif c == 1: my = Probabilities(Numeric.sum(model,axis=1)) my = Probabilities(numpy.sum(model,axis=1)) else: mz = Probabilities(Numeric.sum(model,axis=0)) mz = Probabilities(numpy.sum(model,axis=0)) for x in xrange(d[0]): for y in xrange(d[1]): else: model[x,y,z] *= pz[y,z]/max(mz[y,z],1e-16) # EVALUATE # EVALUATE div = 0.0 for x in xrange(d[0]): for z in xrange(d[2]): if p[x,y,z]>0: div += p[x,y,z]*Numeric.log(p[x,y,z]/model[x,y,z]) div += p[x,y,z]*numpy.log(p[x,y,z]/model[x,y,z]) #print iterations, Entropy(model), div iterations += 1 def KSA(self): d = Numeric.shape(self.m) self.kirk = Numeric.ones(d,Numeric.Float) d = numpy.shape(self.m) self.kirk = numpy.ones(d,numpy.float) # normalize kirkwood approximation for z in xrange(d[2]): if self.pm[x,y,z]>0: div += self.pm[x,y,z]*Numeric.log(self.pm[x,y,z]/self.kirk[x,y,z]) div += self.pm[x,y,z]*numpy.log(self.pm[x,y,z]/self.kirk[x,y,z]) return (div*_log2,sumx) pkirkwood /= self.pa[x]*self.pb[y]*self.pc[z] if ptrue > 1e-6: div = Numeric.log(ptrue/pkirkwood) div = numpy.log(ptrue/pkirkwood) else: div = 0.0 pkirkwood /= self.pa[x]*self.pb[y]*self.pc[z] if ptrue > 1e-6: div = Numeric.log(ptrue/(pkirkwood*norm)) div = numpy.log(ptrue/(pkirkwood*norm)) else: div = 0.0 self.names  = names self.values = values m = Numeric.array(m,Numeric.Float) m = numpy.array(m,numpy.float) self.m = m self.bc = Numeric.sum(m,axis=0) self.ac = Numeric.sum(m,axis=1) self.ab = Numeric.sum(m,axis=2) self.a = Numeric.sum(self.ab,axis=1) self.b = Numeric.sum(self.ab,axis=0) self.c = Numeric.sum(self.ac,axis=0) self.total = Numeric.sum(self.a) self.bc = numpy.sum(m,axis=0) self.ac = numpy.sum(m,axis=1) self.ab = numpy.sum(m,axis=2) self.a = numpy.sum(self.ab,axis=1) self.b = numpy.sum(self.ab,axis=0) self.c = numpy.sum(self.ac,axis=0) self.total = numpy.sum(self.a) self.pm = Probabilities(self.m) self.pc = Probabilities(self.c) dof = 0 (ni,nj,nk) = Numeric.shape(self.m) (ni,nj,nk) = numpy.shape(self.m) for ii in xrange(ni): for jj in xrange(nj): pkirkwood = self.pa[x]*self.pb[y] if ptrue > 1e-6: div = Numeric.log(ptrue/pkirkwood) div = numpy.log(ptrue/pkirkwood) else: div = 0.0 f = Flatten(self.m) p = Probabilities(f) LUT = Numeric.zeros((self.total,),Numeric.Int) LUT = numpy.zeros((self.total,),numpy.int) c = 0 for i in xrange(len(f)): assert(c == self.total) for i in xrange(N): nt = Numeric.zeros((len(f),),Numeric.Float) nt = numpy.zeros((len(f),),numpy.float) for j in xrange(c): nt[ LUT[randint(0,c-1)] ] += 1 for j in xrange(len(f)): if q[j] > 1e-6 and p[j] > 0.0: loss += q[j]*Numeric.log(q[j]/p[j]) #loss += p[j]*Numeric.log(max(p[j],1e-5)/max(q[j],1e-6)) loss += q[j]*numpy.log(q[j]/p[j]) #loss += p[j]*numpy.log(max(p[j],1e-5)/max(q[j],1e-6)) if loss >= nlimit: hits += 1 self.names  = names self.values = values m = Numeric.array(m,Numeric.Float) m = numpy.array(m,numpy.float) self.m = m self.a = Numeric.sum(self.m,axis=1) self.b = Numeric.sum(self.m,axis=0) self.total = Numeric.sum(self.a) self.a = numpy.sum(self.m,axis=1) self.b = numpy.sum(self.m,axis=0) self.total = numpy.sum(self.a) self.pa = Probabilities(self.a) self.pm = Probabilities(self.m) dof = 0 (ni,nj) = Numeric.shape(self.m) (ni,nj) = numpy.shape(self.m) for ii in xrange(ni): for jj in xrange(nj): nj = len(b.values) nk = len(c.values) M = prior*Numeric.ones((ni,nj,nk),Numeric.Float) M = prior*numpy.ones((ni,nj,nk),numpy.float) if wid == None: # no weighting for ex in t: ni = len(a.values) nj = len(b.values) M = prior*Numeric.ones((ni,nj),Numeric.Float) M = prior*numpy.ones((ni,nj),numpy.float) if wid == None: # no weighting for ex in t:
Note: See TracChangeset for help on using the changeset viewer.