我想创建一个多处理版本的文本分类代码,我发现here(其他很酷的东西)。我附加了下面的完整代码。 (!?) -python多处理 - 文本处理
我已经试过几件事情第一次尝试lambda函数,但抱怨不被序列化的,所以试图原代码的精简版:
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
p = Pool(2)
negfeats =[]
posfeats =[]
for f in negids:
words = movie_reviews.words(fileids=[f])
negfeats = p.map(featx, words) #not same form as below - using for debugging
print len(negfeats)
不幸即使这不工作 - 我得到以下痕迹:
File "/usr/lib/python2.6/multiprocessing/pool.py", line 148, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.6/multiprocessing/pool.py", line 422, in get
raise self._value
ZeroDivisionError: float division
任何想法我可能会做错什么?我应该使用pool.apply_async
代替(本身似乎并不能解决问题 - 但也许我正在吠叫错误的树)?
import collections
import nltk.classify.util, nltk.metrics
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
def evaluate_classifier(featx):
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
negfeats = [(featx(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(featx(movie_reviews.words(fileids=[f])), 'pos') for f in posids]
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
classifier = NaiveBayesClassifier.train(trainfeats)
refsets = collections.defaultdict(set)
testsets = collections.defaultdict(set)
for i, (feats, label) in enumerate(testfeats):
refsets[label].add(i)
observed = classifier.classify(feats)
testsets[observed].add(i)
print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)
print 'pos precision:', nltk.metrics.precision(refsets['pos'], testsets['pos'])
print 'pos recall:', nltk.metrics.recall(refsets['pos'], testsets['pos'])
print 'neg precision:', nltk.metrics.precision(refsets['neg'], testsets['neg'])
print 'neg recall:', nltk.metrics.recall(refsets['neg'], testsets['neg'])
classifier.show_most_informative_features()
这是我认为的问题 - 非常感谢! – malangi 2010-06-21 16:29:14
尝试在没有使用pool.map()的情况下运行它(例如,negfeats = [feat(x)for x in words])非常感谢。 – Lavanya 2012-01-06 07:39:42