python多元线性回归拟合_如果我有两个自变量和一个因变量,如何绘制多元线性回归的最佳拟合线...

时间:2025-03-31 15:03:06

下面是一个示例曲面拟合,它可以生成三维散射图、三维曲面图和等高线图。在import numpy, scipy,

import matplotlib

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm # to colormap 3D surfaces from blue to red

import as plt

graphWidth = 800 # units are pixels

graphHeight = 600 # units are pixels

# 3D contour plot lines

numberOfContourLines = 16

def SurfacePlot(func, data, fittedParameters):

f = (figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

(True)

axes = Axes3D(f)

x_data = data[0]

y_data = data[1]

z_data = data[2]

xModel = (min(x_data), max(x_data), 20)

yModel = (min(y_data), max(y_data), 20)

X, Y = (xModel, yModel)

Z = func(numpy.array([X, Y]), *fittedParameters)

axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=, linewidth=1, antialiased=True)

(x_data, y_data, z_data) # show data along with plotted surface

axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot

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

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

axes.set_zlabel('Z Data') # Z axis data label

()

('all') # clean up after using pyplot or else thaere can be memory and process problems

def ContourPlot(func, data, fittedParameters):

f = (figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

axes = f.add_subplot(111)

x_data = data[0]

y_data = data[1]

z_data = data[2]

xModel = (min(x_data), max(x_data), 20)

yModel = (min(y_data), max(y_data), 20)

X, Y = (xModel, yModel)

Z = func(([X, Y]), *fittedParameters)

(x_data, y_data, 'o')

axes.set_title('Contour Plot') # add a title for contour plot

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

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

CS = (X, Y, Z, numberOfContourLines, colors='k')

(CS, inline=1, fontsize=10) # labels for contours

()

('all') # clean up after using pyplot or else thaere can be memory and process problems

def ScatterPlot(data):

f = (figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

(True)

axes = Axes3D(f)

x_data = data[0]

y_data = data[1]

z_data = data[2]

(x_data, y_data, z_data)

axes.set_title('Scatter Plot (click-drag with mouse)')

axes.set_xlabel('X Data')

axes.set_ylabel('Y Data')

axes.set_zlabel('Z Data')

()

('all') # clean up after using pyplot or else thaere can be memory and process problems

def func(data, a, alpha, beta):

t = data[0]

p_p = data[1]

return a * (t**alpha) * (p_p**beta)

if __name__ == "__main__":

xData = ([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])

yData = ([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])

zData = ([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

data = [xData, yData, zData]

initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

# here a non-linear surface fit is made with scipy's curve_fit()

fittedParameters, pcov = .curve_fit(func, [xData, yData], zData, p0 = initialParameters)

ScatterPlot(data)

SurfacePlot(func, data, fittedParameters)

ContourPlot(func, data, fittedParameters)

print('fitted prameters', fittedParameters)

modelPredictions = func(data, *fittedParameters)

absError = modelPredictions - zData

SE = (absError) # squared errors

MSE = (SE) # mean squared errors

RMSE = (MSE) # Root Mean Squared Error, RMSE

Rsquared = 1.0 - ((absError) / (zData))

print('RMSE:', RMSE)

print('R-squared:', Rsquared)