sklearn 交叉验证中的自定义评分函数

Tar*_*lia 8 python scikit-learn cross-validation

我想使用一个自定义函数,cross_validate它使用特定y_test的计算精度,这y_test与实际目标不同y_test

我尝试了几种方法,make_scorer但我不知道如何实际通过我的替代方法y_test

scoring = {'prec1': 'precision',
     'custom_prec1': make_scorer(precision_score()}

scores = cross_validate(pipeline, X, y, cv=5,scoring= scoring)
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议一种方法吗?

avc*_*zov 8

找到了这个方法。也许代码不是最佳的,对此感到抱歉。

好的,让我们开始:

import numpy as np
import pandas as pd

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import GridSearchCV
from sklearn.metrics.scorer import make_scorer

xTrain = np.random.rand(100, 100)
yTrain = np.random.randint(1, 4, (100, 1))

yTrainCV = np.random.randint(1, 4, (100, 1))

model = LogisticRegression()
Run Code Online (Sandbox Code Playgroud)

yTrainCV 将在此处用作自定义记分器。

def customLoss(xArray, yArray):
    indices = xArray.index.values
    tempArray = [1 if value1 != value2 else 0 for value1, value2 in zip(xArray.values, yTrainCV[[indices]])]

    return sum(tempArray)

scorer = {'main': 'accuracy',
          'custom': make_scorer(customLoss, greater_is_better=True)}
Run Code Online (Sandbox Code Playgroud)

这里有几个技巧:

  • 您需要传递给 customLoss 2 个值(来自模型的预测 + 实际值;不过我们不使用第二个参数)
  • 有一些游戏greater_is_betterTrue/False将返回正数或负数
  • 我们从 CV 中得到的指数 GridSearchCV

和...

grid = GridSearchCV(model,
                    scoring=scorer,
                    cv=5,
                    param_grid={'C': [1e0, 1e1, 1e2, 1e3],
                                'class_weight': ['balanced', None]},
                    refit='custom')

 grid.fit(xTrain, pd.DataFrame(yTrain))
 print(grid.score(xTrain, pd.DataFrame(yTrain)))
Run Code Online (Sandbox Code Playgroud)
  • 不要忘记refit参数GridSearchCV
  • 我们DataFrame在这里传递目标数组- 这将帮助我们检测自定义损失函数中的索引

  • 非常感谢你阿夫乔佐夫!!这太完美了,我非常感谢你的帮助。这太棒了,我希望他们能将其作为 make_scorer 的 sklearn 文档中的示例 (2认同)