如何找到拟合的PolynomialFeatures模型的回归曲线方程

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.linear_model import LinearRegressionfrom sklearn.preprocessing import PolynomialFeaturesdata=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]})

我有上述包含输入和输出数据的数据集,我希望生成一条能很好拟合这些数据的曲线。首先,这里是输入和输出值的绘图:enter image description here

我编写了以下代码:

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")

绘图如下:

enter image description here

但是我找不到上面曲线(橙色曲线)的方程,该如何找到呢?


回答:

你的绘图实际上是用以下代码运行的结果:

poly=PolynomialFeatures(degree=7)

而不是degree=2。确实,将上述更改应用于你的代码,我们得到:

enter image description here

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

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)

得到:

enter image description here

使用一些随机生成的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**7y_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的低阶度…

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注