AttributeError: module 'statsmodels.formula.api' has no attribute 'OLS'

青春壹個敷衍的年華 提交于 2020-02-14 05:45:48

问题


I am trying to use Ordinary Least Squares for multivariable regression. But it says that there is no attribute 'OLS' from statsmodels. formula. api library. I am following the code from a lecture on Udemy The code is as follows:

import statsmodels.formula.api as sm
X_opt = X[:,[0,1,2,3,4,5]]
#OrdinaryLeastSquares
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit(

The error is as follows:

AttributeError                            Traceback (most recent call last)
<ipython-input-19-3bdb0bc861c6> in <module>()
      2 X_opt = X[:,[0,1,2,3,4,5]]
      3 #OrdinaryLeatSquares
----> 4 regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()

AttributeError: module 'statsmodels.formula.api' has no attribute 'OLS'

回答1:


Just for completeness, the code should look like this if statsmodels.version is 0.10.0:

import statsmodels.api as sm
X_opt = X[:,[0,1,2,3,4,5]]
#OrdinaryLeastSquares
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()



回答2:


Use this import.

import statsmodels.api as sm



回答3:


Try this instead, worked for me:

import statsmodels.regression.linear_model as sm



回答4:


I have tried the above mentioned methods and while

import statsmodels.api as sm

the import works for me. When I run the next piece of code

X_opt = X[:, [0, 1, 2, 3, 4, 5]]
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()

it gives me this error.

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

If you are getting the above mentioned error, you can solve it by specifying dtype for the np.array.

Replace

X_opt = X[:, [0, 1, 2, 3, 4, 5]]

with

X_opt = np.array(X[:, [0, 1, 2, 3, 4, 5]], dtype=float)


来源:https://stackoverflow.com/questions/56449787/attributeerror-module-statsmodels-formula-api-has-no-attribute-ols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!