Getting 'ValueError: shapes not aligned' on SciKit Linear Regression

前端 未结 3 804
死守一世寂寞
死守一世寂寞 2021-02-08 20:44

Quite new to SciKit and linear algebra/machine learning with Python in general, so I can\'t seem to solve the following:

I have a training set and a test set of data, co

3条回答
  •  不知归路
    2021-02-08 21:06

    This works for me:
    Initially, I was getting this error message:

    shapes (15754,3) and (4, ) not aligned 
    

    I found out that, I was creating a model using 3 variables in my train data. But what I add constant X_train = sm.add_constant(X_train) the constant variable is automatically gets created. So, in total there are now 4 variables.
    And when you test this model by default the test variable has 3 variables. So, the error gets pops up for dimension miss match.
    So, I used the trick that creates a dummy variable for y_test also.

    `X_test = sm.add_constant(X_test)`
    

    Though this a useless variable, but this solves all the issue.

提交回复
热议问题