Fitting the model, testing and getting the score or r2 does not give the error. But when I try to predict the actual data I get this ValueError:

#test and train shapes
test.shape, train.shape
((8523, 1606), (8523, 1606))

#creating dummies for the training dataset
X = train.drop('Item_Outlet_Sales_log', 1) #drop the log target column
y = train.Item_Outlet_Sales_log

X = pd.get_dummies(X)
train = pd.get_dummies(train)
test = pd.get_dummies(test)

#split the train data into train and test set in order to evaluate the model's accuracy
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

#split the the train data into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 42, shuffle = True)
#model 2 fitted with raw data
regression_model_2 = LinearRegression()
regression_model_2.fit(X_train, y_train)

pre = regression_model_2.predict(test)

Error enter image description here I tried different approaches but it still complains about the shapes

  • 2
    $\begingroup$ predict x_test not test. $\endgroup$
    – 10xAI
    Apr 11 at 16:43
  • 1
    $\begingroup$ test is the dataset that I must predict for submission. The x_test I did compute it and its working fine. #model_2 pred_regression_model_2 = regression_model_2.predict(X_test); $\endgroup$
    – SP_
    Apr 11 at 17:04

Check whether dtypes for the test data matches the dtypes for the x_train.

For my case, prediction was fine on x_train. I then used the code below to receive new data(equivalent to your test data) for prediction

# Take input from user
sepal_length = float(input("Enter sepal_length: "))
sepal_width = float(input("Enter sepa_width: "))
petal_length = float(input("Enter petal_length: "))
petal_width = float(input("Enter petal_width: "))

I noticed that although I specified float() some ended up being int() and that was the problem.

Again you might be having a strange variable on the test data(not on included in x_train. This can happen when you create dummies from a categorical data where a category on the test data is not in x_train.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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