Python Nltk朴素贝叶斯分类器内存问题
我的第一篇文章在这里! 我在使用nltk NaiveBayesClassifier时遇到问题。我有一套7000项的训练。每个培训项目都有2或3个世界的描述和代码。我想使用代码作为类的标签,使用描述的每个世界作为特性。 例如: 我叫奥巴马,001 训练集={[feature['My']=True,feature['name']=True,feature['is']=True,feature[Obama]=True],001} 不幸的是,使用这种方法,bayesclassifier.train的训练过程将使用高达3 GB的ram。。 我的方法有什么问题? 谢谢大家!Python Nltk朴素贝叶斯分类器内存问题,python,nltk,bayesian,classification,Python,Nltk,Bayesian,Classification,我的第一篇文章在这里! 我在使用nltk NaiveBayesClassifier时遇到问题。我有一套7000项的训练。每个培训项目都有2或3个世界的描述和代码。我想使用代码作为类的标签,使用描述的每个世界作为特性。 例如: 我叫奥巴马,001 训练集={[feature['My']=True,feature['name']=True,feature['is']=True,feature[Obama]=True],001} 不幸的是,使用这种方法,bayesclassifier.train的训
def document_features(document): # feature extractor
document = set(document)
return dict((w, True) for w in document)
...
words=set()
entries = []
train_set= []
train_length = 2000
readfile = open("atcname.pl", 'r')
t = readfile.readline()
while (t!=""):
t = t.split("'")
code = t[0] #class
desc = t[1] # description
words = words.union(s) #update dictionary with the new words in the description
entries.append((s,code))
t = readfile.readline()
train_set = classify.util.apply_features(document_features, entries[:train_length])
classifier = NaiveBayesClassifier.train(train_set) # Training
使用nltk.classify.apply_features,它返回一个类似于列表的对象,但不将所有功能集存储在内存中
从nltk.classify导入应用功能
更多信息和示例
无论如何,如果要将文件加载到内存中,则需要使用某种形式的延迟加载方法。将根据需要加载。
考虑查看使用NLTK.CythFy.Apple yType,它返回一个对象,该对象类似于列表,但不存储内存中的所有特征集。
从nltk.classify导入应用功能
更多信息和示例
无论如何,如果要将文件加载到内存中,则需要使用某种形式的延迟加载方法。将根据需要加载。
考虑一下谢谢你的忠告!我试过了,但在内存使用方面没有什么改进。使用train_set=classify.util.apply_features文档功能,条目[:1500],只有1500个条目,我使用1.7GB…你能发布你的train set的要点和你试图使用的确切语法吗。apply_功能通常非常有效。感谢您的建议!我试过了,但在内存使用方面没有什么改进。使用train_set=classify.util.apply_features文档功能,条目[:1500],只有1500个条目,我使用1.7GB…你能发布你的train set的要点和你试图使用的确切语法吗。apply_功能通常非常有效。