lolamontes69

probability_graph.py : because I couldn't install matplotlib

Aug 8th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. """ As I couldn't manage to install matplotlib properly, I decided to implement
  2. a version of probability graph from the PDF page 187 using the Python Imaging Library.
  3. Not as smooth as the matplotlib version but it still provides meaningful data :)
  4.  
  5. See end for usage
  6. """
  7.  
  8. import Image, ImageDraw, ImageOps
  9. from random import randint
  10. import numpredict as numpredict
  11.  
  12. def draw_graph(list1,jpeg='probability_graph.jpg'):
  13.     # height and width of jpeg
  14.     w,h=0,0
  15.     for a in range(len(list1)):
  16.         if list1[a][0]>w:
  17.             w=list1[a][0]
  18.         if list1[a][1]>h:
  19.             h=list1[a][1]
  20.     # Draw graph and save image
  21.     img = Image.new('RGB',(w+45, h+45),(255,255,255))
  22.     draw = ImageDraw.Draw(img)
  23.     for a in range(len(list1)-1):
  24.         xycoords=(list1[a][0]+20,list1[a][1]+10)
  25.         x1y1coords=(list1[a][0]+20,10)
  26.         draw.line([xycoords,x1y1coords], fill=128)
  27.     del draw
  28.     img = ImageOps.flip(img)
  29.     draw = ImageDraw.Draw(img)
  30.     for a in range(0,h,50):
  31.         if a == 0: b=str(a)
  32.         else: b=str(a/500.0)
  33.         draw.text((0,h-a+28),b, fill=0)
  34.     for a in range(0,w,25):
  35.         draw.text((a+18,h+35),str(a), fill=0)
  36.     del draw
  37.     img.save(jpeg,'JPEG')
  38.  
  39. def av_probs(data,vec1,highnum,k,weightf,width_a):
  40.     dicus={}
  41.     for a in range(0,highnum+1):
  42.         b = numpredict.probguess(data,vec1,a,a+width_a,k,weightf)
  43.         for c in range(a,a+width_a):
  44.             if c not in dicus: dicus[c]=[]
  45.             dicus[c].append(b)
  46.     high2=0
  47.     for prob in dicus:
  48.         if len(dicus[prob])>high2: high2=len(dicus[prob])
  49.     for x in dicus:
  50.         a = sum(dicus[x])
  51.         if a == 0: dicus[x] = 0.0
  52.         else:
  53.             b = a/high2    
  54.             dicus[x] = b
  55.     list1=[]
  56.     for a in dicus:
  57.         b=(a,int(dicus[a]*500))
  58.         list1.append(b)
  59.     del dicus
  60.     return list1
  61.  
  62. def main(data,vec1,highnum,k,weightf,width_a,jpeg):
  63.     list1 = av_probs(data,vec1,highnum,k,weightf,width_a)
  64.     draw_graph(list1,jpeg='probability_graph.jpg')
  65.  
  66. """
  67. Notes:
  68.  
  69. 1: width_a
  70. **********
  71. probguess() uses high, and low.
  72. av_probs() checks the probabilities within the range 0-highnum, width_a is used
  73.           to provide a range to pass to probguess(). All the results for each number
  74.           within the range are added to a list for each number and average probabilities
  75.           are calculated then converted into y coordinates.
  76.  
  77. 2: Usage
  78. ********
  79. save as probability_graph.py
  80.  
  81. import probability_graph as av_probs
  82. import numpredict as numpredict
  83. data=numpredict.wineset3()
  84. av_probs.main(data,[99,20],120,5,numpredict.gaussian,50,'probability_graph.jpg')
  85.  
  86. """
Advertisement
Add Comment
Please, Sign In to add comment