IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

Advance Machine Learning

26 Sep 2026 · 09:261h 24m3 parts

Source: IITP-big-files\Sem-1\AML\Lectures\2026-09-26 0926 Advance Machine Learning\summary\notes.md

Overview

This class introduced logistic regression for binary classification, distinguishing it from linear regression. It covered the mathematical formulation using the sigmoid function, explained logistic regression as a probabilistic model, and detailed the training process via gradient descent to minimize binary cross-entropy loss. The session connected theoretical concepts to practical implementation, including a from-scratch Python example and a comparison with scikit-learn's library for a customer churn prediction problem. This builds foundational knowledge for classification algorithms preceding deep learning modules.

Learning outcomes

  • Differentiate between regression and classification problems.
  • Explain why logistic regression is used for binary classification and how the sigmoid function enables probability outputs.
  • Implement logistic regression from scratch using gradient descent.
  • Use scikit-learn's logistic regression module for a binary classification task.
  • Calculate error and update model weights using the gradient descent rule.
  • Set a classification threshold to convert probabilities into class labels.
  • Perform basic Exploratory Data Analysis (EDA) for a classification dataset.

Core concepts

Regression vs. Classification

Regression predicts a continuous dependent variable, while classification predicts a discrete label. This distinction determines the choice of algorithm (e.g., linear regression vs. logistic regression).

Logistic Regression

A classification algorithm used to predict a binary outcome. It uses a linear combination of inputs passed through a sigmoid function to output a probability between 0 and 1.

Sigmoid/Logistic Function

A mathematical function that maps any real-valued input to an S-shaped curve between 0 and 1. It is the core mechanism in logistic regression for generating probabilities.

Probabilistic Model

Logistic regression outputs the probability of an instance belonging to a class. A threshold (typically 0.5) is then applied to make a final class prediction.

Gradient Descent

An iterative optimization algorithm used to minimize the loss function (binary cross-entropy) by updating model weights in the direction opposite to the gradient of the loss.

Binary Cross-Entropy Loss

The loss function used specifically for binary classification in logistic regression. It measures the difference between the predicted probability distribution and the actual labels.

Learning Rate

A hyperparameter in gradient descent that controls the step size of each weight update. A critical value that must be tuned—too high risks divergence, too low slows convergence.

Threshold

A cutoff value (e.g., 0.5) applied to the sigmoid output probability to decide the final class prediction (0 or 1).

Formulas and maths

  • General Sigmoid/Logistic Function:
    ( \text{sigmoid}(z) = \frac{1}{1 + e^{-z}} )
    Symbols: ( z ) is the linear input ( \beta_0 + \beta_1 x ) or ( \theta^T x ).
    Use: Converts any real number to a probability between 0 and 1 in logistic regression.

  • Alternative Logistic Function Form:
    ( \frac{L}{1 + e^{-k(x - x_0)}} )
    Symbols: ( L ) is the curve's maximum value, ( k ) is stiffness (slope), ( x_0 ) is the midpoint.
    Use: General form; in standard logistic regression, ( L=1 ) and the midpoint is set for 0.5 probability.

  • Logistic Regression Linear Input:
    ( z = \theta^T x )
    Symbols: ( \theta ) is the weight vector, ( x ) is the feature vector, ( T ) denotes transpose.
    Use: Computes the weighted sum of inputs before applying the sigmoid function.

  • Binary Cross-Entropy Loss:
    ( L = -\frac{1}{m} \sum_{i=1}^{m} [y^{(i)} \log(h^{(i)}) + (1-y^{(i)}) \log(1-h^{(i)})] )
    Symbols: ( m ) is number of samples, ( y ) is actual label (0 or 1), ( h ) is predicted probability from sigmoid.
    Use: The loss function minimized during training of logistic regression.

  • Gradient Descent Weight Update (Descent):
    ( \theta_{\text{new}} = \theta_{\text{old}} - \alpha \frac{dL}{d\theta} )
    Symbols: ( \alpha ) is the learning rate, ( \frac{dL}{d\theta} ) is the gradient of the loss with respect to the weights.
    Use: Iteratively updates weights to reduce loss.

  • Gradient Ascent Weight Update:
    ( \theta_{\text{new}} = \theta_{\text{old}} + \alpha \frac{dL}{d\theta} )
    Symbols: Same as above.
    Use: An alternative update rule; choice between descent and ascent depends on error behavior.

  • Error Calculation:
    ( \text{error} = y_{\text{actual}} - y_{\text{predicted}} )
    Symbols: ( y_{\text{actual}} ) is true label, ( y_{\text{predicted}} ) is sigmoid output probability.
    Use: Computes the immediate difference for a single prediction.

