Python Forum
How to use .predict() method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use .predict() method
#1
I am trying to apply a forecasted model with OLS regression to a test dataset, but when I use the .predict() method, I have this error:

ValueError: shapes (2938,4) and (1583,5) not aligned: 4 (dim 1) != 1583 (dim 0)
I add this replicable example of the problem where I obtain the same error (with different shapes because dataset is different):

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import statsmodels.api as sm
iris_data = load_iris()
iris_dataframe = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)

X_train, X_test = train_test_split(iris_dataframe, test_size=0.2, random_state=1)

X_train.head()
X_test.head()

model_test = sm.OLS(X_train["petal width (cm)"],X_train.iloc[:,0:3])
results_test=model_test.fit()
print(results_test.summary())

test_preds=model_test.predict(X_test)
As you can see, when I try to create test_preds, I have the sample error. I am using wrongly the .predict method? I am using as input the dataset X_test, that has the same columns number and names of the training dataset X_train.
Reply
#2
Is this related?
Reply
#3
(Nov-02-2023, 12:12 PM)Larz60+ Wrote: Is this related?
I think no. That question uses the Iris dataframe as an example like I did, but it's not on .predict() method.
Reply
#4
I found the solution. The object to be passed to .predict() is not the model object, but the fitobject. In this way it works:

test_preds=results_test.predict(X_test)
Reply
#5
Thanks for sharing solution
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to predict Total Hours needed with List as Input? caninan 0 792 Jan-27-2023, 04:20 PM
Last Post: caninan
  Which network architecture should be chosen to predict disease severity? AlekseyPython 2 2,013 Aug-21-2021, 07:12 PM
Last Post: jefsummers
  How to predict output in a regression problem? Rezaafz 1 2,517 Apr-03-2021, 04:16 PM
Last Post: jefsummers
  Keras.Predict into Dataframe Finpyth 13 9,885 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Analyze, predict the next step in a sequence. Antonio0608 0 2,032 Jul-23-2020, 05:53 PM
Last Post: Antonio0608
  Error When Using sklearn Predict Function firebird 0 2,071 Mar-21-2020, 04:34 PM
Last Post: firebird
  Predict Longitude and Latitude Using Python vibeandvisualize 1 2,306 Dec-27-2019, 12:10 PM
Last Post: Larz60+
  How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 1,814 Dec-12-2019, 03:29 AM
Last Post: sandeep_ganga
  Can someone explain how does svr_rbf.predict(dates) work? j2ee 0 3,540 Feb-22-2018, 06:50 PM
Last Post: j2ee
  Model.predict() always returning the same value of 1 for opencv nastyheatnor 0 5,543 Dec-14-2017, 08:20 AM
Last Post: nastyheatnor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020