我正在尝试从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)
但是我在fit函数上遇到以下错误:
File "C:\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py", line 1381, in fit
for label in iter_labels
File "C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 659, in __call__
self.dispatch(function, args, kwargs)
File "C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 406, in dispatch
job = ImmediateApply(func, args, kwargs)
File "C:\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 140, in __init__
self.results = func(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py", line 844, in _log_reg_scoring_path
scores.append(scoring(log_reg, X_test, y_test))
File "C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 1403, in log_loss
T = lb.fit_transform(y_true)
File "C:\Anaconda3\lib\site-packages\sklearn\base.py", line 433, in fit_transform
return self.fit(X, **fit_params).transform(X)
File "C:\Anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 315, in fit
self.y_type_ = type_of_target(y)
File "C:\Anaconda3\lib\site-packages\sklearn\utils\multiclass.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'参数,那么函数工作正常,所以它必须与我如何传递函数有关?
答案 0 :(得分:5)
它应该是scoring="log_loss"
,一个字符串,而不是一个函数。如果要传递可调用对象,则需要具有不同的接口。请参阅docs。可调用者应该采用三个参数:拟合估计量,得分数据(X)和已知真实目标(y)。
答案 1 :(得分:0)
要提供功能,您需要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)