2

I am getting positional arguments error for the ols function under statsmodels.formula.api

have tried for statsmodels.regression.linear_model and changing OLS to ols and vice-versa.

import statsmodels.regression.linear_model as sm

X = np.append(arr=np.ones((50,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).fit()

Expected output is the fitting for the regression model. But I am getting an this error:

from_formula() missing 2 required positional arguments: 'formula' and 'data'

desertnaut
  • 57,590
  • 26
  • 140
  • 166
sarvesh
  • 21
  • 1
  • 3
  • what happens with: `regressor_OLS = sm.ols(X_opt, Y).fit()` – PV8 Sep 12 '19 at 13:13
  • TypeError: from_formula() missing 2 required positional arguments: 'formula' and 'data' – sarvesh Sep 15 '19 at 17:33
  • I am trying to execute backward elimination:: – sarvesh Sep 15 '19 at 17:34
  • Y - represents the set of targets. While, X_opt represents the Data. So basically, I am not understanding how the positional arguments are incorrect. Is it version problem? Thanks. – sarvesh Sep 15 '19 at 17:36

8 Answers8

3

To get this example to work (I am assuming you are running the udemy machine learning course, which is line for line this example) I had to change the import statement. The library they are using is not where the OLS function resides any longer.

import statsmodels.regression.linear_model as lm

then

regressor_ols = lm.OLS(endog = y, exog = x_optimal).fit()
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
auticus
  • 183
  • 2
  • 13
1

This should work :

import statsmodels.api as smf;

X = np.append(arr=np.ones((50,1),dtype=np.int), values = X,axis = 1)

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

regressor_ols = smf.OLS(y,X_opt).fit()
nassim
  • 1,547
  • 1
  • 14
  • 26
  • This does not work. Running that solution you get "AttributeError: module 'statsmodels.formula' has no attribute 'OLS' – auticus Sep 24 '19 at 14:20
0

Remove

import statsmodels.regression.linear_model as sm

And just import statsmodels.api as following

import statsmodels.api as sm

The course is quite old and that's why fragments of code is obsolete, no idea why they are not updating it anymore.

0

Guys this module is part of Linear_model class so use following code to make it work.

import statsmodels.regression.linear_model as lm
X = np.append(arr=np.ones((50,1)).astype(int),values=X,axis=1)

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

regressor_OLS = lm.OLS(endog = y, exog = X_opt).fit()
B. Go
  • 1,436
  • 4
  • 15
  • 22
0

Use import statsmodels.regression.linear_model as lm or import statsmodels.api as sm

import statsmodels.regression.linear_model as lm

X=np.append(arr=np.ones((50,1)).astype(int), values=X, axis=1)
X_opt=X[:,[0, 1, 2, 3, 4, 5]]
regressor_x=sm.OLS(endog=y, exog=X_opt).fit()
regressor_x.summary()
Dima Gimburg
  • 1,376
  • 3
  • 17
  • 35
0

this one worked for me

import statsmodels.api as sm

X=np.insert(X,0,np.ones(X.shape[0]),axis=1)

colList=list()

for i in range(X.shape[1]):
    colList.append(i)

X_opt=np.array(X[:, colList], dtype=float)

regressor_OLS=sm.OLS(endog=y,exog=X_opt).fit()
0

Solution 1:

import statsmodels.api as sm
x = np.append(arr= np.ones((50, 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).fit()

Solution 2:

import statsmodels.regression.linear_model as lm
x = np.append(arr= np.ones((50, 1)).astype(int), values= x, axis=1)
x_opt = x[:, [0,1,2,3,4,5]]
regressor_ols = lm.OLS(endog=y, exog=x_opt).fit()
0

I recently had the same problem, as auticus said, the library with the OLS function is no longer in statsmodels.formula.api. But you also must create X_otp as a list

import statsmodels.regression.linear_model as lm
X = np.append(arr = np.ones((50,1)).astype(int), values = X, axis = 1)
X_opt = X[:, [0, 1, 2, 3, 4, 5]].tolist()
SL = 0.05
regression_OLS = lm.OLS(endog = y, exog = X_opt). fit()