【问题标题】:How to find regression curve equation for a fitted PolynomialFeatures model如何找到拟合的多项式特征模型的回归曲线方程
【发布时间】:2020-12-29 22:05:48
【问题描述】:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

data=pd.DataFrame(
{"input": 
[0.001,0.015,0.066,0.151,0.266,0.402,0.45,0.499,0.598,0.646,0.738,0.782,0.86,0.894,0.924,0.95],
"output":[0.5263157894736842,0.5789473684210524,0.6315789473684206,0.6842105263157897, 
0.6315789473684206, 0.7894736842105263, 0.8421052631578945, 0.7894736842105263,  0.736842105263158,
0.6842105263157897,  0.736842105263158,  0.736842105263158,0.6842105263157897, 0.6842105263157897, 
0.6315789473684206,0.5789473684210524]})

我有上述数据,其中包括输入和输出数据,我想制作一条适合这些数据的曲线。首先绘制输入和输出值在这里:

我已经制作了这段代码:

X=data.iloc[:,0].to_numpy()
X=X.reshape(-1,1)
y=data.iloc[:,1].to_numpy()
y=y.reshape(-1,1)

poly=PolynomialFeatures(degree=2)
poly.fit(X,y)
X_poly=poly.transform(X)

reg=LinearRegression().fit(X_poly,y)
plt.scatter(X,y,color="blue")
plt.plot(X,reg.predict(X_poly),color="orange",label="Polynomial Linear Regression")
plt.xlabel("Temperature")
plt.ylabel("Pressure")
plt.legend(loc="upper left")

情节是:

但是我没有找到上面的曲线方程(橙色曲线)我怎么能找到呢?

【问题讨论】:

  • 由于您使用来自sklearnPolynomialFeatures,它会为您完成所有工作,方程将不可见,但您可以认为它是最好的n次多项式方程适合您的数据。
  • @JenilDave 我将使用这个方程通过积分获得阻力,因此必须确定。也许我应该使用 scipy 回归?
  • @ashraful no 我无法通过这种方式找到
  • 请发布您用于生成绘图的degree 的确切值(它与您在代码中显示的degree=2 不对应,看起来更像degree=7)。跨度>

标签: python machine-learning scikit-learn regression


【解决方案1】:

您的情节实际上对应于您运行的代码

poly=PolynomialFeatures(degree=7)

而不是degree=2。确实,使用上述更改运行您的代码,我们得到:

现在,您的多项式特征是:

poly.get_feature_names()
# ['1', 'x0', 'x0^2', 'x0^3', 'x0^4', 'x0^5', 'x0^6', 'x0^7']

您的线性回归的相应系数是:

reg.coef_
# array([[   0.        ,    5.43894411,  -68.14277256,  364.28508827,
#         -941.70924401, 1254.89358662, -831.27091422,  216.43304954]])

加上截距:

reg.intercept_
# array([0.51228593])

鉴于上述情况,并设置

coef = reg.coef_[0]

因为这里我们在初始数据中只有一个特征,所以你的回归方程是:

y = reg.intercept_ + coef[0] + coef[1]*x + coef[2]*x**2 + coef[3]*x**3 + coef[4]*x**4 + coef[5]*x**5 + coef[6]*x**6 + coef[7]*x**7

为了视觉验证,我们可以用[0, 1]中的一些x数据绘制上述函数

x = np.linspace(0, 1, 15) 

y

运行上述表达式
plt.plot(x, y)

给予:

使用一些随机生成的数据x,我们可以验证方程y_eq的结果确实等于回归模型y_reg在数值精度范围内产生的结果:

x = np.random.rand(1,10)
y_eq = reg.intercept_ + coef[0] + coef[1]*x + coef[2]*x**2 + coef[3]*x**3 + coef[4]*x**4 + coef[5]*x**5 + coef[6]*x**6 + coef[7]*x**7
y_reg = np.concatenate(reg.predict(poly.transform(x.reshape(-1,1)))) 

y_eq
# array([[0.72452703, 0.64106819, 0.67394222, 0.71756648, 0.71102853,
#         0.63582055, 0.54243177, 0.71104983, 0.71287962, 0.6311952 ]])

y_reg
# array([0.72452703, 0.64106819, 0.67394222, 0.71756648, 0.71102853,
#        0.63582055, 0.54243177, 0.71104983, 0.71287962, 0.6311952 ])

np.allclose(y_reg, y_eq)
# True

与问题无关,我想您已经知道尝试将如此高阶多项式拟合到如此少的数据点并不是一个好主意,您可能应该保持在 2 或 3 的低阶...

【讨论】:

    【解决方案2】:

    请注意您是如何制作问题中显示的图的。当我运行您的代码时,我得到以下 (degree=2) 多项式符合预期的数据:

    现在您已经拟合了数据,您可以看到模型的系数:

    print(reg.coef_)
    print(reg.intercept_)
    # [[ 0.          0.85962436 -0.83796885]]
    # [0.5523586]
    

    请注意,用于拟合此模型的数据等价于以下内容:

    X_poly = np.concatenate([np.ones((16,1)), X, X**2], axis=1)
    

    因此单个数据点是一个如下创建的向量:

    temp = 0.5
    x = np.array([1, temp, temp**2]).reshape((1,3))
    

    您的多项式模型只是多项式特征的线性模型:

    y = A.x + B

    y = reg.coef_.dot(x.T) + reg.intercept_
    print(y) #  [[0.77267856]]
    

    验证:

    print(reg.predict(x))  # array([[0.77267856]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 2018-02-16
      • 2020-03-18
      • 1970-01-01
      • 2019-03-02
      • 2019-07-05
      相关资源
      最近更新 更多