Nabeel Parkar
Nabeel Parkar

Reputation: 408

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.

Upvotes: 2

Views: 1390

Answers (1)

ArunJose
ArunJose

Reputation: 2174

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.

Upvotes: 1

Related Questions