quick look at machine learning def dissimilarity(clusters)totdist for in clusterstotdist + variance(return totdist def trykmeans(examplesexampletypenumclustersnumtrialsverbose false)"""calls kmeans numtrials times and returns the result with the lowest dissimilarity""best kmeans(examplesexampletypenumclustersverbosemindissimilarity dissimilarity(bestfor trial in range( numtrials)clusters kmeans(examplesexampletypenumclustersverbosecurrdissimilarity dissimilarity(clustersif currdissimilarity mindissimilaritybest clusters mindissimilarity currdissimilarity return best figure finding the best -means clustering contrived example figure contains code that generatesplotsand clusters examples drawn from two distributions the function gendistributions generates list of examples with twodimensional feature vectors the values of the elements of these feature vectors are drawn from normal distributions the function plotsamples plots the feature vectors of set of examples it uses another pylab plotting feature that we have not yet seenthe function annotate is used to place text next to points on the plot the first argument is the textthe second argument the point with which the text is associatedand the third argument the location of the text relative to the point with which it is associated the function contrivedtest uses gendistributions to create two distributions of ten examples each with the same standard deviation but different meansplots the examples using plotsamplesand then clusters them using trykmeans