Python线性回归,带残差的最佳拟合线

Python线性回归,带残差的最佳拟合线,python,linear-regression,Python,Linear Regression,我已经做了线性回归和最佳拟合线,但是我还希望有一条线将实际点(蓝色的点)连接到预测点(红色的点x),表示预测误差,或所谓的残差。绘图应以类似的方式显示: 到目前为止,我所拥有的是: 提前非常感谢 下面是带有垂直线的示例代码 import numpy, scipy, matplotlib import matplotlib.pyplot as plt from scipy.optimize import curve_fit xData = numpy.array([1.1, 2.2, 3.3

我已经做了线性回归和最佳拟合线,但是我还希望有一条线将实际点(蓝色的点)连接到预测点(红色的点x),表示预测误差,或所谓的残差。绘图应以类似的方式显示:

到目前为止,我所拥有的是:


提前非常感谢

下面是带有垂直线的示例代码

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

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


def func(x, a, b): # simple linear example
    return a * x + b


initialParameters = numpy.array([1.0, 1.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters) 

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 = func(xModel, *fittedParameters)

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

    # now add individual line for each point
    for i in range(len(xData)):
        lineXdata = (xData[i], xData[i]) # same X
        lineYdata = (yData[i], modelPredictions[i]) # different Y
        plt.plot(lineXdata, lineYdata)

    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)

您可以生成一个包含每个液滴的起点和终点的序列,然后使用
plt.plot()
对其进行迭代-这是一个变化:不使用
plt.plot()
而不是
plt.scatter()
只是将这些点连接起来,而不是按照OP的指示添加液滴吗?
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

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


def func(x, a, b): # simple linear example
    return a * x + b


initialParameters = numpy.array([1.0, 1.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters) 

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 = func(xModel, *fittedParameters)

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

    # now add individual line for each point
    for i in range(len(xData)):
        lineXdata = (xData[i], xData[i]) # same X
        lineYdata = (yData[i], modelPredictions[i]) # different Y
        plt.plot(lineXdata, lineYdata)

    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)