sklearn.metrics计算回归模型的四大评价指标

大憨熊 提交于 2019-12-02 06:13:29

    '''
    模型效果指标评估
    y_true:真实的数据值
    y_pred:回归模型预测的数据值
    explained_variance_score:解释回归模型的方差得分,其值取值范围是[0,1],越接近于1说明自变量越能解释因变量
    的方差变化,值越小则说明效果越差。
    mean_absolute_error:平均绝对误差(Mean Absolute Error,MAE),用于评估预测结果和真实数据集的接近程度的程度
    ,其其值越小说明拟合效果越好。
    mean_squared_error:均方差(Mean squared error,MSE),该指标计算的是拟合数据和原始数据对应样本点的误差的
    平方和的均值,其值越小说明拟合效果越好。
    r2_score:判定系数,其含义是也是解释回归模型的方差得分,其值取值范围是[0,1],越接近于1说明自变量越能解释因
    变量的方差变化,值越小则说明效果越差。


explained_variance_score解释方差的得分

在这里插入图片描述


Parameters:	
	y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)
			Ground truth (correct) target values.

	y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)
			Estimated target values.

	sample_weight : array-like of shape = (n_samples), optional
			Sample weights.

	multioutput : string in [‘raw_values’, ‘uniform_average’, ‘variance_weighted’] 				or array-like of shape (n_outputs)
			Defines aggregating of multiple output scores. Array-like value defines 	weights used to average scores.

	‘raw_values’ :
			Returns a full set of scores in case of multioutput input.

	‘uniform_average’ :
		Scores of all outputs are averaged with uniform weight.

	‘variance_weighted’ :
		Scores of all outputs are averaged, weighted by the variances of each individual 	output.

Returns:	
score : float or ndarray of floats
The explained variance or ndarray if ‘multioutput’ is ‘raw_values’.
>>> from sklearn.metrics import explained_variance_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> explained_variance_score(y_true, y_pred)  
0.957...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> explained_variance_score(y_true, y_pred, multioutput='uniform_average')
... 
0.983...

mean_absolute_error平均绝对误差

在这里插入图片描述

sklearn.metrics.mean_absolute_error(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)


Parameters:	
	y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)
	Ground truth (correct) target values.
	
	y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)
	Estimated target values.
	
	sample_weight : array-like of shape = (n_samples), optional
	Sample weights.
	
	multioutput : string in [‘raw_values’, ‘uniform_average’]
	or array-like of shape (n_outputs) Defines aggregating of multiple output values. Array-like value defines weights used to average errors.
	
	‘raw_values’ :
	Returns a full set of errors in case of multioutput input.
	
	‘uniform_average’ :
	Errors of all outputs are averaged with uniform weight.

Returns:	
	loss : float or ndarray of floats
	If multioutput is ‘raw_values’, then mean absolute error is returned for each output separately. If multioutput is ‘uniform_average’ or an ndarray of weights, then the weighted average of all output errors is returned.
	
	MAE output is non-negative floating point. The best value is 0.0.

Examples

>>> from sklearn.metrics import mean_absolute_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_error(y_true, y_pred)
0.5
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> mean_absolute_error(y_true, y_pred)
0.75
>>> mean_absolute_error(y_true, y_pred, multioutput='raw_values')
array([0.5, 1. ])
>>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
... 
0.85...

mean_squared_error 均方误差

在这里插入图片描述

sklearn.metrics.mean_squared_error(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)


Parameters:	
		y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)
		Ground truth (correct) target values.
		
		y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)
		Estimated target values.
		
		sample_weight : array-like of shape = (n_samples), optional
		Sample weights.
		
		multioutput : string in [‘raw_values’, ‘uniform_average’]
		or array-like of shape (n_outputs) Defines aggregating of multiple output values. Array-like value defines weights used to average errors.
		
		‘raw_values’ :
		Returns a full set of errors in case of multioutput input.
		
		‘uniform_average’ :
		Errors of all outputs are averaged with uniform weight.

Returns:	
	loss : float or ndarray of floats
	A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target.

Examples:

>>> from sklearn.metrics import mean_squared_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_squared_error(y_true, y_pred)
0.375
>>> y_true = [[0.5, 1],[-1, 1],[7, -6]]
>>> y_pred = [[0, 2],[-1, 2],[8, -5]]
>>> mean_squared_error(y_true, y_pred)  
0.708...
>>> mean_squared_error(y_true, y_pred, multioutput='raw_values')
... 
array([0.41666667, 1.        ])
>>> mean_squared_error(y_true, y_pred, multioutput=[0.3, 0.7])
... 
0.825...

r2_score

在这里插入图片描述

sklearn.metrics.r2_score(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)

Parameters:	
		y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)
		Ground truth (correct) target values.
		
		y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)
		Estimated target values.
		
		sample_weight : array-like of shape = (n_samples), optional
		Sample weights.
		
		multioutput : string in [‘raw_values’, ‘uniform_average’, ‘variance_weighted’] or None or array-like of shape (n_outputs)
		Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default is “uniform_average”.
		
		‘raw_values’ :
		Returns a full set of scores in case of multioutput input.
		
		‘uniform_average’ :
		Scores of all outputs are averaged with uniform weight.
		
		‘variance_weighted’ :
		Scores of all outputs are averaged, weighted by the variances of each individual output.
		
		Changed in version 0.19: Default value of multioutput is ‘uniform_average’.

Returns:	
	z : float or ndarray of floats
	The R^2 score or ndarray of scores if ‘multioutput’ is ‘raw_values’.

Examples

>>> from sklearn.metrics import r2_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> r2_score(y_true, y_pred)  
0.948...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> r2_score(y_true, y_pred,
...          multioutput='variance_weighted') 
0.938...
>>> y_true = [1, 2, 3]
>>> y_pred = [1, 2, 3]
>>> r2_score(y_true, y_pred)
1.0
>>> y_true = [1, 2, 3]
>>> y_pred = [2, 2, 2]
>>> r2_score(y_true, y_pred)
0.0
>>> y_true = [1, 2, 3]
>>> y_pred = [3, 2, 1]
>>> r2_score(y_true, y_pred)
-3.0

Median absolute error绝对中位差

在这里插入图片描述

sklearn.metrics.median_absolute_error(y_true, y_pred)

Parameters:	
	y_true : array-like of shape = (n_samples)
	Ground truth (correct) target values.
	
	y_pred : array-like of shape = (n_samples)
	Estimated target values.

Returns:	
	loss : float
	A positive floating point value (the best value is 0.0).
>>> from sklearn.metrics import median_absolute_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> median_absolute_error(y_true, y_pred)
0.5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!