Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clustering/kmeans.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (30 sloc)
1.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from pyinclude.draw import * | |
from pyinclude.point import * | |
K = 3 | |
POINTS = 100 | |
RANGE = 640 | |
if __name__ == "__main__": | |
points = [ Point() for i in range(POINTS) ] | |
centroids = [ Centroid() for i in range(K) ] | |
maxIterations = 10 | |
# set random point positions | |
# generate K initial points and then position the rest of the | |
# points around the initial K | |
assert( len(points) >= len(centroids) ) | |
for p in points[:len(centroids)]: | |
p.randomise( RANGE ) | |
for i in range( len(centroids), len(points) ): | |
points[i].randomise( RANGE, points[i%len(centroids)] ) | |
# set random starting positions for the centroids | |
for c in centroids: | |
c.randomise( RANGE ) | |
print( c ) | |
# clustering iterations | |
for i in range( maxIterations ): | |
# kmeans | |
# display centroid positions | |
print( f"Iteration {i}" ) | |
for c in centroids: | |
print( c ) | |
draw( points, centroids, RANGE, RANGE, f"{i:0{int(math.log(maxIterations,10)+1)}d}_clusters.bmp" ) |