Module 5 of 26 · Scikit-Learn Machine Learning · Beginner

Logistic Regression

Duration: 5 min

This module delves into Logistic Regression, a powerful algorithm for binary classification tasks. It is essential for understanding how to model the probability of a binary outcome based on one or more predictor variables. Logistic Regression is widely used in various fields, including finance, medicine, and marketing, for making predictions and informed decisions.

Understanding Logistic Regression

Logistic Regression is a statistical method for analyzing a dataset in which there are one or more independent variables that determine an outcome. The outcome is measured with a dichotomous variable (in which there are only two possible outcomes). It estimates the relationship between the independent variables and the likelihood of the dependent variable.

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification

# Generate a binary classification dataset
X, y = make_classification(n_samples=100, n_features=2, n_informative=2, n_redundant=0, random_state=42)

# Initialize and train the Logistic Regression model
model = LogisticRegression()
model.fit(X, y)

# Predict the class for a new observation
new_observation = [[0, 2]]
prediction = model.predict(new_observation)
print('Prediction:', prediction)

Try it in Google Colab: Open in Colab

Prediction: [1]

Evaluating Logistic Regression Models

Evaluating the performance of a Logistic Regression model is crucial to ensure its effectiveness. Common evaluation metrics include accuracy, precision, recall, and the F1 score. Additionally, the confusion matrix provides a detailed breakdown of the model's predictions, helping to identify any biases or issues.

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train the model on the training set
model.fit(X_train, y_train)

# Make predictions on the testing set
y_pred = model.predict(X_test)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)

print('Accuracy:', accuracy)
print('Confusion Matrix:\n', conf_matrix)

💡 Tip: When working with Logistic Regression, ensure that your data is properly scaled, as the algorithm is sensitive to the scale of the input features. Also, check for multicollinearity among the features, as it can affect the model's performance.

❓ What type of problems is Logistic Regression best suited for?

❓ Which metric is commonly used to evaluate the performance of a Logistic Regression model?

Key Concepts

Concept Description
Sigmoid Function Core principle in this module
Log Loss Core principle in this module
Decision Boundary Core principle in this module
Probability Core principle in this module

Check Your Understanding

❓ How does Logistic handle edge cases?

❓ What is the computational complexity of Logistic?

❓ Which hyperparameter is most critical for Logistic?

← Previous Continue interactively → Next →

Related Courses