0

Can anyone help me? The root problem is that i have recently installed statsmodels library in JupyterNotebook. Now when i try to execute my code it says there is no module named statsmodels.genmod.penalties. I think i am importing L1 penalty in the wrong way. But I coudn't the correct implementation either.
Below is the code i am struggling with,

import statsmodels.api as sm
from statsmodels.genmod.families import links 
from statsmodels.genmod.generalized_linear_model import GLM 
from statsmodels.genmod.cov_struct import CovStruct 
from statsmodels.genmod.families.family import Binomial, NegativeBinomial, Gamma, Gaussian, InverseGaussian, Poisson, Tweedie 
from statsmodels.genmod.families.varfuncs import Binomial as Binomial_VarFunc 
from statsmodels.genmod.families.links import Logit 
from statsmodels.genmod.generalized_linear_model import *
from statsmodels.genmod.penalties import L1

import numpy as np

np.random.seed(123)

n = np.random.randint(0,2000)

#sample size
p = 20

#no. of variables
sigma = np.full((p,p), 0.5)

# True covariance matrix
np.fill_diagonal(sigma, 1)

X = np.random.multivariate_normal(mean = np.zeros(p), cov = sigma, size = n)

# generate mutlivariate normal data
y = (np.random.rand(n) < 0.5).astype(int)

X = sm.add_constant(X)

penalty = sm.penalties.L1(alpha = 0.1) # use L1 penalty with alpha parameter

model = sm.GLM(y, X, family = sm.families.Binomial(link = sm.families.links.logit), penalized = True, penalty = penalty)

result = model.fit()

print(result.summary())

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[21], line 23
     19 y = (np.random.rand(n) < 0.5).astype(int)
     21 X = sm.add_constant(X)
---> 23 penalty = sm.penalties.L1(alpha = 0.1) # use L1 penalty with alpha parameter
     25 model = sm.GLM(y, X, family = sm.families.Binomial(link = sm.families.links.logit), penalized = True, penalty = penalty)
     27 result = model.fit()
AttributeError: module 'statsmodels.api' has no attribute 'penalties'

and if I removed sm from sm.penalties.L1 then I get this error.

NameError                                 Traceback (most recent call last)
Cell In[22], line 23
     19 y = (np.random.rand(n) < 0.5).astype(int)
     21 X = sm.add_constant(X)
---> 23 penalty = penalties.L1(alpha = 0.1) # use L1 penalty with alpha parameter
     25 model = sm.GLM(y, X, family = sm.families.Binomial(link = sm.families.links.logit), penalized = True, penalty = penalty)
     27 result = model.fit()
NameError: name 'penalties' is not defined
  • I think you imported `L1` from this line `from statsmodels.genmod.penalties import L1`. Have you tried removing both `sm` and `penalties` ? And then you have `L1(alpha = 0.1)`. – GenZ Aug 14 '23 at 14:04

0 Answers0