我知道我可以实现这样一个均方根误差函数:

def rmse(predictions, targets):
    return np.sqrt(((predictions - targets) ** 2).mean())

如果这个rmse函数是在某个库中实现的,可能是在scipy或scikit-learn中,我在寻找什么?


当前回答

sklearn的mean_squared_error本身包含一个参数平方,默认值为True。如果我们将其设置为False,相同的函数将返回RMSE而不是MSE。

from sklearn.metrics import mean_squared_error
rmse = mean_squared_error(y_true, y_pred , squared=False)

其他回答

sklearn的mean_squared_error本身包含一个参数平方,默认值为True。如果我们将其设置为False,相同的函数将返回RMSE而不是MSE。

from sklearn.metrics import mean_squared_error
rmse = mean_squared_error(y_true, y_pred , squared=False)
from sklearn.metrics import mean_squared_error
rmse = mean_squared_error(y_actual, y_predicted, squared=False)

or 

import math
from sklearn.metrics import mean_squared_error
rmse = math.sqrt(mean_squared_error(y_actual, y_predicted))

在scikit-learn 0.22.0中,您可以将参数squared=False传递给mean_squared_error()以返回RMSE。

from sklearn.metrics import mean_squared_error
mean_squared_error(y_actual, y_predicted, squared=False)
from sklearn import metrics              
import numpy as np
print(np.sqrt(metrics.mean_squared_error(y_test,y_predict)))

你可能想加上绝对值np。如果你在处理复数。

import numpy as np
rms = np.sqrt(np.mean(np.abs(x-y)**2))

注意,如果使用np. linalgg .norm,它已经处理了复数。

import numpy as np
rms = np.linalg.norm(x-y)/np.sqrt(len(x))