简体   繁体   English

使用cross_val_score通过交叉验证计算均方误差的函数

[英]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 :)非常感谢,谢谢你 :)

Your using the wrong formula in your code, here is the correct formula for mean square error.您在代码中使用了错误的公式,这是均方误差的正确公式。

在此处输入图片说明

Y is the expected output, O is actual output from neural network. Y 是预期输出,O 是神经网络的实际输出。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM