Skip to content

Instantly share code, notes, and snippets.

Created September 30, 2018 13:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Constrained Lloyd Iteration
from lloyd import Field
from scipy.spatial import voronoi_plot_2d
import matplotlib.pyplot as plt
import numpy as np
import umap, os
def plot(vor, name, e=0.3):
'''Plot the Voronoi map of 2D numpy array X'''
plot = voronoi_plot_2d(vor, show_vertices=False, line_colors='y', line_alpha=0.5, point_size=5)
if not os.path.exists('plots'): os.makedirs('plots')
if len(str(name)) < 2: name = '0' + str(name)
plot.savefig( 'plots/' + str(name) + '.png' )
# get 1000 observations in two dimensions and plot their Voronoi map
X = np.random.rand(1000, 4)
X = umap.UMAP().fit_transform(X)
# run 20 iterations of Lloyd's algorithm
field = Field(X)
for i in range(20):
print(' * running iteration', i)
plot(field.voronoi, i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment