Logistic Regression
Master Logistic Regression for binary classification tasks.
60 min•By Priygop Team•Last updated: Feb 2026
What is Logistic Regression?
Logistic Regression is a supervised learning algorithm used for binary classification problems. Despite its name, it's a classification algorithm, not a regression algorithm.
Key Concepts
- Binary Classification: Predicts two classes (0 or 1)
- Sigmoid Function: Maps any real number to (0,1)
- Decision Boundary: Threshold for classification
- Cost Function: Log Loss (Cross-entropy)
Sigmoid Function
- Mathematical Definition: σ(z) = 1 / (1 + e^(-z))
- Where z = β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ
Implementation Example
Example
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
# Generate sample data
np.random.seed(42)
X = np.random.randn(100, 2)
y = (X[:, 0] + X[:, 1] > 0).astype(int)
# Split the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create and train the model
model = LogisticRegression()
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.4f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))Classification Metrics
Example
from sklearn.metrics import precision_score, recall_score, f1_score, roc_auc_score
# Precision, Recall, F1-Score
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
# ROC-AUC Score
y_pred_proba = model.predict_proba(X_test)[:, 1]
roc_auc = roc_auc_score(y_test, y_pred_proba)
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1-Score: {f1:.4f}")
print(f"ROC-AUC: {roc_auc:.4f}")