🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Logistic Regression — Complete Guide for Classification

ML FoundationsClassification🟢 Free Lesson

Advertisement

Supervised Learning

Classification with Probability — From Linear to Sigmoid

Logistic regression transforms linear outputs into probabilities using the sigmoid function. It is the foundation of classification in machine learning.

  • Sigmoid Function — Map any real number to a probability between 0 and 1
  • Cross-Entropy Loss — The cost function that powers classification training
  • Multiclass Extension — Softmax regression for multiple classes

"The goal is to turn data into information, and information into insight."

Logistic Regression — Complete Guide for Classification

Despite its name, logistic regression is a classification algorithm. It predicts the probability that an input belongs to a class.


From Linear to Logistic Regression

Sigmoid Function: σ(z) = 1 / (1 + e⁻ᶻ)zσ(z)1.00.50.0-6-336z=0 → σ=0.5Inflection pointKey Propertiesσ(0) = 0.5σ(z) → 1 as z → +∞σ(z) → 0 as z → −∞Derivative:σ'(z) = σ(z)(1 − σ(z))Output range:(0, 1) — valid probabilityDecision rule:σ(z) ≥ 0.5 → Class 1σ(z) < 0.5 → Class 0

Cost Function

Cross-Entropy Loss BehaviorWhen y = 1 (True label)ŷ (predicted probability)010L = −log(ŷ)ŷ=1 → L=0ŷ=0 → L=∞When y = 0 (True label)ŷ (predicted probability)L = −log(1−ŷ)ŷ=0 → L=0ŷ=1 → L=∞Cross-entropy heavily penalizes confident wrong predictions — the gradient is largest when the model is most wrong

Decision Boundary

Decision Boundary in 2D Feature Spacex₁ (feature 1)x₂ (feature 2)w₁x₁ + w₂x₂ + b = 0Class 0Class 1Multi-class: SoftmaxSoftmax converts logits to probabilities:softmax(zᵢ) = eᶻⁱ / Σⱼ eᶻʲExample with 3 classes:z = [2.0, 1.0, 0.1]eᶻ = [7.39, 2.72, 1.11]softmax = [0.659, 0.242, 0.099]Sum = 1.0 ✓Predicted: Class 0 (highest probability)Output is a valid probability distribution over K classes

Gradient Derivation


Python Implementation

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = LogisticRegression(C=1.0, penalty='l2', solver='lbfgs')
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]

from sklearn.metrics import accuracy_score, roc_auc_score
print(f"Accuracy: {accuracy_score(y_test, y_pred):.3f}")
print(f"AUC-ROC: {roc_auc_score(y_test, y_prob):.3f}")

Key Takeaways


What to Learn Next

-> Linear Regression From scatter plots to predictions — the simplest ML algorithm.

-> Naive Bayes Bayes' theorem in action — fast, simple, surprisingly powerful.

-> SVM Finding the optimal boundary — maximum margin classification.

Need Expert Machine Learning Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement