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

sha*_*msy 5 scikit-learn

我正在尝试使用自定义评分函数来计算多类对数损失与真实值和 Predict_probay 数组。有没有办法让 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
Run Code Online (Sandbox Code Playgroud)

我看到有多个选项,score_func、loss_func 和 make_scorer。我尝试使用 make_scorer 和 Greater_is_better=False 并尝试了 loss_func 参数,但似乎仍然使用 .predict 方法。我该如何解决这个问题?

更新 - 如果我设置 need_threshold=True 我会收到多类错误。我对这种情况下不支持多类的理解是否正确?如果是,有人可以建议解决方法吗?

谢谢。

Don*_*sto 1

这个问题的最佳答案: 通过 sklearn.metrics.make_scorer 将估计器传递给自定义得分函数

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

clf.predict_proba(X)[:,1]

这满足了我的需求(标准化基尼系数)。由于某种原因,我无法让 sklearnmetrics.make_scorer与需要概率的自定义函数一起使用。