[英]what is the problem with my code linreg.predict() not giving out right answer?
SO The question given to me was 所以给我的问题是
Write a function that fits a polynomial LinearRegression model on the training data X_train for degrees 1, 3, 6, and 9. (Use PolynomialFeatures in sklearn.preprocessing to create the polynomial features and then fit a linear regression model) For each model, find 100 predicted values over the interval x = 0 to 10 (eg np.linspace(0,10,100)) and store this in a numpy array. 在训练数据X_train上针对度1、3、6和9编写一个适合多项式LinearRegression模型的函数(在sklearn.preprocessing中使用PolynomialFeatures创建多项式特征,然后拟合线性回归模型)对于每个模型,找到在x = 0到10的间隔内有100个预测值(例如np.linspace(0,10,100)),并将其存储在numpy数组中。 The first row of this array should correspond to the output from the model trained on degree 1, the second row degree 3, the third row degree 6, and the fourth row degree 9. 此数组的第一行应对应于在度1,第二行度3,第三行度6和第四行度9上训练的模型的输出。
So tried the problem myself and failed and saw some other persons GitHub code and was very similar to me but it worked. 因此,我自己尝试了该问题,但失败了,并看到了其他人的GitHub代码,与我非常相似,但是有效。
So what is the difference between my code and the other person code? 那么我的代码和其他人的代码有什么区别?
Here is some basic code prior to my question 这是我提问之前的一些基本代码
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)
Here is my approach 这是我的方法
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)
Here is the code that works 这是有效的代码
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)
My approach got an error 我的方法出现错误
13 print("### **** ####")
---> 14 k[count,:]=t.reshape(1,-1)
15
16
ValueError: could not broadcast input array from shape (200) into shape (100)
While other code worked fine 虽然其他代码工作正常
The difference lies in the argument for linreg.predict
. 区别在于linreg.predict
的参数。 You are overwriting your pred
variable with the result of poly.fit_transform
, which changes it's shape from (100,1)
to (200,2)
in the first iteration of the loop. 您正在用poly.fit_transform
的结果覆盖pred
变量,该结果在循环的第一次迭代(200,2)
其形状从(100,1)
更改为(200,2)
。 In the second iteration, t
does not fit into k
anymore, resulting in the error you are facing. 在第二次迭代中, t
不再适合k
,从而导致您面临错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.