我的代码 linreg.predict() 没有给出正确答案有什么问题?
what is the problem with my code linreg.predict() not giving out right answer?
所以给我的问题是
编写一个函数,在训练数据 X_train 上拟合 1、3、6 和 9 阶的多项式线性回归模型。(使用 sklearn.preprocessing 中的 PolynomialFeatures 创建多项式特征,然后拟合线性回归模型)对于每个模型,在 x = 0 到 10 的区间内找到 100 个预测值(例如 np.linspace(0,10,100))并将其存储在一个 numpy 数组中。该数组的第一行应对应于在度数 1、第二行度数 3、第三行度数 6 和第四行度数 9 上训练的模型的输出。
所以我自己尝试了这个问题,但失败了,看到其他人 GitHub 代码,与我非常相似,但它有效。
那么我的代码和别人的代码有什么区别呢?
这是我提出问题之前的一些基本代码
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)
这是我的方法
pred=np.linspace(0,10,100).reshape(100,1)
k=np.zeros((4,100))
for count,i in enumerate([1,3,6,9]):
poly = PolynomialFeatures(degree=i)
X_poly = poly.fit_transform(X_train.reshape(-1,1))
linreg = LinearRegression()
linreg.fit(X_poly,y_train.reshape(-1,1))
pred = poly.fit_transform(pred.reshape(-1,1))
t=linreg.predict(pred)
#print(t) #used for debugging
print("### **** ####") #used for debugging
k[count,:]=t.reshape(1,-1)
print(k)
这是有效的代码
result = np.zeros((4, 100))
for i, degree in enumerate([1, 3, 6, 9]):
poly = PolynomialFeatures(degree=degree)
X_poly = poly.fit_transform(X_train.reshape(11,1))
linreg = LinearRegression().fit(X_poly, y_train)
y=linreg.predict(poly.fit_transform(np.linspace(0,10,100).reshape(100,1)))
result[i, :] = y
print(result)
我的方法出错了
13 print("### **** ####")
---> 14 k[count,:]=t.reshape(1,-1)
15
16
ValueError: could not broadcast input array from shape (200) into shape (100)
虽然其他代码工作正常
区别在于linreg.predict
的参数。您正在用 poly.fit_transform
的结果覆盖 pred
变量,这会在循环的第一次迭代中将其形状从 (100,1)
更改为 (200,2)
。在第二次迭代中,t
不再适合 k
,导致您遇到错误。
所以给我的问题是
编写一个函数,在训练数据 X_train 上拟合 1、3、6 和 9 阶的多项式线性回归模型。(使用 sklearn.preprocessing 中的 PolynomialFeatures 创建多项式特征,然后拟合线性回归模型)对于每个模型,在 x = 0 到 10 的区间内找到 100 个预测值(例如 np.linspace(0,10,100))并将其存储在一个 numpy 数组中。该数组的第一行应对应于在度数 1、第二行度数 3、第三行度数 6 和第四行度数 9 上训练的模型的输出。
所以我自己尝试了这个问题,但失败了,看到其他人 GitHub 代码,与我非常相似,但它有效。
那么我的代码和别人的代码有什么区别呢?
这是我提出问题之前的一些基本代码
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)
这是我的方法
pred=np.linspace(0,10,100).reshape(100,1)
k=np.zeros((4,100))
for count,i in enumerate([1,3,6,9]):
poly = PolynomialFeatures(degree=i)
X_poly = poly.fit_transform(X_train.reshape(-1,1))
linreg = LinearRegression()
linreg.fit(X_poly,y_train.reshape(-1,1))
pred = poly.fit_transform(pred.reshape(-1,1))
t=linreg.predict(pred)
#print(t) #used for debugging
print("### **** ####") #used for debugging
k[count,:]=t.reshape(1,-1)
print(k)
这是有效的代码
result = np.zeros((4, 100))
for i, degree in enumerate([1, 3, 6, 9]):
poly = PolynomialFeatures(degree=degree)
X_poly = poly.fit_transform(X_train.reshape(11,1))
linreg = LinearRegression().fit(X_poly, y_train)
y=linreg.predict(poly.fit_transform(np.linspace(0,10,100).reshape(100,1)))
result[i, :] = y
print(result)
我的方法出错了
13 print("### **** ####")
---> 14 k[count,:]=t.reshape(1,-1)
15
16
ValueError: could not broadcast input array from shape (200) into shape (100)
虽然其他代码工作正常
区别在于linreg.predict
的参数。您正在用 poly.fit_transform
的结果覆盖 pred
变量,这会在循环的第一次迭代中将其形状从 (100,1)
更改为 (200,2)
。在第二次迭代中,t
不再适合 k
,导致您遇到错误。