Worked examples

  1. Probability Complement: If predicted probability for class A is 0.78, then probability for the other class is ( 1 - 0.78 = 0.22 ).
  2. Threshold Application: With threshold = 0.5, a sigmoid output of 0.7 (>0.5) predicts class A (e.g., cat); an output of 0.3 (<0.5) predicts class B (e.g., dog).
  3. From-Scratch Implementation Steps:
    • Input x = [1, 1, 0] (shape 3x1).
    • Initial random weights θ = [1, 1, 1].
    • Compute z = θ^T x (may require transpose for dimension matching).
    • Apply sigmoid: h = 1 / (1 + e^{-z}). Example output: 0.7.
    • If actual label y = 1, error = 1 - 0.7 = 0.3.
    • With learning rate α = 0.01 and derivative dL/dθ = 0.2, update weight: θ_new = θ_old - 0.01 * 0.2.
    • Next iteration uses updated θ, yielding a new prediction (e.g., 0.72) and reduced error.
  4. Churn Prediction Dataset:
    • Dataset dimensions: 7043 rows, 21 columns (later noted as 25).
    • Target distribution: 'No' = 5174 rows, 'Yes' = 1869 rows.
    • Selected features: Tenure and Monthly Charges.
    • Added intercept column: np.ones to create squared matrix.
    • Initial weights (θ): np.zeros.
    • Learning rate: 0.1, Iterations: 100,000.
    • Training time measured with time.time().
    • From-scratch model accuracy: 77.93% (~78%).
    • scikit-learn model accuracy: ~78%.

Questions asked in class

  • Q: Can't binary classification problem be solved for the multi-class problem?
    A: No, the sigmoid function is for binary classification only. Multi-class problems require other methods.
  • Q: Why not define the threshold for multi-class?
    A: Logistic regression is probabilistic and finds only one class probability at a time; the other is simply (1-p). Setting multiple thresholds isn't feasible.
  • Q: Logistic regression is called a regression because…
    A: It uses a regression line (( \beta_0 + \beta_1 x )) but fits it between 0 and 1 via the sigmoid, hence the name.
  • Q: Can we use linear regression for classification?
    A: No, linear regression outputs any real value (e.g., 500, -200), not suitable for 0/1 class labels.
  • Q: Why is gradient descent important?
    A: It updates weights to reduce error by following the derivative of the loss.
  • Q: What if learning rate is too high or too low?
    A: Too high: may overshoot and never converge. Too low: takes too long to converge.
  • Q: How do you come up with the loss equation?
    A: Binary cross-entropy is standard, combining ( y \log(h) ) for the positive class and ( (1-y) \log(1-h) ) for the negative class.
  • Q: Is gradient descent the only method?
    A: No, Maximum Likelihood Estimation is an alternative, but gradient descent is common and extends to deep learning.
  • Q: Why do we need learning rate in gradient descent?
    A: It decides the step size/momentum for moving towards the error minimum.
  • Q: When do we use transpose?
    A: When matrix dimensions are incompatible for dot product (e.g., converting 3x1 to 1x3).
  • Q: What is the difference between gradient descent and gradient ascent?
    A: Descent uses minus sign (θ - α * gradient), ascent uses plus sign (θ + α * gradient). Choice depends on error behavior.

