如何查看Python回归的结果(等式)?

时间:2019-06-25 14:54:06

标签: python regression

我想查看python中多项式回归的回归方程。

我是python的新手,在R中,我正在寻找的类似命令是“ summary”。我已经在python中尝试过打印功能。

x = (LIST)
y = (LIST)

x = x[:, np.newaxis]
y = y[:, np.newaxis]

poly = PolynomialFeatures(degree=2)
x_poly = poly.fit_transform(x)

poly.fit(x_poly,y)
lin = LinearRegression()
lin.fit(x_poly,y)
y_poly_pred = lin.predict(x_poly)

print(lin)
print(poly)
print(lin.predict)
print(poly.fit_transform)

我希望输出给我ax ^ 2 + bx + c方程,或者至少是给出该方程的信息。相反,我得到了(下面)我的4条打印语句。

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, 
normalize=False)
PolynomialFeatures(degree=2, include_bias=True, interaction_only=False,
               order='C')
<bound method LinearModel.predict of LinearRegression(copy_X=True, 
fit_intercept=True, n_jobs=None, normalize=False)>
<bound method TransformerMixin.fit_transform of 
PolynomialFeatures(degree=2, include_bias=True, interaction_only=False,
               order='C')>

1 个答案:

答案 0 :(得分:0)

这是示例图形多项式拟合器,使用numpy.polyfit进行拟合,使用numpy.polyval进行评估。这个例子有八个数据点,使polynomialOrder = 7可以很好地显示龙格现象。

plot

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)