比较不同曲线的均方误差

Comparing mean squared errors for different curves

我正在寻找适合线性、二次和三次函数的最小二乘法,并尝试打印它们的错误。一切正常,但我不明白如果我每次都变得更合适,为什么他们的错误会增加,我是否以错误的方式计算错误?这是情节,我的代码如下:

例如,这是获取立方图的代码。

import numpy as np
import matplotlib.pyplot as plt

A = np.array(((0,1),
             (1,1),
             (2,1),
             (3,1)))
xfeature = A.T[0]
squaredfeature = A.T[0] ** 2
cubedfeature = A.T[0] ** 3
ones = np.ones(4)

b = np.array((1,2,0,3), ndmin=2 ).T
b = b.reshape(4)

order = 3

features = np.concatenate((np.vstack(ones), np.vstack(xfeature), np.vstack(squaredfeature), np.vstack(cubedfeature)), axis = 1)
xstar = np.matmul( np.matmul( np.linalg.inv( np.matmul(features.T, features) ), features.T), b)

plt.scatter(A.T[0],b, c = 'red')
u = np.linspace(0,3,1000)
plt.plot(u, u**3*xstar[3] + u**2*xstar[2] + u*xstar[1] + xstar[0], 'b-')
plt.show()

b = np.array((1,2,0,3), ndmin=2 ).T
y_prediction = u**3*xstar[3] + u**2*xstar[2] + u*xstar[1] + xstar[0]
SSE = np.sum(np.square(y_prediction - b))
MSE = np.mean(np.square(y_prediction - b))
print("Sum of squared errors:", SSE)
print("Mean squared error:", MSE)

我认为这只是你最后一段代码中的一个小错误:你计算的是沿线的误差,而不是仅仅计算点的误差。相反,您要做的是计算每个点的距离。换句话说,y_prediction 和 b 应该具有相同的尺寸

b = np.array((1,2,0,3))
y_prediction = xfeature**3*xstar[3] + xfeature**2*xstar[2] + xfeature*xstar[1] + xstar[0]
SSE = np.sum(np.square(y_prediction - b))
MSE = np.mean(np.square(y_prediction - b))
print("Sum of squared errors:", SSE)
print("Mean squared error:", MSE)

那是你想要的吗?

作为一种不同的拟合方法,这里是一个使用 numpy 的 polyfit() 的 Python 图形多项式拟合器示例。您可以更改代码顶部的多项式顺序。

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 # example quadratic

# curve fit the test data
fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
print('Fitted Parameters:', fittedParameters)

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

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

print()


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

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = numpy.polyval(fittedParameters, xModel)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

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