为什么sklearn中的cross_val_score会翻转指标的值?
Why does cross_val_score in sklearn flip the value of the metric?
我正在拟合来自 sklearn
的模型。
LogisticRegressionCV(
solver="sag", scoring="neg_log_loss", verbose=0, n_jobs=-1, cv=10
)
拟合导致 model.score
(在训练集上)为 0.67 并发生变化。由于无法(或者我不知道如何)访问作为模型拟合的一部分执行的交叉验证的结果,我 运行 作为对同一模型的单独交叉验证
cross_val_score(model, X, y, cv=10, scoring="neg_log_loss")
这是一个负数数组return
[-0.69517214 -0.69211235 -0.64173978 -0.66429986 -0.77126878 -0.65127196
-0.66302393 -0.65916281 -0.66893633 -0.67605681]
如果标志被翻转,它似乎在与训练分数兼容的范围内。
我已经阅读了 issue 中关于 cross_val_score 翻转给定评分函数的符号的讨论,解决方案似乎引入了 neg_*
指标以使这种翻转变得不必要,我正在使用neg_log_loss
。该问题讨论了 mse
,但论点似乎也适用于 log_loss
。有没有办法让 cross_val_score
return 与其参数中指定的指标相同?或者这是我应该提交的错误?或者是我的误解,cross_val_score
?
仍然可以预期更改符号
我希望这对 SO 来说是一个足够具体的问题。 Sklearn
开发人员将用户重定向到 SO,以解决不是明确的错误报告或功能需求的问题。
在评论中为每个请求添加最少的重现代码(sklearn v 0.19.1 python 2.7):
from numpy.random import randn, seed
from sklearn.linear_model import LogisticRegressionCV
from sklearn.model_selection import cross_val_score
seed (0)
X = randn(100,2)
y = randn(100)>0
model = LogisticRegressionCV(
solver="sag", scoring="neg_log_loss", verbose=0, n_jobs=-1, cv=10
)
model.fit(X=X, y=y)
model.score(X,y)
cross_val_score(model, X, y, cv=10, scoring="neg_log_loss")
使用此代码,它看起来不再像一个简单的指标翻转符号。分数的输出为 0.59,交叉验证分数的输出为 array([-0.70578452, -0.68773683, -0.68627652, -0.69731349, -0.69198876, -0.70089103, -0.69476663, -0.68279466, -0.70066003, -0.68532253])
。
注意:在 Vivek Kumar and piccolbo.
的富有成果的评论线程后编辑
关于 LinearRegressionCV score
方法的奇怪结果
您发现了一个错误,已在版本 0.20.0
中修复。
来自changelog:
Fix: Fixed a bug in linear_model.LogisticRegressionCV where the score method always computes accuracy, not the metric given by the scoring parameter. #10998 by Thomas Fan.
此外,sklearn 的 0.19 LogisticRegressionCV documentation 说:
score(X, y, sample_weight=None)
Returns the mean accuracy on the given test data and labels.
从版本 0.20.0
开始,docs 更新了错误修复:
score(X, y, sample_weight=None)
Returns the score using the scoring option on the given test data and labels.
关于cross_val_score
中返回的负值
cross_val_score
翻转 error
或 loss
指标的结果值,同时保留 score
指标的符号。来自 documentation:
All scorer objects follow the convention that higher return values are better than lower return values. Thus metrics which measure the distance between the model and the data, like metrics.mean_squared_error, are available as neg_mean_squared_error which return the negated value of the metric.
我正在拟合来自 sklearn
的模型。
LogisticRegressionCV(
solver="sag", scoring="neg_log_loss", verbose=0, n_jobs=-1, cv=10
)
拟合导致 model.score
(在训练集上)为 0.67 并发生变化。由于无法(或者我不知道如何)访问作为模型拟合的一部分执行的交叉验证的结果,我 运行 作为对同一模型的单独交叉验证
cross_val_score(model, X, y, cv=10, scoring="neg_log_loss")
这是一个负数数组return
[-0.69517214 -0.69211235 -0.64173978 -0.66429986 -0.77126878 -0.65127196
-0.66302393 -0.65916281 -0.66893633 -0.67605681]
如果标志被翻转,它似乎在与训练分数兼容的范围内。
我已经阅读了 issue 中关于 cross_val_score 翻转给定评分函数的符号的讨论,解决方案似乎引入了 neg_*
指标以使这种翻转变得不必要,我正在使用neg_log_loss
。该问题讨论了 mse
,但论点似乎也适用于 log_loss
。有没有办法让 cross_val_score
return 与其参数中指定的指标相同?或者这是我应该提交的错误?或者是我的误解,cross_val_score
?
我希望这对 SO 来说是一个足够具体的问题。 Sklearn
开发人员将用户重定向到 SO,以解决不是明确的错误报告或功能需求的问题。
在评论中为每个请求添加最少的重现代码(sklearn v 0.19.1 python 2.7):
from numpy.random import randn, seed
from sklearn.linear_model import LogisticRegressionCV
from sklearn.model_selection import cross_val_score
seed (0)
X = randn(100,2)
y = randn(100)>0
model = LogisticRegressionCV(
solver="sag", scoring="neg_log_loss", verbose=0, n_jobs=-1, cv=10
)
model.fit(X=X, y=y)
model.score(X,y)
cross_val_score(model, X, y, cv=10, scoring="neg_log_loss")
使用此代码,它看起来不再像一个简单的指标翻转符号。分数的输出为 0.59,交叉验证分数的输出为 array([-0.70578452, -0.68773683, -0.68627652, -0.69731349, -0.69198876, -0.70089103, -0.69476663, -0.68279466, -0.70066003, -0.68532253])
。
注意:在 Vivek Kumar and piccolbo.
的富有成果的评论线程后编辑关于 LinearRegressionCV score
方法的奇怪结果
您发现了一个错误,已在版本 0.20.0
中修复。
来自changelog:
Fix: Fixed a bug in linear_model.LogisticRegressionCV where the score method always computes accuracy, not the metric given by the scoring parameter. #10998 by Thomas Fan.
此外,sklearn 的 0.19 LogisticRegressionCV documentation 说:
score(X, y, sample_weight=None)
Returns the mean accuracy on the given test data and labels.
从版本 0.20.0
开始,docs 更新了错误修复:
score(X, y, sample_weight=None)
Returns the score using the scoring option on the given test data and labels.
关于cross_val_score
中返回的负值
cross_val_score
翻转 error
或 loss
指标的结果值,同时保留 score
指标的符号。来自 documentation:
All scorer objects follow the convention that higher return values are better than lower return values. Thus metrics which measure the distance between the model and the data, like metrics.mean_squared_error, are available as neg_mean_squared_error which return the negated value of the metric.