[英]A function to calculate the mean square error by cross-validation using cross_val_score
I would like to write a function that allows me to calculate the root mean square error obtained by 5-sample cross-validation using the cross_val_score function of sklearn.model_selection.我想编写一个函数,允许我使用 sklearn.model_selection 的 cross_val_score 函数计算通过 5 样本交叉验证获得的均方根误差。
(Knowing that the scoring argument of the cross_val_score()function allows to choose the metric we want to use.) (知道 cross_val_score() 函数的评分参数允许选择我们想要使用的指标。)
I found this method, but it does not correspond to the question :我找到了这个方法,但它与问题不符:
def rmse(predictions, targets):
return np.sqrt(((predictions - targets)**2).mean())
Thank you very much, Merci beaucoup :)非常感谢,谢谢你 :)
You can simply set scoring='mean_squared_error'
in sklearn.model_selection.cross_val_score
.您可以简单地在
sklearn.model_selection.cross_val_score
设置scoring='mean_squared_error'
。 Check out the documentation for the validator and the metric .查看验证器和指标的文档。
In other words:换句话说:
cv = cross_val_score(estimator=my_estimator, X, y, cv=5, scoring='mean_squared_error')
You can try :你可以试试 :
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv=5))
return rmse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.