sklearn.metrics.mean_squared_error

sklearn.metrics.mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', squared=True)

均方误差回归损失

用户指南中阅读更多内容。

参数 说明
y_true array-like of shape (n_samples,) or (n_samples, n_outputs)
真实目标值。
y_pred array-like of shape (n_samples,) or (n_samples, n_outputs)
预测目标值。
sample_weight array-like of shape (n_samples,), optional
样本权重。
multioutput string in [‘raw_values’, ‘uniform_average’] or array-like of shape (n_outputs)
定义多个输出值的汇总。类似数组的值定义了用于平均误差的权重。
- ‘raw_values’:
如果是多输出格式的输入,则返回完整的错误集。
- ‘uniform_average’:
所有输出的误差均以相同的权重平均。
squared boolean value, optional (default = True)
如果为True,则返回MSE值;如果为False,则返回RMSE值。
返回值 说明
loss float or ndarray of floats
非负浮点值(最佳值为0.0)或浮点值数组,每个目标对应一个浮点值。

示例

>>> from sklearn.metrics import mean_squared_error
>>> y_true = [3-0.527]
>>> y_pred = [2.50.028]
>>> mean_squared_error(y_true, y_pred)
0.375
>>> y_true = [3-0.527]
>>> y_pred = [2.50.028]
>>> mean_squared_error(y_true, y_pred, squared=False)
0.612...
>>> y_true = [[0.51],[-11],[7-6]]
>>> y_pred = [[02],[-12],[8-5]]
>>> mean_squared_error(y_true, y_pred)
0.708...
>>> mean_squared_error(y_true, y_pred, multioutput='raw_values')
array([0.416666671.        ])
>>> mean_squared_error(y_true, y_pred, multioutput=[0.30.7])
0.825...