如何在scikit中的LogisticRegressionCV中实现不同的评分功能



我正试图从scikit learn 0.16中实现LogisticRegressionCV类,但很难将其用于不同的评分函数。医生说要传入sklearn.metrics中的一个评分函数,所以我尝试了以下代码:

from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics import log_loss
...
model_regression = LogisticRegressionCV(scoring=log_loss)
model_regression.fit(data_combined, winners_losers)

然而,我在拟合函数上得到以下错误:

  File "C:Anaconda3libsite-packagessklearnlinear_modellogistic.py", line 1381, in fit
    for label in iter_labels
  File "C:Anaconda3libsite-packagessklearnexternalsjoblibparallel.py", line 659, in __call__
    self.dispatch(function, args, kwargs)
  File "C:Anaconda3libsite-packagessklearnexternalsjoblibparallel.py", line 406, in dispatch
    job = ImmediateApply(func, args, kwargs)
  File "C:Anaconda3libsite-packagessklearnexternalsjoblibparallel.py", line 140, in __init__
    self.results = func(*args, **kwargs)
  File "C:Anaconda3libsite-packagessklearnlinear_modellogistic.py", line 844, in _log_reg_scoring_path
    scores.append(scoring(log_reg, X_test, y_test))
  File "C:Anaconda3libsite-packagessklearnmetricsclassification.py", line 1403, in log_loss
    T = lb.fit_transform(y_true)
  File "C:Anaconda3libsite-packagessklearnbase.py", line 433, in fit_transform
    return self.fit(X, **fit_params).transform(X)
  File "C:Anaconda3libsite-packagessklearnpreprocessinglabel.py", line 315, in fit
    self.y_type_ = type_of_target(y)
  File "C:Anaconda3libsite-packagessklearnutilsmulticlass.py", line 287, in type_of_target
    'got %r' % y)
ValueError: Expected array-like (array or non-string sequence), got LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr',
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0)

我在这里做错了什么?如果没有"scoring=log_loss"参数,那么函数就可以正常工作,所以它一定与我如何传递函数有关?

它应该是scoring="neg_log_loss",一个字符串,而不是函数。如果你想传递一个可调用的,它需要有一个不同的接口。请参阅文档。一个可调用函数应该有三个参数:拟合的估计器、要得分的数据(X)和已知的真实目标(y)。

要提供函数,需要make_scorer包装

import sklearn.metrics 
scorefunc = sklearn.metrics.accuracy_score  # Replace with custom
myscorer = sklearn.metrics.make_scorer(
         scorefunc,
         greater_is_better=True,
         needs_threshold=False # ... classification
)
LogisticRegressionCV(... scoring=myscorer,)

附带说明一下,如果sklearn的LogisticRegression主要是回归,并且有一个新的LogisticClassification类封装了这一点,那就太好了。目前不可能提供回归误差,也不可能提供实值目标。(AFAIK)

相关内容

  • 没有找到相关文章

最新更新