簡體   English   中英

在 k 折交叉驗證 sklearn 中使用 MAPE

[英]Using MAPE in k fold cross validation sklearn

需要在交叉驗證中使用 MAPE 而不是 R2,只是想知道是否有任何簡單的等價物

score = cross_val_score(reg, X, y, scoring='neg_mean_absolute_percentage_error', cv=kfold)

我在 這里看到 sklearn 將 MAPE 列為評分方法,但是當我嘗試執行上述代碼時出現此錯誤

'neg_mean_absolute_percentage_error' is not a valid scoring value

(編輯:為 NMAPE 而不是 NMAE 編輯)

您可以在自定義 function 上使用sklearn.metrics.make_scorer來獲得您需要的東西。 下面是一些幫助代碼。

NMAPE 的定義是根據這篇文章中的公式定義的。 它只是下面等式的負數 -

在此處輸入圖像描述

import numpy as np
from sklearn.metrics import make_scorer
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score

#define custom function which returns single output as metric score
def NMAPE(y_true, y_pred): 
    return 1 - np.mean(np.abs((y_true - y_pred) / y_true)) * 100

#make scorer from custome function
nmape_scorer = make_scorer(NMAPE)

#dummy data
X = np.random.random((1000,10))
y = np.random.random(1000,)

#cross validation score on model
reg = LinearRegression()
cross_val_score(reg, X, y, scoring=nmape_scorer, cv=5)
array([-4453.67565485,  -148.201211  ,  -222.92820259,  -185.27855064,
        -657.27927049])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM