0

I have first done a train/test split then fitted that data to a LinearRegression model shown below

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.4, random_state = 101)

Log_m = LinearRegression()

Log_m.fit(X_train,y_train)

predictions = Log_m.predict(X_test)

I have been given another test data frame and wanted to fit that to the Log_m model which has been created. So I did

predictions_t = Log_m.predict(fin_df1_t)

But I get the error message :

ValueError: shapes (1450,262) and (282,) not aligned: 262 (dim 1) != 282 (dim 0)

These are the shapes of dataframes

fin_df1_t (1450,262)

X_test (556,282)

X_train (834,282)

y_test (556,)

y_train (834,)
2
  • 1
    there are 282 columns in your training set 'X_train' .but the data frame 'fin_df1_t' only has 262 columns that's why it is showing an error regarding the shape. Jul 1, 2020 at 15:25
  • 1
    you need to identify columns that are in used in X_train that are also in fin_df1_t and subset that
    – StupidWolf
    Jul 1, 2020 at 20:30

1 Answer 1

1

The feature columns of new test data (262) are not equal to feature columns of Xtrain and Xtest (282), so it will always give an error. Both should have the same feature columns. For example, Xtrain and Xtest have the same columns (282), so there is no error at that step.

2
  • So would I need to go into the data frames and make sure they both have the same amount of columns. Also, do those columns need to have the same labels ? Jul 3, 2020 at 7:04
  • yes both should have same columns and same trend of labels. Jul 3, 2020 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.