lolamontes69

Ch11 Ex1- Programming Collective Intelligence

Sep 14th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. """ Chapter 11 Exercise 1: More function types.
  2.  
  3.    We started with a very short list of functions. What other functions can you think of?
  4.    Implement a Euclidean distance node with four parameters.
  5.  
  6.    Starting with the distance and weighting functions in the pdf....
  7.    Think of a mathematical formula I can probably add it :D
  8.    Basically you can adapt any parameter/constant taking function and adapt it to work with gp.
  9. """
  10. def euclidean(l):
  11.     p=l[:2]
  12.     q=l[2:]
  13.     sumSq=0.0
  14.     for i in range(len(p)):
  15.         sumSq+=(p[i]-q[i])**2
  16.     # take the square root
  17.     return int(sumSq**0.5)
  18. eucw=fwrapper(euclidean,4,'euclidean')
  19.  
  20. def manhattan(l):
  21.     v1=l[:2]
  22.     v2=l[2:]
  23.     d = 0.0
  24.     for i in range(len(v1)):
  25.         d += abs(v1[i]-v2[i])
  26.     return int(d)
  27. manw=fwrapper(manhattan,4,'manhattan')
  28.  
  29. def variance(l):
  30.     mean=float(sum(l))/len(l)
  31.     s=sum([(v-mean)**2 for v in l])
  32.     return int(s/len(l))
  33. varw=fwrapper(variance,4,'variance')
  34.  
  35. # Returns potentially larger values
  36. def dotproduct(l):
  37.     a=l[:2]
  38.     b=l[2:]
  39.     return sum([a[i]*b[i] for i in range(len(a))])
  40. dotw=fwrapper(dotproduct,4,'dotproduct')
  41.  
  42. # Returns -1 0 or 1
  43. def pearson(l):
  44.     x=l[:2]
  45.     y=l[2:]
  46.     n=len(x)
  47.     vals=range(n)
  48.     sumx=sum([float(x[i]) for i in vals])
  49.     sumy=sum([float(y[i]) for i in vals])
  50.     sumxSq=sum([x[i]**2.0 for i in vals])
  51.     sumySq=sum([y[i]**2.0 for i in vals])
  52.     pSum=sum([x[i]*y[i] for i in vals])
  53.     num=pSum-(sumx*sumy/n)
  54.     den=((sumxSq-pow(sumx,2)/n)*(sumySq-pow(sumy,2)/n))**0.5
  55.     if den==0: return 0
  56.     r=num/den
  57.     return int(r)
  58. pearw=fwrapper(pearson,4,'pearson')
  59.  
  60. # Returns either 0 3 or 10
  61. def tanimoto(l):
  62.     a=l[:2]
  63.     b=l[2:]
  64.     c=[v for v in a if v in b]
  65.     if len(a)+len(b)-len(c)==0: return 0
  66.     return int(10*(float(len(c))/(len(a)+len(b)-len(c))))
  67. taniw=fwrapper(tanimoto,4,'tanimoto')
  68.  
  69. def weightedmean(l):
  70.     x=l[:2]
  71.     w=l[2:]
  72.     num=sum([x[i]*w[i] for i in range(len(w))])
  73.     den=sum([w[i] for i in range(len(w))])
  74.     if den==0: return 0
  75.     return int(num/den)
  76. weiw=fwrapper(weightedmean,4,'weightedmean')
  77.  
  78. # Returns either 7 11 or 20
  79. def giniimpurity(l):
  80.     total=len(l)
  81.     counts={}
  82.     for item in l:
  83.         counts.setdefault(item,0)
  84.         counts[item]+=1
  85.     imp=0
  86.     for j in l:
  87.         f1=float(counts[j])/total
  88.         for k in l:
  89.             if j==k: continue
  90.             f2=float(counts[k])/total
  91.             imp+=f1*f2
  92.     return int(imp*10.0)
  93. giniw=fwrapper(giniimpurity,4,'gini')
  94.  
  95.  
  96. # Returns 0 1 2 3 or 4
  97. def entropy(l):
  98.     from math import log
  99.     log2=lambda x:log(x)/log(2)
  100.     total=len(l)
  101.     counts={}
  102.     for item in l:
  103.         counts.setdefault(item,0)
  104.         counts[item]+=1
  105.     ent=0
  106.     for i in counts:
  107.         p=float(counts[i])/total
  108.         ent-=p*log2(p)
  109.     return int(ent*2.0)
  110. entw=fwrapper(entropy,4,'entropy')
  111.  
  112.  
  113. def topnum(l):
  114.     return max(l)
  115. topw=fwrapper(topnum,4,'topnum')
  116.  
  117.  
  118. def bottomnum(l):
  119.     return min(l)
  120. botw=fwrapper(bottomnum,4,'bottomnum')
  121.  
  122.  
  123. def hiddenfunction(l):
  124.     x=max(l)
  125.     y=min(l)
  126.     return x**2+2*y+3*x+5
  127. hidw=fwrapper(hiddenfunction,4,'hiddenfunction')
Advertisement
Add Comment
Please, Sign In to add comment