【问题标题】:GridSearchCV for multi-label classification for each label separatelyGridSearchCV 对每个标签分别进行多标签分类
【发布时间】:2015-07-03 01:50:34
【问题描述】:

我正在使用 scikit learn 进行多标签分类。我使用 RandomForestClassifier 作为基本估计器。我想使用 GridSearchCV 为每个标签优化它的参数。目前我正在通过以下方式进行操作:

from sklearn.ensemble import RandomForestClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.grid_search import GridSearchCV

parameters = {
  "estimator__n_estimators": [5, 50, 200],
  "estimator__max_depth" : [None, 10,20],
  "estimator__min_samples_split" : [2, 5, 10],
}
model_to_tune = OneVsRestClassifier(RandomForestClassifier(random_state=0,class_weight='auto'))
model_tuned = GridSearchCV(model_to_tune, param_grid=params, scoring='f1',n_jobs=2)
print model_tuned.best_params_
{'estimator__min_samples_split': 10, 'estimator__max_depth': None, 'estimator__n_estimators': 200}

这些是考虑所有标签时给出最佳 f1 分数的参数。我想分别为每个标签找到参数。有没有内置函数可以做到这一点?

【问题讨论】:

  • 多标签强调mutually inclusive,以便观察可以同时是多个类的成员。如果您想为每个标签训练单独的分类器,那么对于该特定分类器,每个观察只能是 ONE 类(one-vs-the-rest)的成员,即mutually exclusive。您想要的方法似乎与您实际的多标签观察相矛盾,我认为 scikit-learn 中没有实现。
  • @JianxunLi 嗨,我想知道 `OneVsRestClassifier` 所做的是否只是多标签文献中的二元相关性。如果是这样,不考虑标签之间的交互确实是使用二元相关性的主要缺点,因此当您“手动”训练单个分类器与使用 OneVsRestClassifier 时,它应该是相同的。

标签: python scikit-learn


【解决方案1】:

这并不难,虽然它不是内置的,我不确定我是否理解你为什么想要这样做。

只需像这样预处理您的数据:

for a_class in list_of_unique_classes:
    y_this_class = (y_all_class==a_class)
    model_to_tune = RandomForestClassifier(random_state=0,class_weight='auto')
    model_tuned = GridSearchCV(model_to_tune, param_grid=params, scoring='f1',n_jobs=2)
    model_tuned.fit( X, y_this_class )

    # Save the best parameters for this class

(另外,请注意 f1 分数,它不能很好地描述倾斜数据集的分类器性能。您想使用 ROC 曲线和/或informedness)。

【讨论】:

  • 我尝试使用知情来选择模型。但是当我将模型应用于测试数据时,对于某些类,模型将所有观察值预测为 1,而实际上该类只有大约 5% 的 1。我在使用 f1 分数时没有遇到这个问题。为什么会发生这种情况?
猜你喜欢
  • 2013-11-18
  • 2016-09-26
  • 2021-11-21
  • 2019-09-24
  • 2016-05-09
  • 2017-06-03
  • 2016-01-18
  • 1970-01-01
相关资源
最近更新 更多