Home > Back-end >  How to set penalty of elastic net in Logistic Regression
How to set penalty of elastic net in Logistic Regression

Time:02-04

I was currently working on one of the assignments from the IBM Machine learning course. I ended up getting one error multiple times while training the model even when I set penalty = 'elasticnet'. I know that the elastic net model needs an L1 ratio and I am not even sure that I need to set the l1_ratio at all or where should I set the L1_ratio. The code I was working on is below:

#defining Logistic Regression with Elastic Net penalty
l1_ratio=0.5
#elastic net penalty to shrink coefficients without removing any features from the model
penalty= 'elasticnet'
# Our classification problem is multinomial
multi_class = 'multinomial'
#Use saga for elastic net penalty and multinomial classes.  sklearn only support saga for elastic net
solver = 'saga'
#setting max iteration to 1000
max_iter = 1000
#Initiating the LogisticRegression and training the model
e_net_model = LogisticRegression(random_state=rs, penalty=penalty, multi_class=multi_class, solver=solver, max_iter = 1000)
#training
e_net_model.fit(X_train, y_train) 

Error I was having while fitting the model:

TypeError                                 Traceback (most recent call last)
Input In [60], in <cell line: 2>()
      1 # Type your code here
----> 2 e_net_model.fit(X_train, y_train)

File ~\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:1291, in LogisticRegression.fit(self, X, y, sample_weight)

Picture of the Error

CodePudding user response:

Try this code:

from scipy.sparse.construct import rand
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, n_classes=3, random_state=1)
#defining Logistic Regression with Elastic Net penalty
l1_ratio=0.5
#elastic net penalty to shrink coefficients without removing any features from the model
penalty= 'elasticnet'
# Our classification problem is multinomial
multi_class = 'multinomial'
#Use saga for elastic net penalty and multinomial classes.  sklearn only support saga for elastic net
solver = 'saga'
#setting max iteration to 1000
max_iter = 1000
#Initiating the LogisticRegression and training the model
e_net_model = LogisticRegression(random_state=0, penalty=penalty, multi_class=multi_class, solver=solver, max_iter = 1000,l1_ratio=l1_ratio)
#training
e_net_model.fit(X, y) 
# define a single row of input data
row = [1.89149379, -0.39847585, 1.63856893, 0.01647165, 1.51892395, -3.52651223, 1.80998823, 0.58810926, -0.02542177, -0.52835426]
# predict the class label
yhat = e_net_model.predict([row])
# summarize the predicted class
print('Predicted Class: %d' % yhat[0])

Also, make sure that your training data doesn't have missing values.

  • Related