Created
September 30, 2018 13:33
-
-
Save duhaime/347e1061d51139eb77ab6bf65b11debc to your computer and use it in GitHub Desktop.
Constrained Lloyd Iteration
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
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) | |
plot.set_figheight(14) | |
plot.set_figwidth(20) | |
plt.axis([field.bb[0]-e, field.bb[1]+e, field.bb[2]-e, field.bb[3]+e]) | |
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 | |
np.random.seed(1144392507) | |
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) | |
field.relax() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment