0% found this document useful (0 votes)
107 views

Introduction To Computation and Programming Using Python, Revised - Guttag, John v..302

The document describes code for a machine learning algorithm that clusters mammal data based on dental formulas. The code allows scaling of features, which normalizes the data values. Running the code with and without scaling produces different clustering results, with scaling improving the separation of mammals into herbivore, carnivore and omnivore groups.

Uploaded by

ZhichaoWang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Introduction To Computation and Programming Using Python, Revised - Guttag, John v..302

The document describes code for a machine learning algorithm that clusters mammal data based on dental formulas. The code allows scaling of features, which normalizes the data values. Running the code with and without scaling produces different clustering results, with scaling improving the separation of mammals into herbivore, carnivore and omnivore groups.

Uploaded by

ZhichaoWang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

285

Chapter 19. A Quick Look at Machine Learning

def readMammalData(fName, scale):


"""Assumes scale is a Boolean.

If True, features are scaled"""

#start of code is same as in previous version


#Use featureVals to build list containing the feature vectors
#for each mammal scale features, if needed
if scale:
for i in range(numFeatures):
featureVals[i] = scaleFeatures(featureVals[i])
#remainder of code is the same as in previous version
def testTeeth(numClusters, numTrials, scale):
features, labels, species =\
readMammalData('dentalFormulas.txt', scale)
examples = buildMammalExamples(features, labels, species)
#remainder of code is the same as in the previous version

Figure 19.15 Code that allows scaling of features


When we execute the code
print 'Cluster without scaling'
testTeeth(3, 20, False)
print '\nCluster with scaling'
testTeeth(3, 20, True)

it prints
Cluster without scaling
Cow, Elk, Moose, Sea lion
3 herbivores, 1 carnivores, 0 omnivores
Badger, Cougar, Dog, Fox, Guinea pig, Jaguar, Kangaroo, Mink, Mole,
Mouse, Porcupine, Pig, Rabbit, Raccoon, Rat, Red bat, Skunk, Squirrel,
Woodchuck, Wolf
4 herbivores, 9 carnivores, 7 omnivores
Bear, Deer, Fur seal, Grey seal, Human, Lion
1 herbivores, 3 carnivores, 2 omnivores
Cluster with scaling
Cow, Deer, Elk, Moose
4 herbivores, 0 carnivores, 0 omnivores
Guinea pig, Kangaroo, Mouse, Porcupine, Rabbit, Rat, Squirrel,
Woodchuck
4 herbivores, 0 carnivores, 4 omnivores
Badger, Bear, Cougar, Dog, Fox, Fur seal, Grey seal, Human, Jaguar,
Lion, Mink, Mole, Pig, Raccoon, Red bat, Sea lion, Skunk, Wolf
0 herbivores, 13 carnivores, 5 omnivores

You might also like