简体   繁体   中英

python statsmodels.predict does not work

x=range(100)
y=sin(x)
result=sm.OLS(x,y).fit()
result.predict(x)

Gives:

ValueError: matrices are not aligned

This is very simple code, not sure why it's not working? I searched lots of forums but could not find exact solution.

quick answer:

I think you want x and y reversed result=sm.OLS(y, x).fit() The dependent variable ( y ) comes first, and then the array of explanatory variables ( x ).

The call to predict works with statsmodels master, but maybe you need a 2-D x in an older version:

result.predict(x[:,None])

to make the explanatory variable into a column_array. I don't remember when this was changed for 1-D x.)

Note also, that there is no constant/intercept added automatically when we don't use the formula interface.

The predict for the sample or training data can also be accessed through results.fittedvalues .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM