Karoïevski
Karoïevski

Reputation: 15

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.

(Knowing that the scoring argument of the cross_val_score()function allows to choose the metric we want to use.)

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 :)

Upvotes: 0

Views: 2389

Answers (3)

Atipiks
Atipiks

Reputation: 11

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

Upvotes: 1

Arturo Sbr
Arturo Sbr

Reputation: 6323

You can simply set scoring='mean_squared_error' in sklearn.model_selection.cross_val_score. 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')

Upvotes: 1

user9321739
user9321739

Reputation:

Your using the wrong formula in your code, here is the correct formula for mean square error.

enter image description here

Y is the expected output, O is actual output from neural network.

Upvotes: 1

Related Questions