我试图在GridSearchCV
的scoring
参数中使用log_loss
参数来调整这个多类(6个类别)的分类器。我不明白如何给它一个label
参数。即使我给它sklearn.metrics.log_loss
,它在交叉验证的每次迭代中都会发生变化,所以我不明白如何给它labels
参数?
我使用的是Python v3.6
和Scikit-Learn v0.18.1
如何使用GridSearchCV
和log_loss
来调整多类模型?
我的类表示:
1 312 183 284 195 176 22Name: encoding, dtype: int64
我的代码:
param_test = {"criterion": ["friedman_mse", "mse", "mae"]}gsearch_gbc = GridSearchCV(estimator = GradientBoostingClassifier(n_estimators=10), param_grid = param_test, scoring="log_loss", n_jobs=1, iid=False, cv=cv_indices)gsearch_gbc.fit(df_attr, Se_targets)
这是错误的结尾部分,完整的错误在这里 https://pastebin.com/1CshpEBN:
ValueError: y_true contains only one label (1). Please provide the true labels explicitly through the labels argument.
更新: 只需使用这个来基于@Grr创建评分器
log_loss_build = lambda y: metrics.make_scorer(metrics.log_loss, greater_is_better=False, needs_proba=True, labels=sorted(np.unique(y)))
回答:
我的假设是你的数据分割中y_true只有一个类标签。虽然这根据你发布的分布来看似乎不太可能,但我认为这是可能的。虽然我之前没有遇到过这种情况,但在[sklearn.metrics.log_loss
](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html)中,如果所有标签都相同,则需要提供label
参数。文档这一部分的措辞也表明,如果不传递labels
,该方法会假设为二分类。
正如你正确假设的,你应该传递log_loss
作为scorer=sklearn.metrics.log_loss(labels=your_labels)