同樣是使用NLTK來實現,NLTK的安裝之前博文有說過在此不再贅述。
http://www.cnblogs.com/mansiisnam/p/5301892.html
之前在網上找了很多實現最大熵+LBFGS的資料,也看了大牛自己寫代碼實現出來的博客。但是本人的基礎薄弱難以對大牛的代碼進行修改以達到自己的預期,所以就想着使用工具包實現。windows 使用NLTK的MEGAM比較麻煩,博主之前找了很多資料沒有實現有興趣的可以看這個鏈接(http://www.cnblogs.com/createMoMo/archive/2013/05/15/3079290.html)
博主使用的linux 實現的方法特別簡單
只要在你項目中根目錄加上megam.opt這個文件,如圖
這個文件在官網有下載但是官網給出的是32位的。如果你是64位的則需要通過OCaml 將其編譯成64位。然后在代碼上添加
nltk.config_megam('./megam.opt')這一句即可。整個程序即可運行。附上32 和64位megam.opt 和data文件下載鏈接:
megam:http://files.cnblogs.com/files/mansiisnam/MEGAM.zip
上述文件經測試在unbantu 和centos 7.0 64 上可用。
data:http://files.cnblogs.com/files/mansiisnam/data.zip
代碼如下:python 2.7
import sys; import scipy; import nltk; from nltk.classify import MaxentClassifier nltk.config_megam('./megam.opt') def load_data(filename): for line in open(filename, mode='r'): sample = line.strip().split("\t"); y = sample[0]; reason1={'outlook':sample[1],'temperature':sample[2],'humidity':sample[3],'windy':sample[4]}; if(y=='no'): train.append((reason1,'x')); elif(y=='yes'): train.append((reason1,'y')) ; def print_maxent_test_header(): print(' '*11+''.join([' test[%s] ' % i for i in range(len(test))])) print(' '*11+' p(x) p(y)'*len(test)) print('-'*(11+15*len(test))) def test_maxent(algorithm): print "%11s" % algorithm , " " try: classifier = MaxentClassifier.train( train, algorithm, trace=0, max_iter=1000) except Exception as e: print('Error: %r' % e) return for featureset in test: pdist = classifier.prob_classify(featureset) print "%8.15f" % pdist.prob('x'), "%6.15f" % pdist.prob('y') , print " " if __name__ == '__main__' : train=[]; load_data('data.txt'); test1={'outlook':'sunny','temperature':'hot','humidity':'high','windy':'FALSE'}; test2={'outlook':'overcast','temperature':'hot','humidity':'high','windy':'FALSE'}; test3={'outlook':'sunny','temperature':'cool','humidity':'high','windy':'TRUE'}; test=[]; test.append(test1); test.append(test2); test.append(test3); print_maxent_test_header(); test_maxent('GIS'); test_maxent('IIS'); test_maxent('MEGAM'); sys.exit(0);