【问题标题】:sklearn custom scorer multiple metrics at oncesklearn 自定义记分器一次多个指标
【发布时间】:2017-03-05 12:47:38
【问题描述】:

我有一个函数,它返回一个带有 多个 记分器的 Observation 对象 如何将其集成到自定义 sklearn 记分器中? 我将其定义为:

class Observation():
    def __init__(self):
        self.statValues = {}
        self.modelName = ""

    def setModelName(self, nameOfModel):
        self.modelName = nameOfModel

    def addStatMetric(self, metricName,metricValue):
        self.statValues[metricName] = metricValue

自定义分数定义如下:

def myAllScore(y_true, y_predicted):
    return Observation
my_scorer = make_scorer(myAllScore)

可能看起来像

{   'AUC_R': 0.6892943119440752,
    'Accuracy': 0.9815382629183745,
    'Error rate': 0.018461737081625407,
    'False negative rate': 0.6211453744493393,
    'False positive rate': 0.0002660016625103907,
    'Lift value': 33.346741089307166,
    'Precision J': 0.9772727272727273,
    'Precision N': 0.9815872808592603,
    'Rate of negative predictions': 0.0293063938288739,
    'Rate of positive predictions': 0.011361068973307943,
    'Sensitivity (true positives rate)': 0.3788546255506608,
    'Specificity (true negatives rate)': 0.9997339983374897,
    'f1_R': 0.9905775376404309,
    'kappa': 0.5384745595159575}

【问题讨论】:

    标签: python scikit-learn classification scoring


    【解决方案1】:

    事实上,这是可能的,如本分叉中所述:multiscorer

    为了完整起见,这里举个例子:

    from multiscorer.multiscorer import MultiScorer
    
    #Scikit's libraries for demonstration
    from sklearn.metrics import accuracy_score, precision_score
    from sklearn.model_selection import cross_val_score
    from numpy import average
    
    scorer = MultiScorer({
      'accuracy': (accuracy_score, {}),
      'precision': (precision_score, {'average': 'macro'})
    })
    
    ...
    
    cross_val_score(clf, X, target, scoring=scorer )
    
    results = scorer.get_results()
    
    for metric in results.keys():
      print("%s: %.3f" % (metric, average(results[metric])))
    

    【讨论】:

      【解决方案2】:

      简而言之:你不能。

      长版:记分器必须返回单个标量,因为它可以用于模型选择,并且通常用于比较对象。由于没有向量空间上的完整排序 - 您不能在记分器(或字典,但从数学角度来看它可能被视为向量)内返回向量。此外,即使是其他用例,例如进行交叉验证,也不支持将任意结构化对象作为返回值,因为它们尝试在值列表上调用 np.mean,并且此操作未针对 python 字典列表定义(其中你的方法返回)。

      您唯一能做的就是为您拥有的每个指标创建单独的记分器,并独立使用它们。

      【讨论】:

      • 我明白了。但是是否有可能类似于scikit-learn.org/stable/modules/generated/… 一次获得多个得分?
      • 你已经实现了这个,所以我不明白你的问题。分类报告不是记分器,你不能在记分器上下文中使用它。
      • 正确。我手动实现了循环的火车测试。但是,由于我想尝试堆叠和混合 (github.com/rushter/heamy),我开始转向 sklearn 管道,该管道很好地与记分器功能集成。
      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 2019-03-26
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2016-10-04
      • 2012-04-28
      • 1970-01-01
      相关资源
      最近更新 更多