Scikit Learn を使用して Python でランダム フォレストを使用するのに苦労しています。私の問題は、テキスト分類 (3 つのクラス - ポジティブ/ネガティブ/ニュートラル) に使用し、抽出する特徴は主に単語/ユニグラムであるため、これらを数値特徴に変換する必要があることです。DictVectorizer
でそれを行う方法を見つけましたfit_transform
:
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
rf = RandomForestClassifier(n_estimators = 100)
trainFeatures1 = vec.fit_transform(trainFeatures)
# Fit the training data to the training output and create the decision trees
rf = rf.fit(trainFeatures1.toarray(), LabelEncoder().fit_transform(trainLabels))
testFeatures1 = vec.fit_transform(testFeatures)
# Take the same decision trees and run on the test data
Output = rf.score(testFeatures1.toarray(), LabelEncoder().fit_transform(testLabels))
print "accuracy: " + str(Output)
私の問題は、fit_transform
メソッドが約 8000 のインスタンスを含むトレーニング データセットで動作していることですが、テスト セットを約 80000 のインスタンスである数値機能にも変換しようとすると、次のようなメモリ エラーが発生します。
testFeatures1 = vec.fit_transform(testFeatures)
File "C:\Python27\lib\site-packages\sklearn\feature_extraction\dict_vectorizer.py", line 143, in fit_transform
return self.transform(X)
File "C:\Python27\lib\site-packages\sklearn\feature_extraction\dict_vectorizer.py", line 251, in transform
Xa = np.zeros((len(X), len(vocab)), dtype=dtype)
MemoryError
何が原因で、回避策はありますか? どうもありがとう!