我与在scikit学习包中实现不同的分类试验,做一些NLP任务。 我用它来进行分类的代码如下
def train_classifier(self, argcands):
# Extract the necessary features from the argument candidates
train_argcands_feats = []
train_argcands_target = []
for argcand in argcands:
train_argcands_feats.append(self.extract_features(argcand))
train_argcands_target.append(argcand["info"]["label"])
# Transform the features to the format required by the classifier
self.feat_vectorizer = DictVectorizer()
train_argcands_feats = self.feat_vectorizer.fit_transform(train_argcands_feats)
# Transform the target labels to the format required by the classifier
self.target_names = list(set(train_argcands_target))
train_argcands_target = [self.target_names.index(target) for target in train_argcands_target]
# Train the appropriate supervised model
self.classifier = LinearSVC()
#self.classifier = SVC(kernel="poly", degree=2)
self.classifier.fit(train_argcands_feats,train_argcands_target)
return
def execute(self, argcands_test):
# Extract features
test_argcands_feats = [self.extract_features(argcand) for argcand in argcands_test]
# Transform the features to the format required by the classifier
test_argcands_feats = self.feat_vectorizer.transform(test_argcands_feats)
# Classify the candidate arguments
test_argcands_targets = self.classifier.predict(test_argcands_feats)
# Get the correct label names
test_argcands_labels = [self.target_names[int(label_index)] for label_index in test_argcands_targets]
return zip(argcands_test, test_argcands_labels)
如通过代码可以看出,我在测试支持向量机分类的两种实现方式:在LinearSVC和SVC与多项式核。 现在,我的“问题”。 当使用LinearSVC,我得到的,没有任何问题分类:测试实例标记有一些标签。 但是,如果我用多项式SVC,所有的测试实例加上相同的标签。 我知道,一个可能的解释是,简单地说,多项式SVC是不是合适的分类来使用我的任务,这很好。 我只是想确保我使用适当的多项式SVC。
感谢所有帮助/建议你可以给我。
更新后的答案中给出的建议,我已经改变了训练的分类做了以下代码:
# Train the appropriate supervised model
parameters = [{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['poly'], 'degree': [2]}]
self.classifier = GridSearchCV(SVC(C=1), parameters, score_func = f1_score)
现在,我得到了以下信息:
ValueError: The least populated class in y has only 1 members, which is too few. The minimum number of labels for any class cannot be less than k=3.
这有是与类的实例在我的训练数据的分布不均,对不对? 还是我打电话的程序是否有误?