Key takeaways

  • Regression predicts continuous values; classification predicts discrete labels.
  • Logistic regression is specifically for binary classification problems.
  • The sigmoid function outputs a probability between 0 and 1, making logistic regression a probabilistic model.
  • A threshold (typically 0.5) converts the sigmoid probability into a final class prediction (0 or 1).
  • The loss function for logistic regression is binary cross-entropy.
  • Model weights are optimized using gradient descent (or ascent) to minimize the loss.
  • The learning rate is a critical hyperparameter controlling the step size in gradient descent.
  • Transpose operations are used to make matrix dimensions compatible for dot products.
  • Implementing logistic regression from scratch involves initializing weights, computing sigmoid outputs, calculating error, and iteratively updating weights.
  • scikit-learn's logistic regression module handles gradient descent internally, requiring only features (X) and labels (y).
  • Exploratory Data Analysis (EDA), like visualizing tenure vs. churn, is a crucial step before model training.
  • For the churn dataset, using two features (Tenure, Monthly Charges) and 100k iterations yielded ~78% accuracy with both custom and scikit-learn implementations.

Exam-style questions

  1. Explain why linear regression is unsuitable for a binary classification task like spam detection, and how logistic regression addresses this limitation.
    Outline: Linear regression outputs unbounded continuous values, not probabilities. Logistic regression uses the sigmoid function to map linear outputs to probabilities between 0 and 1, enabling class prediction via a threshold.

  2. Given a logistic regression model with weights θ = [0.5, -1.2] and an input x = [1, 3], calculate the predicted probability. Assume an intercept is included in the weight vector.
    Outline: Compute z = θ^T x = (0.51) + (-1.23) = 0.5 - 3.6 = -3.1. Apply sigmoid: p = 1 / (1 + e^(-(-3.1))) = 1 / (1 + e^3.1). Calculate numerical value.

  3. Describe the role of the learning rate in gradient descent. What are the consequences of setting it too high or too low?
    Outline: Learning rate (α) controls step size. Too high causes overshooting and potential divergence; too low leads to extremely slow convergence and increased training time.

  4. For a binary classification problem, the sigmoid output for an instance is 0.85. Using a threshold of 0.5, what is the predicted class? What is the implied probability for the other class?
    Outline: Since 0.85 > 0.5, predict class 1. Probability for the other class (class 0) is 1 - 0.85 = 0.15.

  5. You are implementing logistic regression from scratch. Outline the steps in one training iteration, from computing the prediction to updating the weights.
    Outline: (1) Compute z = θ^T x. (2) Apply sigmoid to get prediction h. (3) Calculate error = y - h. (4) Compute gradient of loss dL/dθ. (5) Update weights: θ = θ - α * (dL/dθ).

Glossary

  • Regression - Supervised learning task where the target variable is continuous.
  • Classification - Supervised learning task where the target variable is discrete.
  • Logistic Regression - A binary classification algorithm that outputs probabilities via the sigmoid function.
  • Sigmoid Function - An S-shaped function mapping any input to a value between 0 and 1.
  • Binary Classification - A classification problem with exactly two possible outcomes.
  • Threshold - A cutoff probability value used to assign class labels.
  • Gradient Descent - An iterative optimization algorithm that minimizes a function by moving in the direction opposite its gradient.
  • Learning Rate - A hyperparameter in gradient descent controlling the size of update steps.
  • Binary Cross-Entropy - The loss function used for training logistic regression models.
  • Transpose - An operation that flips a matrix over its diagonal, changing its dimensions.
  • Exploratory Data Analysis (EDA) - The process of analyzing datasets through summaries and visualizations.
  • Intercept - A constant term in a linear model, often added as a column of ones to the feature matrix.
  • Maximum Likelihood Estimation - A statistical method for estimating model parameters by maximizing the likelihood function.

Follow-ups

  • Detailed explanation of gradient descent, forward propagation, and backward propagation is promised for the deep learning module/next class.
  • The instructor will upload the demo CSV data files and Jupyter notebook (ipynb) for the churn prediction example.
  • A real-time example of weight update direction (plus/minus) based on error increase/decrease will be discussed.
  • The relationship between the "logistic regression function" and "sigmoid function" will be clarified further (instructor stated they are "100 percent same" but also mentioned logistic regression is an extension).
  • The alternative logistic formula ( \frac{L}{1 + e^{-k(x - x_0)}} ) and its parameters in standard logistic regression will be elaborated.
  • Practical implementation of logistic regression using Python was mentioned as the immediate next topic.