2012-03-15 8 views
1

私の最初の投稿はここに! nltk NaiveBayesClassifierの使用に問題があります。私は7000項目のトレーニングセットを持っています。各トレーニング項目には、2つまたは3つの世界の説明とコードがあります。私はクラスのラベルと説明の各世界のコードをフィーチャーとして使用したいと考えています。 例:Nltkナイーブベイジアンクラシファイアメモリの問題

、001 ...

= {[機能[ '私']がTrue、機能[ '名前']は真、機能[を」= =トレーニングセット "私の名前はオバマ氏です" [] = True、feature [Obama] = True]、001}

残念ながら、この方法を使用すると、トレーニング手順NaiveBayesClassifier.trainは最大3GBのRAMを使用します。 私のアプローチでは何が間違っていますか? ありがとうございました!リストのように動作しますが、メモリ内のすべての機能セットが格納されていないオブジェクトを返します

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 

答えて

5

使用nltk.classify.apply_features

from nltk.classify import apply_features

詳細情報および例here

あなたはメモリにとにかくファイルをロードしている、あなたは、遅延ロードの方法のいくつかのフォームを使用する必要があります。必要に応じてロードされます。 探してみるthis

+0

アドバイスありがとうございます!私は試みたが、私はメモリの使用法のいくつかの改善はありません。 train_set = classify.util.apply_features(document_features、entries [:1500])を使用すると、1500アイテムしか使用できません。私は1.7GBを使用します。 – Marco

+0

あなたの列車セットの要点と使用しようとしている構文。 apply_featuresは通常非常にうまく機能します。 – subiet

+0

ありがとう..更新。 – Marco