Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Created on 03-05-2011
  4.  
  5. @author: jakub
  6. '''
  7. from base import *
  8. import sys, math, random
  9.  
  10. def kMeans(distanceObject, points, k, cutoff):
  11. randomPoints = random.sample(points, k)
  12. clusters = []
  13. for p in randomPoints:
  14. clusters.append(CCluster([p]))
  15. while True:
  16. lists = []
  17. for c in clusters:
  18. lists.append([])
  19. for p in points:
  20. nearestCluster = distanceObject.getDistanceBetweenPoints(p, distanceObject.getCentroid(clusters[0]))
  21. index = 0
  22. for i in range(len(clusters[1:])):
  23. distance = distanceObject.getDistanceBetweenPoints(p, distanceObject.getCentroid(clusters[i + 1]))
  24. if distance < nearestCluster:
  25. nearestCluster = distance
  26. index = i + 1
  27. lists[index].append(p)
  28. bigestChange = 0.0
  29. for i in range(len(clusters)):
  30. shift = clusters[i].update(lists[i])
  31. bigestChange = max(bigestChange, shift)
  32. if bigestChange < cutoff: break
  33. return clusters
  34.  
  35. def makeRandomPunkt(n, lower, upper):
  36. coords = []
  37. for i in range(n): coords.append(random.uniform(lower, upper))
  38. return CPoint(coords)
  39. def main(args):
  40. input = [x.split(',') for x in open('input').read().split('\n')]
  41. print input
  42. punkty = []
  43. for i in input:
  44. punkty.append(CPoint(i[:-1]))
  45. klastry = kSrodki(punkty, len(set([x[0] for x in input])), 0.5)
  46. print "\nPunkty:"
  47. for p in punkty: print "P:", p
  48. print "\nklastry:"
  49. for c in klastry: print "C:", len(c.punkty), c
  50. if __name__ == "__main__": main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement