简体   繁体   English

我的代码linreg.predict()没有给出正确答案是什么问题?

[英]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.

相关问题 该代码显示一个字符串列表包含一个特定的单词。 请告诉我为什么我的代码没有给出正确的答案? - The code shows that a list of strings contains a particular word. Please tell me why my code is not giving the right answer? pysha3没有给出正确的答案 - pysha3 not giving the right answer 给定一个整数数组,成对交换元素? 我的代码有什么问题我得到了正确的答案但无法提交 - Given an array of integers, swap the elements in pairs ? what's wrong with my code I am getting the answer right but not able to submit 我的神经网络预测给了我一个错误:IndexError: list index out of range - My neural networks predict is giving me an error: IndexError: list index out of range 排序列表给出错误的答案 - Sorting a list is giving out the wrong answer 为什么我的DNA模式的DNA模式没有给出正确的输出? - Why is my Python code for DNA pattern not giving the right output? 子集和问题的实现给出了错误的答案 - Implementation of subset sum problem is giving wrong answer 25 Locker问题-无法弄清楚我的代码出了什么问题 - 25 Locker Problem - can't figure out what's wrong with my code 当这个问题出现时,如果我想继续我的代码怎么办? RuntimeError:未找到最佳参数 - What if I want to continue my code when this problem come out? RuntimeError: Optimal parameters not found Python:无法弄清楚为什么我的循环会跳过正确的答案 - Python: Can't figure out why my loop skips the right answer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM