日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

《集体智慧编程》第六章

發布時間:2024/9/30 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《集体智慧编程》第六章 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.P126代碼
為了定義閾值,請修改初始化方法,在classifier中加入一個新的實例變量:

def __init__(self, getfeatures):classifier.__init__(self, getfeatures)self.thresholds = {}

這段代碼在做修改時,應直接在類classifier里的定義_ _ init _ _() 中加入最后一句代碼,前面的一句代碼就不要了。
修改后的_ _ init _ _()為:

class classifier:def __init__(self, getfeatures, filename = None):#count the number of feature or classify groupself.fc = {}#count the number of doc in each classificationself.cc = {}self.getfeatures = getfeatures#classifier.__init__(self, getfeatures)self.thresholds = {}

2.P131
在輸入代碼驗證時,如果輸入

>>> reload(docclass) <module 'docclass' from 'docclass.py'> >>> docclass.sampletrain(c1) >>> c1.classify('quick rabbit')

就會提示錯誤

>>> reload(docclass) <module 'docclass' from 'docclass.py'> >>> docclass.sampletrain(c1) >>> c1.classify('quick rabbit') Traceback (most recent call last):File "<stdin>", line 1, in <module>File "docclass.py", line 94, in classifyprobs[cat] = self.prob(item, cat) AttributeError: fisherclassifier instance has no attribute 'prob'

正確做法應該是在重新加載了文件后應該先重新對c1進行重新定義,就不會提示錯誤了。如下

>>> reload(docclass) <module 'docclass' from 'docclass.py'> >>> docclass.sampletrain(c1) >>> c1.classify('quick rabbit') Traceback (most recent call last):File "<stdin>", line 1, in <module>File "docclass.py", line 94, in classifyprobs[cat] = self.prob(item, cat) AttributeError: fisherclassifier instance has no attribute 'prob' >>> c1 = docclass.fisherclassifier(docclass.getwords) >>> docclass.sampletrain(c1) >>> c1.classify('quick rabbit') 'good' >>> c1.classify('quick money') 'bad' >>> c1.setminimum('bad', 0.8) >>> c1.classify('quick money') 'good' >>> c1.setminimum('good', 0.4) >>> c1.classify('quick money') 'good' >>>

3.P128
本頁中進行歸一化計算時,文章中的公式為:
cprob = clf/(clf+nclf)
但是在程序中卻是

p = clf / (freqsum)

我認為在計算nclf時就已經包括了clf,故不需要再加一次既可以實現歸一化,所以應該將文章中的公式改為:
cprob = clf/nclf
當然,加不加clf并不會影響最終結果,只會影響概率的數值,不會影響排行。
4.P129
文中的“包含單詞‘casino’的文檔是垃圾郵件的概率為0.9”一句有誤,經過計算,包含單詞‘casino’的文檔是垃圾郵件的概率應該為1.0
5.P137
書中代碼:

def entryfeatures(entry):splitter = re.compile('\\W*')f = {}#get words in title and sign ittitlewords = [s.lower() for s in splitter.split(entry['title']) if len(s) > 2 and len(s) < 20]for w in titlewords: f['Title: ' + w] = 1#get words in absrtactsummarywords = [s.lower() for s in splitter.split(entry['summary']) if len(s) > 2 and len(s) < 20]#count capitalize wordsuc = 0for i in range(len(summarywords)):w = summarywords[i]f[w] = 1if w.isupper(): uc += 1#words from absrtact as featuresif i < len(summarywords) - 1:twowords = ' '.join(summarywords[i : i + 1])f[twowords] = 1#keep names compile of artile's creater and publicorf['Publisher: ' + entry['publisher']] = 1#UPPERCASE is a virtual word, and it is used to aim at too many capitalize words existif float(uc) / len(summarywords) > 0.3: f['UPPERCASE'] = 1return f

統計大寫單詞的數量時,用到了前面提取到的summarywords變量,但是,在提取summarywords變量時,

summarywords = [s.lower() for s in splitter.split(entry['summary']) if len(s) > 2 and len(s) < 20]

可以看到,lower()函數已經把summarywords變量中的單詞全變成小寫的了。所以在統計后面的大寫單詞也就沒有意義了。所以我認為應該改為

summarywords = [s for s in splitter.split(entry['summary']) if len(s) > 2 and len(s) < 20]

請忽略我的渣英語。

總結

以上是生活随笔為你收集整理的《集体智慧编程》第六章的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。