我目前正在尝试将文本分为7个类别。到目前为止,我已经能够通过多数投票(带有SVM,多项式NB,随机森林和KNN)达到90%的准确率)。
我想通过使用词嵌入来尝试进一步提高此精度,从而减少样本的尺寸。我使用gensim word2vec创建我的模型以及停用词和令牌生成器的NLTK列表:
with open('data.pkl','r') as f:
corpus=pickle.load(f)
with open('targets.pkl','r') as f:
targets=pickle.load(f)
tokenized_corpus=[word_tokenize(anomaly) for anomaly in corpus]
stop_words=['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
for i,anomaly in enumerate(tokenized_corpus):
for w in anomaly:
if w in stop_words:
tokenized_corpus[i].remove(w)
model = gensim.models.Word2Vec(
tokenized_corpus,
window=5,
size=100)
model.train(tokenized_corpus, total_examples=len(corpus), epochs=10)
模型看起来不错,当我使用单词之间的相似性时,我得到令人满意的结果。
我使用此类来获取样本的均值表示形式
class MeanEmbeddingVectorizer(object):
def __init__(self, word2vec):
self.word2vec = word2vec
if len(word2vec)>0:
self.dim=len(word2vec[next(iter(word2vec))])
else:
self.dim=0
def fit(self, X, y):
return self
def transform(self, X):
return np.array([
np.mean([self.word2vec[w] for w in words if w in self.word2vec]
or [np.zeros(self.dim)], axis=0) #moyenne des vecteurs des mots (a.word2vec[w])[ou 0 si il connait pas le mot] composant un élément de X
for words in X
])
和
w2v = dict(zip(model.wv.index2word, model.wv.syn0))
a=MeanEmbeddingVectorizer(w2v)
X_transformed=a.transform(tokenized_corpus)
最后,我建立一个通用的sklearn管道,并让sklearn对数据执行GridSearchCV:
test_param_n_estimators=[i for i in range(1,50)]
parameters = {'clf2__n_estimators': test_param_n_estimators}
pip=Pipeline([['clf2',RandomForestClassifier()]])
gs_clf2 = GridSearchCV(pip, parameters,verbose=10,n_jobs=2)
gs_clf2 = gs_clf2.fit(X_transformed, targets)
print(gs_clf2.best_score_)
print(gs_clf2.best_params_)
问题是我获得随机精度得分(大约0.5)。
也许降维并不总是能够提高精度,但我认为我听不懂或做错了什么,您是否知道出什么问题了?
提前谢谢
答案 0 :(得分:0)
您似乎没有以正确的方式从模型中提取向量。 您应该使用:
np.mean([model.wv[w] for w in words if w in model.wv.vocab]
or [np.zeros(model.dim)], axis=0)
此外,自从您将tokenized_corpus
传递到Word2Vec模型constructor
和model.train
以来,您都在训练模型两次。