Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- '''
- Created on 03-05-2011
- @author: jakub
- '''
- from base import *
- import sys, math, random
- def kMeans(distanceObject, points, k, cutoff):
- randomPoints = random.sample(points, k)
- clusters = []
- for p in randomPoints:
- clusters.append(CCluster([p]))
- while True:
- lists = []
- for c in clusters:
- lists.append([])
- for p in points:
- nearestCluster = distanceObject.getDistanceBetweenPoints(p, distanceObject.getCentroid(clusters[0]))
- index = 0
- for i in range(len(clusters[1:])):
- distance = distanceObject.getDistanceBetweenPoints(p, distanceObject.getCentroid(clusters[i + 1]))
- if distance < nearestCluster:
- nearestCluster = distance
- index = i + 1
- lists[index].append(p)
- bigestChange = 0.0
- for i in range(len(clusters)):
- shift = clusters[i].update(lists[i])
- bigestChange = max(bigestChange, shift)
- if bigestChange < cutoff: break
- return clusters
- def makeRandomPunkt(n, lower, upper):
- coords = []
- for i in range(n): coords.append(random.uniform(lower, upper))
- return CPoint(coords)
- def main(args):
- input = [x.split(',') for x in open('input').read().split('\n')]
- print input
- punkty = []
- for i in input:
- punkty.append(CPoint(i[:-1]))
- klastry = kSrodki(punkty, len(set([x[0] for x in input])), 0.5)
- print "\nPunkty:"
- for p in punkty: print "P:", p
- print "\nklastry:"
- for c in klastry: print "C:", len(c.punkty), c
- if __name__ == "__main__": main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement