Changeset 3695:4e8391daacbc in orange
 Timestamp:
 05/27/07 18:40:47 (7 years ago)
 Branch:
 default
 Convert:
 27273f78a4c78e0570da8f85387ee7831ac1b583
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/orngContingency.py
r1411 r3695 18 18 19 19 20 import Numeric, orange20 import numpy, orange 21 21 from random import * 22 22 23 _log2 = 1.0/ Numeric.log(2)24 _log2e = Numeric.log(2)23 _log2 = 1.0/numpy.log(2) 24 _log2e = numpy.log(2) 25 25 26 26 def Flatten(m): 27 if len( Numeric.shape(m)) > 1:28 v = Numeric.ravel(m)27 if len(numpy.shape(m)) > 1: 28 v = numpy.ravel(m) 29 29 else: 30 30 v = m 31 31 return v 32 32 33 33 def Probabilities(m): 34 t = Numeric.sum(Flatten(m))34 t = numpy.sum(Flatten(m)) 35 35 if t == 0: 36 36 return 0 … … 41 41 v = Flatten(m) 42 42 pv = Probabilities(v) 43 lv = Numeric.log(Numeric.clip(pv,1e6,1.0))*_log244 return  Numeric.dot(pv,lv)43 lv = numpy.log(numpy.clip(pv,1e6,1.0))*_log2 44 return numpy.dot(pv,lv) 45 45 46 46 … … 75 75 76 76 def IPF(self,tolerance=1e6,maxiterations=100): 77 d = Numeric.shape(self.m)77 d = numpy.shape(self.m) 78 78 p = self.pm 79 79 px = self.pab … … 84 84 pzz = self.pc 85 85 v = 1.0/(d[0]*d[1]*d[2]) 86 model = Numeric.ones(d,Numeric.Float)*v86 model = numpy.ones(d,numpy.float)*v 87 87 88 88 iterations = 0 … … 93 93 for c in range(3): 94 94 if c == 0: 95 mx = Probabilities( Numeric.sum(model,axis=2))95 mx = Probabilities(numpy.sum(model,axis=2)) 96 96 elif c == 1: 97 my = Probabilities( Numeric.sum(model,axis=1))97 my = Probabilities(numpy.sum(model,axis=1)) 98 98 else: 99 mz = Probabilities( Numeric.sum(model,axis=0))99 mz = Probabilities(numpy.sum(model,axis=0)) 100 100 for x in xrange(d[0]): 101 101 for y in xrange(d[1]): … … 107 107 else: 108 108 model[x,y,z] *= pz[y,z]/max(mz[y,z],1e16) 109 # EVALUATE 109 # EVALUATE 110 110 div = 0.0 111 111 for x in xrange(d[0]): … … 113 113 for z in xrange(d[2]): 114 114 if p[x,y,z]>0: 115 div += p[x,y,z]* Numeric.log(p[x,y,z]/model[x,y,z])115 div += p[x,y,z]*numpy.log(p[x,y,z]/model[x,y,z]) 116 116 #print iterations, Entropy(model), div 117 117 iterations += 1 … … 122 122 123 123 def KSA(self): 124 d = Numeric.shape(self.m)125 self.kirk = Numeric.ones(d,Numeric.Float)124 d = numpy.shape(self.m) 125 self.kirk = numpy.ones(d,numpy.float) 126 126 127 127 # normalize kirkwood approximation … … 145 145 for z in xrange(d[2]): 146 146 if self.pm[x,y,z]>0: 147 div += self.pm[x,y,z]* Numeric.log(self.pm[x,y,z]/self.kirk[x,y,z])147 div += self.pm[x,y,z]*numpy.log(self.pm[x,y,z]/self.kirk[x,y,z]) 148 148 return (div*_log2,sumx) 149 149 … … 153 153 pkirkwood /= self.pa[x]*self.pb[y]*self.pc[z] 154 154 if ptrue > 1e6: 155 div = Numeric.log(ptrue/pkirkwood)155 div = numpy.log(ptrue/pkirkwood) 156 156 else: 157 157 div = 0.0 … … 164 164 pkirkwood /= self.pa[x]*self.pb[y]*self.pc[z] 165 165 if ptrue > 1e6: 166 div = Numeric.log(ptrue/(pkirkwood*norm))166 div = numpy.log(ptrue/(pkirkwood*norm)) 167 167 else: 168 168 div = 0.0 … … 172 172 self.names = names 173 173 self.values = values 174 m = Numeric.array(m,Numeric.Float)174 m = numpy.array(m,numpy.float) 175 175 self.m = m 176 self.bc = Numeric.sum(m,axis=0)177 self.ac = Numeric.sum(m,axis=1)178 self.ab = Numeric.sum(m,axis=2)179 180 self.a = Numeric.sum(self.ab,axis=1)181 self.b = Numeric.sum(self.ab,axis=0)182 self.c = Numeric.sum(self.ac,axis=0)183 self.total = Numeric.sum(self.a)176 self.bc = numpy.sum(m,axis=0) 177 self.ac = numpy.sum(m,axis=1) 178 self.ab = numpy.sum(m,axis=2) 179 180 self.a = numpy.sum(self.ab,axis=1) 181 self.b = numpy.sum(self.ab,axis=0) 182 self.c = numpy.sum(self.ac,axis=0) 183 self.total = numpy.sum(self.a) 184 184 185 185 self.pm = Probabilities(self.m) … … 191 191 self.pc = Probabilities(self.c) 192 192 dof = 0 193 (ni,nj,nk) = Numeric.shape(self.m)193 (ni,nj,nk) = numpy.shape(self.m) 194 194 for ii in xrange(ni): 195 195 for jj in xrange(nj): … … 215 215 pkirkwood = self.pa[x]*self.pb[y] 216 216 if ptrue > 1e6: 217 div = Numeric.log(ptrue/pkirkwood)217 div = numpy.log(ptrue/pkirkwood) 218 218 else: 219 219 div = 0.0 … … 226 226 f = Flatten(self.m) 227 227 p = Probabilities(f) 228 LUT = Numeric.zeros((self.total,),Numeric.Int)228 LUT = numpy.zeros((self.total,),numpy.int) 229 229 c = 0 230 230 for i in xrange(len(f)): … … 234 234 assert(c == self.total) 235 235 for i in xrange(N): 236 nt = Numeric.zeros((len(f),),Numeric.Float)236 nt = numpy.zeros((len(f),),numpy.float) 237 237 for j in xrange(c): 238 238 nt[ LUT[randint(0,c1)] ] += 1 … … 241 241 for j in xrange(len(f)): 242 242 if q[j] > 1e6 and p[j] > 0.0: 243 loss += q[j]* Numeric.log(q[j]/p[j])244 #loss += p[j]* Numeric.log(max(p[j],1e5)/max(q[j],1e6))243 loss += q[j]*numpy.log(q[j]/p[j]) 244 #loss += p[j]*numpy.log(max(p[j],1e5)/max(q[j],1e6)) 245 245 if loss >= nlimit: 246 246 hits += 1 … … 254 254 self.names = names 255 255 self.values = values 256 m = Numeric.array(m,Numeric.Float)256 m = numpy.array(m,numpy.float) 257 257 self.m = m 258 258 259 self.a = Numeric.sum(self.m,axis=1)260 self.b = Numeric.sum(self.m,axis=0)261 self.total = Numeric.sum(self.a)259 self.a = numpy.sum(self.m,axis=1) 260 self.b = numpy.sum(self.m,axis=0) 261 self.total = numpy.sum(self.a) 262 262 263 263 self.pa = Probabilities(self.a) … … 265 265 self.pm = Probabilities(self.m) 266 266 dof = 0 267 (ni,nj) = Numeric.shape(self.m)267 (ni,nj) = numpy.shape(self.m) 268 268 for ii in xrange(ni): 269 269 for jj in xrange(nj): … … 277 277 nj = len(b.values) 278 278 nk = len(c.values) 279 M = prior* Numeric.ones((ni,nj,nk),Numeric.Float)279 M = prior*numpy.ones((ni,nj,nk),numpy.float) 280 280 if wid == None: # no weighting 281 281 for ex in t: … … 294 294 ni = len(a.values) 295 295 nj = len(b.values) 296 M = prior* Numeric.ones((ni,nj),Numeric.Float)296 M = prior*numpy.ones((ni,nj),numpy.float) 297 297 if wid == None: # no weighting 298 298 for ex in t:
Note: See TracChangeset
for help on using the changeset viewer.