简体   繁体   中英

Error using statsmodels.api using ols().fit()

I have been following a tutorial on Udemy for machine learning. I am using the statsmodels.formula.api library but the class OLS was not in there and I guess it was moved because I found a thread saying to use statsmodels.api and it imports. The problem is that when I run the fit() method on sm.OLS() , code below, I get the error 'NoneType' Object has no attribute 'shape' . Here's the code and the error.

import statsmodels.api as sm
X = np.append(arr = np.ones((len(X), 1)).astype(int), values = X , axis = 1)
X_opt = X[:, [0, 1, 2, 3, 4, 5]]
regressor_OLS = sm.OLS(endog = y, exorg = X_opt).fit()

Error on the line regressor_OLS = sm.OLS(endog = y, exorg = X_opt).fit()

AttributeError: 'NoneType' object has no attribute 'shape'

Note: If I exclude .fit() as in just run regressor_OLS = sm.OLS(endog = y, exorg = X_opt) , it works without a error but it's not the result I want.

Change it to

import statsmodels.api as sm
X = np.append(arr = np.ones((len(X), 1)).astype(int), values = X , axis = 1)
X_opt = X[:, [0, 1, 2, 3, 4, 5]]
regressor_OLS = sm.OLS(endog = y, exog = X_opt)
res=regressor_OLS.fit()

This is because you have to fit your regressor after you initialise it. ON other note just noticed it is exog not exorg

Hope this helps.

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