Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scikit learn Scikit学习:线性回归插值不起作用_Scikit Learn_Linear Regression_Interpolation - Fatal编程技术网

Scikit learn Scikit学习:线性回归插值不起作用

Scikit learn Scikit学习:线性回归插值不起作用,scikit-learn,linear-regression,interpolation,Scikit Learn,Linear Regression,Interpolation,我试图使用Scikit learn的LinearRegression类执行插值,但结果似乎是错误的。其思想是使用多项式拟合,拟合次数等于观测次数减1。这将使线性回归估计产生插值。然而,线性回归不能给出插值解 完整代码: import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.preprocessing import Polyn

我试图使用Scikit learn的LinearRegression类执行插值,但结果似乎是错误的。其思想是使用多项式拟合,拟合次数等于观测次数减1。这将使线性回归估计产生插值。然而,线性回归不能给出插值解

完整代码:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

# Data
x = np.array([65.44, 65.99, 65.16, 66.24, 66.85, 66.78, 67.52, 65.1 , 62.72,
       63.53, 63.62, 64.23, 64.89, 64.3 , 66.41])
y = np.array([8.5503, 8.5596, 8.4819, 8.505 , 8.5137, 8.5174, 8.5183, 8.5336,
       8.5581, 8.5534, 8.531 , 8.5546, 8.6349, 8.6553, 8.639 ])

# Design matrix with polynomial degree corresponding to the number of points (interpolation)
polyEstimator = PolynomialFeatures(len(x) - 1)
XHat = polyEstimator.fit_transform(x.reshape(-1, 1))

# Regression 
linReg = LinearRegression(fit_intercept=False) #normalize=True, 
linRegFit = linReg.fit(XHat, y)
yPredict = linRegFit.predict(XHat)
plt.figure()
plt.plot(x, yPredict, label='Fit')
plt.plot(x, y, 'x', label='True')
plt.legend()


我尝试了normalize=True,但也没有给出正确的答案。

您需要按照
x
的升序进行预测。我通过将数据放入数据框,然后按
x
排序解决了这个问题。如果预测到该数据帧中,则绘图是正确的。您需要数据帧结构,因为您不能单独按
x
排序。这将使您的
(x,y)
对无效

将numpy导入为np
将matplotlib.pyplot作为plt导入
从sklearn.linear\u模型导入线性回归
从sklearn.preprocessing导入多项式特征
作为pd进口熊猫
#资料
x=np.数组([65.44,65.99,65.16,66.24,66.85,66.78,67.52,65.1,62.72,
63.53, 63.62, 64.23, 64.89, 64.3 , 66.41])
y=np.数组([8.5503,8.5596,8.4819,8.505,8.5137,8.5174,8.5183,8.5336,
8.5581, 8.5534, 8.531 , 8.5546, 8.6349, 8.6553, 8.639 ])
df=pd.DataFrame({'x':x,'y':y})
df=df.sort_值(按='x')
#与点数对应的多项式次数的设计矩阵(插值)
多刺激=多项式特征(len(x)-1)
XHat=polyEstimator.fit_变换(np.array(df['x'])。重塑(-1,1))
#回归
linReg=线性回归(拟合_截距=假)#规格化=真,
linRegFit=linReg.fit(XHat,df['y'])
df['yppredict']=linRegFit.predict(XHat)
plt.图()
plt.plot(df['x'],df['yppredict'],label='Fit')
plt.plot(df['x'],df['y'],'x',label='True')
plt.legend()

非常感谢您回答@E.Sommer。我尝试了你的代码,但在我的计算机上结果仍然不正确。绘图看起来不一样,但仍然不正确。你是说除了matplotlib样式之外,你的绘图与我的不同吗?你说的“不正确”是什么意思?您的估计工作正常,您的绘图是错误的,只是因为
x
未排序导致绘图在x轴上来回移动。对不起,我得到的绘图与您的相同。由于多项式次数等于点数减去1,因此估计的直线应穿过所有点,而这不会发生。我认为这与观察结果的顺序无关。我认为你的推理是不对的。仅仅因为你拟合了一个非常灵活的多项式,并不意味着你就达到了所有的数据点。也许这是更好的问题,我认为当多项式次数等于观测数减1时,解应该穿过所有点,例如,请参见“Unisolvence定理”一节,我将检查您的链接。