我正在嘗試使用自定義評分函數來計算具有ground truth和predict_proba y數組的多類日誌丟失。有沒有辦法讓GridSearchCV使用這種評分功能?使用自定義分數函數時,GridSearchCV可以使用predict_proba嗎?
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 with greater_is_better = False,並嘗試使用loss_func參數,但它似乎仍然使用.predict方法。我怎樣才能解決這個問題?
更新 - 如果我設置needs_threshold = True我得到一個多類錯誤。我正確地理解這種情況下不支持多類嗎?如果是的話,有人可以提出解決方法嗎?
謝謝。
你可以簡單地使用''scoring =「log_loss」''並且不需要打擾所有這些。不過,應該在這裏支持多類。 –
謝謝安德烈亞斯。我沒有看到log_loss作爲文檔中的得分選項,所以並不認爲它是內置的。但是知道這很棒!雖然我無法使用多種類來使用自定義函數。在製作記分員時,我們是否通過說need_threshold = true來打開predict_proba? – shankarmsy
是的。所有內置損失函數的列表在這裏:http://scikit-learn.org/dev/modules/model_evaluation.html#common-cases-predefined-values –