def buildmammalexamples(featurelistlabellistspeciesnames)examples [for in range(len(speciesnames))features pylab array(featurelist[ ]example example(speciesnames[ ]featureslabellist[ ]examples append(examplereturn examples def testteeth(numclustersnumtrials)featureslabelsspecies readmammaldata('dentalformulas txt'examples buildmammalexamples(featureslabelsspeciesbestclustering =trykmeans(examplesexamplenumclustersnumtrialsfor in bestclusteringnames 'for in members()names + getname('print '\ 'names[:- #remove trailing comma and space herbivorescarnivoresomnivores for in members()if getlabel(= herbivores + elif getlabel(= carnivores + elseomnivores + print herbivores'herbivores,'carnivores'carnivores,',omnivores'omnivoresfigure clustering animals when we executed the code testteeth( it printed cowelkmoosesea lion herbivores carnivores omnivores badgercougardogfoxguinea pigjaguarkangaroominkmolemouseporcupinepigrabbitraccoonratred batskunksquirrelwoodchuckwolf herbivores carnivores omnivores beardeerfur sealgrey sealhumanlion herbivores carnivores omnivores so much for our conjecture that the clustering would be related to the eating habits of the various species cursory inspection suggests that we have clustering totally dominated by the weights of the animals the problem is that the range of weights is much larger than the range of any of the other features thereforewhen the euclidean distance between examples is computedthe only feature that truly matters is weight we encountered similar problem in section when we found that the distance between animals was dominated by the number of legs we solved the problem there by turning the number of legs into binary feature (legged or leglessthat was fine for that data setbecause all of the animals happened to have either zero or four legs herehoweverthere is no way to binarize weight without losing great deal of information