python - 学习 : Evaluate performance of each classifier of OneVsRestClassifier inside GridSearchCV

标签 python scikit-learn multilabel-classification grid-search

我正在使用 OneVsRestClassifierSVC 处理多标签分类,

from sklearn.datasets import make_multilabel_classification
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV

L=3
X, y = make_multilabel_classification(n_classes=L, n_labels=2,
                                  allow_unlabeled=True,
                                  random_state=1, return_indicator=True)    
model_to_set = OneVsRestClassifier(SVC())

parameters = {
    "estimator__C": [1,2,4,8],
    "estimator__kernel": ["poly","rbf"],
    "estimator__degree":[1, 2, 3, 4],
}

model_tunning = GridSearchCV(model_to_set, param_grid=parameters,
                             scoring='f1')

model_tunning.fit(X, y)

print model_tunning.best_score_
print model_tunning.best_params_

#0.855175822314
#{'estimator__kernel': 'poly', 'estimator__C': 1, 'estimator__degree': 3}

第一个问题

0.85 代表什么?它是 L 分类器中最好的分数还是平均分数?同样,参数集是否代表 L 分类器中的最佳得分者?

第二个问题

如果我是对的,OneVsRestClassifier 确实为每个标签构建了 L 分类器,因此可以期望访问或观察每个标签的性能.但是,在上面的示例中,如何从 GridSearchCV 对象中获取 L 分数呢?

编辑

为了简化问题并帮助自己更多地了解OneVsRestClassifier,在调整模型之前,

model_to_set.fit(X,y)
gp = model_to_set.predict(X) # the "global" prediction
fp = model_to_set.estimators_[0].predict(X) # the first-class prediction
sp = model_to_set.estimators_[1].predict(X) # the second-class prediction
tp = model_to_set.estimators_[2].predict(X) # the third-class prediction

可以证明gp.T[0]==fp, gp.T[1]==sp and gp.T[2 ]==tp。因此,“全局”预测只是“顺序”L 个体预测,第二个问题已解决

但令我困惑的是,如果一个元分类器 OneVsRestClassifier 包含 L 分类器,GridSearchCV 怎么可能只返回一个最好的分数,对应于 4*2*4 组参数中的一组,对于具有 L 分类器的元分类器 OneVsRestClassifier

如有任何评论,我们将不胜感激。

最佳答案

GridSearchCV 根据您的参数值创建网格,它将您的 OneVsRestClassifier 评估为原子分类器(即 GridSearchCV 不知道其中的内容元分类器)

第一:0.85 是 OneVsRestClassifier 在参数 ("estimator__C", "estimator__kernel", "estimator__degree"),这意味着 GridSearchCV 评估 16(同样,它仅在这种特殊情况下)可能的 OneVsRestClassifier每个都包含 L 个 SVC。一个 OneVsRestClassifier 中的所有 L 个分类器都具有相同的参数值(但它们每个都在学习从 L 个可能的 L 中识别它们自己的类)

即来自

{OneVsRestClassifier(SVC(C=1, kernel="poly", degree=1)),
 OneVsRestClassifier(SVC(C=1, kernel="poly", degree=2)),
 ...,
 OneVsRestClassifier(SVC(C=8, kernel="rbf", degree=3)),
 OneVsRestClassifier(SVC(C=8, kernel="rbf", degree=4))}

它选择得分最高的一个。

model_tunning.best_params_ 表示 OneVsRestClassifier(SVC()) 的参数,它将实现 model_tunning.best_score_。 您可以从 model_tunning.best_estimator_ 属性中获得最好的 OneVsRestClassifier

其次:没有现成的代码可以从 OneVsRestClassifier 获取 L 分类器的单独分数,但您可以查看 OneVsRestClassifier.fit< 的实现 方法,或采用此方法(应该有效 :)):

# Here X, y - your dataset
one_vs_rest = model_tunning.best_estimator_
yT = one_vs_rest.label_binarizer_.transform(y).toarray().T
# Iterate through all L classifiers
for classifier, is_ith_class in zip(one_vs_rest.estimators_, yT):
    print(classifier.score(X, is_ith_class))

关于python - 学习 : Evaluate performance of each classifier of OneVsRestClassifier inside GridSearchCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33783374/

相关文章:

python - 使用 PyTorch 的交叉熵损失函数是否需要 One-Hot 编码?

python - 使用机器学习预测行数

python - 嵌套列表中的缩进

python - 正则表达式查找一个或多个字符,包括中间带有句号、撇号或连字符的字符。如果最后一个符号仅在末尾出现一次,则没有最后一个符号

python - 导入错误 : DLL load failed: The specified module could not be found (sklearn)

python - Scikit Learn 多标签分类 : ValueError: You appear to be using a legacy multi-label data representation

python - 如何确保glob命令 'finds'是我需要聚合的文件?

python - 大多数类别的随机子抽样

python - Scikit learn 中的交叉验证与网格搜索

machine-learning - 使用weka对word2vec进行分类