【发布时间】:2019-01-18 03:46:33
【问题描述】:
我有点不确定如何将 SKLearn 的 GridSearchCV 应用于我与 NLTK 一起使用的随机森林。讨论了如何正常使用 GridSearchCV here,但是我的数据的格式与标准的 x 和 y 拆分不同。这是我的代码:
import nltk
import numpy as np
from nltk.classify.scikitlearn import SklearnClassifier
from nltk.corpus.reader import CategorizedPlaintextCorpusReader
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
reader_train = CategorizedPlaintextCorpusReader('C:/Users/User/Documents/Sentiment/machine_learning/amazon/amazon/', r'.*\.txt', cat_pattern=r'(\w+)/*', encoding='latin1')
documents_train = [ (list(reader_train.words(fileid)), category)
for category in reader_train.categories()
for fileid in reader_train.fileids(category) ]
all_words = []
for w in reader_train.words():
all_words.append(w.lower())
all_words = nltk.FreqDist(all_words)
word_features = list(all_words.keys())[:3500]
def find_features(documents):
words = set(documents)
features = {}
for w in word_features:
features[w] = (w in words)
return features
featuresets_train = [(find_features(rev), category) for (rev, category) in documents_train]
np.random.shuffle(featuresets_train)
training_set = featuresets_train[:1600]
testing_set = featuresets_train[:400]
RandFor = SklearnClassifier(RandomForestClassifier())
RandFor.train(training_set)
print("RandFor accuracy:", (nltk.classify.accuracy(RandFor, testing_set)) *100)
此代码不是生成传统的 x 和 y 拆分,而是生成一个元组列表,其中每个元组的格式如下:
({'i': True, 'am': False, 'conflicted': False ... 'about': False}, neg)
有没有办法将 GridSearchCV 应用于这种格式的数据?
【问题讨论】:
-
您正在将已经完美的 scikit learn 估计器(在这种情况下为
RandomForestClassifier)包装到与 nltk 兼容的估计器中。您需要使用RandomForestClassifier和GridSearchCV吗? -
GridSearchCV 不是必需品,我很乐意尝试其他优化算法。但是,它确实需要是 RandomForest。
标签: performance scikit-learn nltk random-forest hyperparameters