Get startedGet started for free

Fit logistic regression with L1 regularization

You will now run a logistic regression model on scaled data with L1 regularization to perform feature selection alongside model building. In the video exercise you have seen how the different C values have an effect on your accuracy score and the number of non-zero features. In this exercise, you will set the C value to 0.025.

The LogisticRegression and accuracy_score functions from sklearn library have been loaded for you. Also, the scaled features and target variables have been loaded as train_X, train_Y for training data, and test_X, test_Y for test data.

This exercise is part of the course

Machine Learning for Marketing in Python

View Course

Exercise instructions

  • Initialize a logistic regression with L1 regularization and C value of 0.025.
  • Fit the model on the training data.
  • Predict churn values on the test data.
  • Print the accuracy score of your predicted labels on the test data.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize logistic regression instance 
logreg = ___(penalty='l1', ___=0.025, solver='liblinear')

# Fit the model on training data
logreg.___(train_X, ___)

# Predict churn values on test data
pred_test_Y = logreg.predict(___)

# Print the accuracy score on test data
print('Test accuracy:', round(accuracy_score(test_Y, ___), 4))
Edit and Run Code