我正在尝试使用OneVsRestClassifier对一组注释进行多标签分类。我的目标是将每条评论标记为可能的主题列表。我的自定义分类器使用 csv 中手动策划的单词列表及其相应的标签来标记每个评论。我正在尝试使用投票分类器将从单词袋技术和我的自定义分类器中获得的结果结合起来。这是我现有代码的一部分:
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.ensemble import VotingClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.grid_search import GridSearchCV
from sklearn.linear_model import SGDClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MultiLabelBinarizer
class CustomClassifier(BaseEstimator, ClassifierMixin):
def __init__(self, word_to_tag):
self.word_to_tag = word_to_tag
def fit(self, X, y=None):
return self
def predict_proba(self, X):
prob = np.zeros(shape=(len(self.word_to_tag), 2))
for index, comment in np.ndenumerate(X):
prob[index] = [0.5, 0.5]
for word, label in self.word_to_tag.iteritems():
if (label == self.class_label) and (comment.find(word) >= 0):
prob[index] = [0, 1]
break
return prob
def _get_label(self, ...):
# Need to have a way of knowing which label being classified
# by OneVsRestClassifier (self.class_label)
bow_clf = Pipeline([('vect', CountVectorizer(stop_words='english', min_df=1, max_df=0.9)),
('tfidf', TfidfTransformer(use_idf=False)),
('clf', SGDClassifier(loss='log', penalty='l2', alpha=1e-3, n_iter=5)),
])
custom_clf = CustomClassifier(word_to_tag_dict)
ovr_clf = OneVsRestClassifier(VotingClassifier(estimators=[('bow', bow_clf), ('custom', custom_clf)],
voting='soft'))
params = { 'estimator_weights': ([1, 1], [1, 2], [2, 1]) }
gs_clf = GridSearchCV(ovr_clf, params, n_jobs=-1, verbose=1, scoring='precision_samples')
binarizer = MultiLabelBinarizer()
gs_clf.fit(X, binarizer.fit_transform(y))
我的目的是使用这个手动策划的单词列表,这些单词由几种启发式方法获得,以改进仅应用单词袋获得的结果。目前,我正在努力寻找一种方法来知道在预测时正在分类哪个标签,因为使用OneVsRestClassifier为每个标签创建了CustomClassifier的副本。
我认为您正在寻找classes_
属性:http://scikit-learn.org/dev/modules/generated/sklearn.multiclass.OneVsRestClassifier.html#sklearn.multiclass.OneVsRestClassifier