lolamontes69

Ch3 Ex7-Collective Intelligence (scaledown() rewrite)

Jun 12th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.43 KB | None | 0 0
  1. #
  2. #    --------------------------------------------------------------------
  3. #    scaledown() from clusters.py
  4. #    rewrote for Collective Intelligence chapter 3 exercise 7
  5. #    in 1D, 3D and 6D versions
  6. #    --------------------------------------------------------------------
  7. #
  8. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  9. #                           1 Dimensional VERSION
  10. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  11.  
  12. def scaledown_1D(data, distance=pearson, rate=0.01):
  13.     n = len(data)
  14.  
  15.     # The real distances between every pair of items
  16.     realdist = [[distance(data[i], data[j]) for j in range(n)] for i in range(0, n)]
  17.     outersum = 0.0
  18.  
  19.     # Randomly initialize the starting points of the locations in 2D
  20.     loc = [[random.random()] for i in range(n)]
  21.     fakedist = [[0.0 for j in range(n)] for i in range(n)]
  22.    
  23.     lasterror = None
  24.     for m in range(0, 1000):
  25.         # Find projected distances
  26.         for i in range(n):
  27.             for j in range(n):
  28.                 fakedist[i][j]=sqrt(sum([pow(loc[i][x]-loc[j][x],2) for x in range(len(loc[i]))]))
  29.  
  30.         # Move points
  31.         grad = [[0.0] for i in range(n)]
  32.         errorterm = 0
  33.         totalerror = 0
  34.         for k in range(n):
  35.             for j in range(n):
  36.                 if j == k: continue
  37.                 try:
  38.                     # The error is percent difference between the distances
  39.                     errorterm = (fakedist[j][k]-realdist[j][k])/realdist[j][k]
  40.  
  41.                     # Each point needs to be moved away from or towards the other
  42.                     # point in proportion to how much error it has
  43.                     grad[k][0] += ((loc[k][0]-loc[j][0])/fakedist[j][k])*errorterm
  44.  
  45.                     # Keep track of the total error
  46.                 except: pass
  47.                 totalerror += abs(errorterm)
  48.         print totalerror
  49.  
  50.         # If the answer got worse by moving the points, we are done
  51.         if lasterror and lasterror<totalerror: break
  52.         lasterror = totalerror
  53.  
  54.         # Move each of the points by the learning rate times the gradient
  55.         for k in range(n):
  56.             loc[k][0] -= rate*grad[k][0]
  57.  
  58.     return loc
  59.  
  60. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  61. #                            3 Dimensional VERSION
  62. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  63.  
  64. def scaledown_3D(data, distance=pearson, rate=0.01):
  65.     n = len(data)
  66.  
  67.     # The real distances between every pair of items
  68.     realdist = [[distance(data[i], data[j]) for j in range(n)] for i in range(0, n)]
  69.     outersum = 0.0
  70.  
  71.     # Randomly initialize the starting points of the locations in 2D
  72.     # ---------------------ADD DIMENSIONS HERE---------------------------------
  73.     loc = [[random.random(),random.random(),random.random()] for i in range(n)]
  74.     # -------------------------------------------------------------------------
  75.     fakedist = [[0.0 for j in range(n)] for i in range(n)]
  76.    
  77.     lasterror = None
  78.     for m in range(0, 1000):
  79.         # Find projected distances
  80.         for i in range(n):
  81.             for j in range(n):
  82.                 fakedist[i][j]=sqrt(sum([pow(loc[i][x]-loc[j][x],2) for x in range(len(loc[i]))]))
  83.  
  84.         # Move points
  85.         # ---------------------ADD DIMENSIONS HERE-----------------------------
  86.         grad = [[0.0, 0.0, 0.0] for i in range(n)]
  87.         # ---------------------------------------------------------------------
  88.         errorterm = 0
  89.         totalerror = 0
  90.         for k in range(n):
  91.             for j in range(n):
  92.                 if j == k: continue
  93.                 try:
  94.                     # The error is percent difference between the distances
  95.                     errorterm = (fakedist[j][k]-realdist[j][k])/realdist[j][k]
  96.  
  97.                     # Each point needs to be moved away from or towards the other
  98.                     # point in proportion to how much error it has
  99.                     # ---------------ADD DIMENSIONS HERE--------------------------
  100.                     grad[k][0] += ((loc[k][0]-loc[j][0])/fakedist[j][k])*errorterm          
  101.                     grad[k][1] += ((loc[k][1]-loc[j][1])/fakedist[j][k])*errorterm
  102.                     grad[k][2] += ((loc[k][2]-loc[j][2])/fakedist[j][k])*errorterm
  103.                     # ------------------------------------------------------------
  104.  
  105.                     # Keep track of the total error
  106.                 except: pass
  107.                 totalerror += abs(errorterm)
  108.         print totalerror
  109.  
  110.         # If the answer got worse by moving the points, we are done
  111.         if lasterror and lasterror<totalerror: break
  112.         lasterror = totalerror
  113.  
  114.         # Move each of the points by the learning rate times the gradient
  115.         for k in range(n):
  116.             # --------------------ADD DIMENSIONS HERE--------------------------
  117.             loc[k][0] -= rate*grad[k][0]
  118.             loc[k][1] -= rate*grad[k][1]
  119.             loc[k][2] -= rate*grad[k][2]
  120.             # -----------------------------------------------------------------
  121.     return loc
  122.  
  123.  
  124. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  125. #                         6 Dimensional VERSION
  126. # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  127.  
  128.  
  129. def scaledown_6D(data, distance=pearson, rate=0.01):
  130.     n = len(data)
  131.  
  132.     # The real distances between every pair of items
  133.     realdist = [[distance(data[i], data[j]) for j in range(n)] for i in range(0, n)]
  134.     outersum = 0.0
  135.  
  136.     # Randomly initialize the starting points of the locations in 2D
  137.     # ---------------------ADD DIMENSIONS HERE---------------------------------
  138.     loc = [[random.random(),random.random(),random.random(),random.random(),random.random(),random.random()] for i in range(n)]
  139.     # -------------------------------------------------------------------------
  140.     fakedist = [[0.0 for j in range(n)] for i in range(n)]
  141.    
  142.     lasterror = None
  143.     for m in range(0, 1000):
  144.         # Find projected distances
  145.         for i in range(n):
  146.             for j in range(n):
  147.                 fakedist[i][j]=sqrt(sum([pow(loc[i][x]-loc[j][x],2) for x in range(len(loc[i]))]))
  148.  
  149.         # Move points
  150.         # ---------------------ADD DIMENSIONS HERE-----------------------------
  151.         grad = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0] for i in range(n)]
  152.         # ---------------------------------------------------------------------
  153.         errorterm = 0
  154.         totalerror = 0
  155.         for k in range(n):
  156.             for j in range(n):
  157.                 if j == k: continue
  158.                 try:
  159.                     # The error is percent difference between the distances
  160.                     errorterm = (fakedist[j][k]-realdist[j][k])/realdist[j][k]
  161.  
  162.                     # Each point needs to be moved away from or towards the other
  163.                     # point in proportion to how much error it has
  164.                     # ---------------ADD DIMENSIONS HERE--------------------------
  165.                     grad[k][0] += ((loc[k][0]-loc[j][0])/fakedist[j][k])*errorterm          
  166.                     grad[k][1] += ((loc[k][1]-loc[j][1])/fakedist[j][k])*errorterm
  167.                     grad[k][2] += ((loc[k][2]-loc[j][2])/fakedist[j][k])*errorterm
  168.                     grad[k][0] += ((loc[k][3]-loc[j][3])/fakedist[j][k])*errorterm          
  169.                     grad[k][1] += ((loc[k][4]-loc[j][4])/fakedist[j][k])*errorterm
  170.                     grad[k][2] += ((loc[k][5]-loc[j][5])/fakedist[j][k])*errorterm
  171.                     # ------------------------------------------------------------
  172.  
  173.                     # Keep track of the total error
  174.                 except: pass
  175.                 totalerror += abs(errorterm)
  176.         print totalerror
  177.  
  178.         # If the answer got worse by moving the points, we are done
  179.         if lasterror and lasterror<totalerror: break
  180.         lasterror = totalerror
  181.  
  182.         # Move each of the points by the learning rate times the gradient
  183.         for k in range(n):
  184.             # --------------------ADD DIMENSIONS HERE--------------------------
  185.             loc[k][0] -= rate*grad[k][0]
  186.             loc[k][1] -= rate*grad[k][1]
  187.             loc[k][2] -= rate*grad[k][2]
  188.             loc[k][3] -= rate*grad[k][3]
  189.             loc[k][4] -= rate*grad[k][4]
  190.             loc[k][5] -= rate*grad[k][5]
  191.             # -----------------------------------------------------------------
  192.     return loc
Advertisement
Add Comment
Please, Sign In to add comment