【发布时间】:2017-07-19 16:56:28
【问题描述】:
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
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)
poly = PolynomialFeatures(degree=1)
X_poly = poly.fit_transform(X_train)
model = LinearRegression().fit(X_poly, y_ploy)
和 ValueError:发现样本数量不一致的输入变量:[1, 15]
我发现: 长度(x)->15,
array([ 0.35281047, 0.79431716, 1.62431903, 2.59103578,
3.23065446, 3.375973 , 4.47573197, 4.96972856,
5.69364194, 6.51069113, 7.17166586, 8.14799756,
8.72363612, 9.31004929, 10.08877265])
在 poly.fit_transform 之后, len(X_poly)->1,包含16个数字,好像加了1,不知道为什么会这样?
array([[ 1. , 0.35281047, 0.79431716, 1.62431903,
2.59103578, 3.23065446, 3.375973 , 4.47573197,
4.96972856, 5.69364194, 6.51069113, 7.17166586,
8.14799756, 8.72363612, 9.31004929, 10.08877265]])
所以模型无法完成,但我该如何修复它?
【问题讨论】:
标签: python arrays numpy machine-learning linear-regression