Sunday, 20 January 2013

Patchwork quilt in Python

Here's a fun python function to make a plot of [x,y] coordinate data as a patchwork quilt with user-defined 'complexity'. It initially segments the data into 'numsegs' number of clusters by a k-means algorithm. It then takes each segment and creates 'numclass' sub-clusters based on the euclidean distance from the centroid of the cluster. Finally, it plots each subcluster a different colour and prints the result as a png file.

inputs:
'data' is a Nx2 numpy array of [x,y] points
'numsegs' and 'numclass' are integer scalars. The greater these numbers the greater the 'complexity' of the output and the longer the processing time

import numpy as np
from scipy.cluster.vq import kmeans,vq
from pylab import *
def patchwork(data,numsegs,numclass):
# computing K-Means with K = numsegs
centroids,_ = kmeans(data,numsegs)
# assign each sample to a cluster
idx,_ = vq(data,centroids)
# loop through number of classes
for k in range(numsegs):
# get data in kth segment
datXY=data[idx==k,:]
# get distances of each point from centroid
dat1=np.sqrt((datXY[:,0]-centroids[k,0])**2 + (datXY[:,1]-centroids[k,1])**2)
# get numclass new centroids within kth segment
cents,_ = kmeans(dat1,numclass)
# assign each to subcluster
i,_ = vq(dat1,cents)
# loop through and plot each
for p in range(numclass):
plot(datXY[i==p,0],datXY[i==p,1],'.')
savefig("outputs"+str(numsegs)+'_'+str(numclass)+'_res.png')
close()
view raw patchwork.py hosted with ❤ by GitHub


Some example outputs in increasing complexity:

numsegs=10, numclass=5

numsegs=15, numclass=15

numsegs=20, numclass=50



No comments: