Python和R均方根误差平均绝对误差算法模型

要点

  1. 回归模型误差评估指标归一化均方根误差
  2. 生态状态指标
  3. 神经网络成本误差计算
  4. 气体排放气候算法模型
    Python和R均方根误差平均绝对误差算法模型_第1张图片

Python误差指标

均方根误差和平均绝对误差

均方根偏差或均方根误差是两个密切相关且经常使用的度量值之一,用于衡量真实值或预测值与观测值或估计值之间的差异。

估计器 θ ^ \hat{\theta} θ^ 相对于估计参数 θ \theta θ 的 RMSD 定义为均方误差的平方根:
RMSD ⁡ ( θ ^ ) = MSE ⁡ ( θ ^ ) = E ( ( θ ^ − θ ) 2 ) \operatorname{RMSD}(\hat{\theta})=\sqrt{\operatorname{MSE}(\hat{\theta})}=\sqrt{ E \left((\hat{\theta}-\theta)^2\right)} RMSD(θ^)=MSE(θ^) =E((θ^θ)2)
对于无偏估计量,RMSD 是方差的平方根,称为标准差。

如果 X 1 , … , X n X_1, \ldots, X_n X1,,Xn 是具有真实平均值 x 0 x_0 x0 的总体样本,则该样本的 RMSD 为
R M S D = 1 n ∑ i = 1 n ( X i − x 0 ) 2 RMSD =\sqrt{\frac{1}{n} \sum_{i=1}^n\left(X_i-x_0\right)^2} RMSD=n1i=1n(Xix0)2
回归因变量 y t y_t yt 的时间 t t t 的预测值 y ^ t \hat{y}_t y^t 的 RMSD(观察到 T T T 次的变量)针对 T T T 不同的预测进行计算,作为偏差平方平均值:
R M S D = ∑ t = 1 T ( y t − y ^ t ) 2 T RMSD =\sqrt{\frac{\sum_{t=1}^T\left(y_t-\hat{y}_t\right)^2}{T}} RMSD=Tt=1T(yty^t)2
平均绝对误差是对表达相同现象的成对观测值之间的误差的度量。 Y Y Y X X X 的示例包括预测与观察的比较、后续时间与初始时间的比较以及一种测量技术与替代测量技术的比较。 平均绝对误差的计算方式为绝对误差之和除以样本大小:
M A E = ∑ i = 1 n ∣ y i − x i ∣ n = ∑ i = 1 n ∣ e i ∣ n MAE =\frac{\sum_{i=1}^n\left|y_i-x_i\right|}{n}=\frac{\sum_{i=1}^n\left|e_i\right|}{n} MAE=ni=1nyixi=ni=1nei
因此,它是绝对误差 ∣ e i ∣ = ∣ y i − x i ∣ \left|e_i\right|=\left|y_i-x_i\right| ei=yixi 的算术平均值,其中 y i y_i yi 是预测值, x i x_i xi 是真实值。替代的公式可以包括相对频率作为权重因子。平均绝对误差使用与测量数据相同的尺度。这被称为与尺度相关的精度测量,因此不能用于在使用不同尺度的预测值之间进行比较。平均绝对误差是时间序列分析中预测误差的常见度量,有时与更标准的平均绝对偏差定义相混淆。

两种误差示例:

import pandas as pd
from sklearn.linear_model import LinearRegression
sal_data={"年限":[2,2.2, 2.8, 4, 7, 8, 11, 12, 21, 25], 
          "薪水": [7, 8, 11, 15, 22, 29, 37 ,45.7, 49, 52]}
df=pd.DataFrame(sal_data)
df.head(3)

年限 薪水 1 2.0 7.0 2 2.2 8.0 3 2.8 11.0 \begin{array}{rrr} \hline & 年限 & 薪水 \\ \hline 1 & 2.0 & 7.0 \\ \hline 2 & 2.2 & 8.0 \\ \hline 3 & 2.8 & 11.0 \\ \hline \end{array} 123年限2.02.22.8薪水7.08.011.0

X=df[['年限]]
y=df.Salary
lm=LinearRegression()
lm.fit(X,y)
yp=lm.predict(X)
print(yp)
[12.23965934 12.64846842 13.87489568 16.32775018 22.45988645 24.50393187 30.63606813 32.68011355 51.07652234 59.25270403]

现在,将通过绘制预测(yp)和实际薪资(y)来评估我们的模型。

from bokeh.plotting import figure, show, output_file
p=figure(title="Actual vs Predicted Salary", width=450, height=300)
p.title.align = 'center'
p.circle(df.Exp, df.Salary)
p.line(df.Exp, df.Salary, legend_label='Actual Salary', line_width=3, line_alpha=0.4)
p.circle(df.Exp, yp, color="red")
p.line(df.Exp,yp, color="red",legend_label='Predicted Salary', line_width=3, line_alpha=0.4)
p.xaxis.axis_label = 'Experience'
p.yaxis.axis_label = 'Salary'
show(p)

从上图中,我们可以看到预测数据点和实际数据点之间存在差距。从统计学上讲,这种差距/差异称为残差,通常称为误差,用于 RMSE 和 MAE。Scikit-learn 提供了度量库来计算这些值。但是,我们将使用上述数学表达式来计算均方根误差和平均绝对误差。这两种方法都会给出相同的结果。

import numpy as np
print(f'Residuals: {y-yp}')
np.sqrt(np.mean(np.square(y-yp)))  
np.mean(abs(y-yp))                 

from sklearn.metrics import mean_squared_error, mean_absolute_error
np.sqrt(mean_squared_error(y, yp))
mean_absolute_error(y, yp)
6.48
5.68

这是我们的基准模型。 平均绝对误差约为 5.7。现在我们的目标是通过减少这个错误来改进这个模型。

from sklearn.preprocessing import PolynomialFeatures
pf=PolynomialFeatures()     
X_poly=pf.fit_transform(X) 
lm.fit(X_poly, y)
yp=lm.predict(X_poly)

计算我们的误差指标

p.sqrt(np.mean(np.square(y-yp)))
np.mean(abs(y-yp))
2.3974
1.6386

不同曲线均方误差

多项式拟合

import numpy, matplotlib
import matplotlib.pyplot as plt

xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7, 0.0])
yData = numpy.array([1.1, 20.2, 30.3, 40.4, 50.0, 60.6, 70.7, 0.1])

polynomialOrder = 2 

fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
print('Fitted Parameters:', fittedParameters)

modelPredictions = numpy.polyval(fittedParameters, xData)
absError = modelPredictions - yData

SE = numpy.square(absError) 
MSE = numpy.mean(SE)
RMSE = numpy.sqrt(MSE)
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    axes.plot(xData, yData,  'D')

    xModel = numpy.linspace(min(xData), max(xData))
    yModel = numpy.polyval(fittedParameters, xModel)

    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') 
    axes.set_ylabel('Y Data') 

    plt.show()
    plt.close('all') 

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

更新:亚图跨际

你可能感兴趣的:(Python,交叉知识,R,回归模型,误差指标,归一化均方根误差,生态状态指标,神经网络成本误差,气体排放气候模型,多项式拟合)