The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

aroc725 (22) [Avatar] Offline
#1
I tried running the code on page 70, but I'm getting incorrect values for 'p0V' and 'p1V':

>>> from numpy import *

>>> reload(bayes)
<module 'bayes' from 'bayes.pyc'>

>>> listOfPosts,listClasses = bayes.loadDataSet()

>>> myVocabList = bayes.createVocabList(listOfPosts)

>>> myVocabList
['cute', 'love', 'help', 'garbage', 'quit', 'I', 'problems', 'is', 'park', 'stop
', 'flea', 'dalmation', 'licks', 'food', 'not', 'him', 'buying', 'posting', 'has
', 'worthless', 'ate', 'to', 'maybe', 'please', 'dog', 'how', 'stupid', 'so', 't
ake', 'mr', 'steak', 'my']

>>> trainMat = []

>>> for postInDoc in listOfPosts:
... trainMat.append(bayes.setOfWords2Vec(myVocabList, postInDoc))
...

>>> p0V,p1V,pAb = bayes.trainNB0(trainMat,listClasses)

>>> pAb
0.5

>>> p0V
array([-2.56494936, -2.56494936, -2.56494936, -3.25809654, -3.25809654,
-2.56494936, -2.56494936, -2.56494936, -3.25809654, -2.56494936,
-2.56494936, -2.56494936, -2.56494936, -3.25809654, -3.25809654,
-2.15948425, -3.25809654, -3.25809654, -2.56494936, -3.25809654,
-2.56494936, -2.56494936, -3.25809654, -2.56494936, -2.56494936,
-2.56494936, -3.25809654, -2.56494936, -3.25809654, -2.56494936,
-2.56494936, -1.87180218])

>>> p1V
array([-3.04452244, -3.04452244, -3.04452244, -2.35137526, -2.35137526,
-3.04452244, -3.04452244, -3.04452244, -2.35137526, -2.35137526,
-3.04452244, -3.04452244, -3.04452244, -2.35137526, -2.35137526,
-2.35137526, -2.35137526, -2.35137526, -3.04452244, -1.94591015,
-3.04452244, -2.35137526, -2.35137526, -3.04452244, -1.94591015,
-3.04452244, -1.65822808, -3.04452244, -2.35137526, -3.04452244,
-3.04452244, -3.04452244])
>>>
peter.harrington (82) [Avatar] Offline
#2
Re: Section 4.5.2, page 70
There are two lines on page 71 3/4 of the way down the page that alter the results of trainNB0().

If you take the code from the repo and run the example the results you got were correct, and it has these changes built in.


If you type in everything from the book you will get results like those shown on page 71.
402196 (1) [Avatar] Offline
#3
Yeah. The result of sample code from me and you are the same