多项式回归曲线我是新来sklearn和我有一个适当的简单的任务:给定的15点的散点图,我需要故障配件在sklearn
- 把他们的11作为我的训练样本',
- 通过这11个点拟合3次多项式曲线;
- 在15个点上画出得到的多项式曲线。
但是我陷入了第二步。
这是数据图:
%matplotlib notebook
import numpy as np from sklearn.model_selection
import train_test_split from sklearn.linear_model
import LinearRegression from sklearn.preprocessing import PolynomialFeatures
np.random.seed(0)
n = 15
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
plt.figure() plt.scatter(X_train, y_train, label='training data')
plt.scatter(X_test, y_test, label='test data')
plt.legend(loc=4);
我再取11分X_train
和度数3的聚特点如下改造他们:
degrees = 3
poly = PolynomialFeatures(degree=degree)
X_train_poly = poly.fit_transform(X_train)
然后我尽量合身通过变换点的线(注意:X_train_poly.size
= 364)。
linreg = LinearRegression().fit(X_train_poly, y_train)
,我得到以下错误:
ValueError: Found input variables with inconsistent numbers of samples: [1, 11]
我已阅读,解决类似的和更为复杂的问题(例如Multivariate (polynomial) best fit curve in python?)的各种问题,但我不能提取它们的解决方案。
可能重复:https://stackoverflow.com/questions/32097392/sklearn-issue-found-arrays-with-inconsistent-numbers -of次采样时,这样做,REGR – Moritz