首页 文章

使用自定义分数函数时,GridSearchCV可以使用predict_proba吗?

提问于
浏览
4

我正在尝试使用自定义评分函数来计算具有基础事实和predict_proba y数组的多类日志丢失 . 有没有办法让GridSearchCV使用这个评分函数?

def multiclass_log_loss(y_true, y_pred):
Parameters
----------
y_true : array, shape = [n_samples]
        true class, intergers in [0, n_classes - 1)
y_pred : array, shape = [n_samples, n_classes]

Returns
-------
loss : float
"""
eps=1e-15
predictions = np.clip(y_pred, eps, 1 - eps)

# normalize row sums to 1
predictions /= predictions.sum(axis=1)[:, np.newaxis]

actual = np.zeros(y_pred.shape)
n_samples = actual.shape[0]
actual[np.arange(n_samples), y_true.astype(int)] = 1
vectsum = np.sum(actual * np.log(predictions))
loss = -1.0 / n_samples * vectsum
return loss

我看到有多个选项,score_func,loss_func和make_scorer . 我尝试使用make_scorer和greater_is_better = False并尝试了loss_func参数,但似乎仍然使用.predict方法 . 我怎样才能解决这个问题?

更新 - 如果我设置needs_threshold = True我得到一个多类错误 . 我是否正确理解这种情况下不支持多类?如果是,有人可以提出解决方法吗?

谢谢 .

1 回答

  • 0

    这个问题的最佳答案:Pass estimator to custom score function via sklearn.metrics.make_scorer

    可能有你需要的东西 . 可以定义一个记分器,该记分器将分类器 clf ,特征数组 X 和目标 y_true 作为参数,并将 clf.predict_proba() 方法的结果提供给返回错误的评分函数 . 作为提示,对于二进制分类,您可能需要使用

    clf.predict_proba(X)[:,1]

    这符合我的需求(标准化的基尼评分) . 出于某种原因,我无法使用需要概率的自定义函数 . [2605879_ s metrics.make_scorer ] .

相关问题