Course contentsShow
← Machine Learning and Deep Learning
Lesson 255 of 3,5384. Classification with Logistic RegressionFree lesson

Logistic Regression with Scikit-learn

Training and configuring logistic regression models using sklearn's LogisticRegression class.

Logistic Regression with Scikit-learn

What you'll learn: How to train and configure logistic regression models using Scikit-learn's LogisticRegression class in just a few lines of code.

From Theory to Practice

You've learned the mathematical foundations of logistic regression—the sigmoid function, binary cross-entropy loss, gradient descent, and regularization. Now it's time to put that knowledge into action using Scikit-learn's powerful LogisticRegression class, which handles all the computational heavy lifting for you.

Using the LogisticRegression Class

The LogisticRegression class follows Scikit-learn's familiar API pattern. You instantiate the model, fit it to your training data, and make predictions:

from sklearn.linear_model import LogisticRegression

# Create the model with default settings
model = LogisticRegression()

# Fit to training data (X_train, y_train)
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)  # Get probabilities

Key Configuration Parameters

The LogisticRegression class offers several important parameters:

  • penalty: Choose regularization type ('l2' for Ridge, 'l1' for Lasso, 'elasticnet', or 'none')
  • C: Inverse of regularization strength (smaller values = stronger regularization)
  • solver: Algorithm for optimization ('lbfgs', 'saga', 'liblinear')
  • max_iter: Maximum iterations for convergence
# Example with custom configuration
model = LogisticRegression(penalty='l2', C=1.0, max_iter=100)

Key Takeaway: Scikit-learn's LogisticRegression class provides a production-ready implementation that handles optimization, regularization, and prediction seamlessly, letting you focus on model evaluation rather than mathematical implementation details.