Overview
This class covered foundational methods for evaluating and training supervised learning models. It began with data splitting techniques, comparing the holdout method and k-fold cross-validation, emphasizing their application to sequential versus non-sequential data. The lecture then introduced supervised learning's two main categories—regression and classification—and focused on linear regression. It detailed the model's equation, the concept of error calculation (absolute and squared), and the objective of minimizing error through methods like least squares and optimizers such as gradient descent. This session builds the groundwork for model training and evaluation, leading into more complex regression variants.
Learning outcomes
- Explain and contrast the holdout and k-fold cross-validation methods for data splitting.
- Distinguish between sequential and non-sequential data and apply appropriate splitting techniques.
- Define linear regression and write its equation for simple, multiple, and polynomial cases.
- Describe the training process for a linear regression model, including weight initialization, prediction, and error calculation.
- Calculate total absolute error, sum of squared error (SSE), mean absolute error, and mean squared error for a given set of predictions.
- Explain the role of an optimizer (e.g., gradient descent) in updating model coefficients to minimize error.
Core concepts
Data Splitting for Model Evaluation
Splitting a dataset into training and testing sets is crucial to evaluate a model's performance on unseen data. The holdout method uses a single, fixed split, while k-fold cross-validation rotates the test set across k partitions, providing a more robust evaluation by using each data point for both training and testing across iterations.
Sequential vs. Non-Sequential Data
The order of data points matters for sequential data (e.g., time series, text), requiring a sequential split to preserve temporal dependencies. For non-sequential data (e.g., images), the split can be random. This distinction dictates the splitting strategy in both holdout and k-fold methods.
Supervised Learning: Regression vs. Classification
Supervised learning involves predicting a target variable from input features. Regression predicts a continuous target (e.g., price, salary), while classification predicts a categorical target (e.g., spam/not spam). This class focused on regression, specifically linear models.
Linear Regression Model
A linear regression model assumes a linear relationship between input variables (features) and a continuous output variable. It is represented by a line (in 2D) or a hyperplane (in higher dimensions). The model's parameters are coefficients (weights) and an intercept (bias), which are learned from data.
Error Calculation and Minimization
Error is the difference between the model's prediction and the actual target value. Aggregating errors (e.g., via sum of squared error) provides a metric for model accuracy. The training process aims to find the model parameters that minimize this error, using methods like the least squares approach or iterative optimizers.
Optimizers in Model Training
An optimizer is an algorithm that automatically adjusts the model's coefficients to reduce the total error iteratively. Gradient descent is a fundamental optimizer that updates weights based on the error gradient, moving the model toward the minimum error configuration without manual intervention.
Formulas and maths
Linear Regression Equation (Simple)
y = θ₀ + θ₁x₁
- y: Output/target variable (continuous).
- x₁: Input variable/feature.
- θ₀: Intercept/bias term.
- θ₁: Coefficient/slope for x₁.
- Used to define the predicted output for a single input feature.
Linear Regression Equation (Multiple)
y = θ₀ + θ₁x₁ + θ₂x₂ + ... + θₙxₙ
- x₁, x₂, ..., xₙ: Multiple input variables/features.
- θ₁, θ₂, ..., θₙ: Coefficients for each feature.
- Used when the output depends on more than one input feature.
Error for a Single Observation
error = target value – predicted value
- target value: Actual value from the dataset (ground truth).
- predicted value: Value estimated by the model.
- Calculated for each data point to measure prediction accuracy.
Sum of Squared Error (SSE)
SSE = (error₁)² + (error₂)² + ... + (errorₙ)²
- errorᵢ: Error for the i-th observation.
- n: Number of observations.
- Used in the least square method to aggregate errors, ensuring all contributions are positive.
Mean Squared Error (MSE)
MSE = SSE / n
- SSE: Sum of squared error.
- n: Number of observations.
- Represents the average squared error; a common loss function to minimize during training.
Mean Absolute Error (MAE)
MAE = (|error₁| + |error₂| + ... + |errorₙ|) / n
- |errorᵢ|: Absolute value of the error for the i-th observation.
- n: Number of observations.
- An alternative error metric that averages the magnitude of errors without squaring.
Worked examples
Holdout Method Data Split
For a dataset of 1 crore (10 million) observations:
- Training set: ~75 lakh (7.5 million) observations (75%).
- Test set: ~25 lakh (2.5 million) observations (25%).
k-Fold Cross-Validation (k=5)
Dataset is divided into 5 equal partitions (folds), each containing 20% of the data.
- Iteration 1: Fold 1 as test set, Folds 2-5 combined as training set.
- Iteration 2: Fold 2 as test set, Folds 1,3,4,5 as training set.
- This repeats for 5 iterations, ensuring each fold serves as the test set once.
Linear Regression Prediction and Error
Model equation: price = b₀ + b₁ * size, with initialized weights b₀ = 25, b₁ = 15.
For a house of size 1000 sq ft:
Predicted price = 25 + 15 * 1000 = 15025 (units).
If the target price is 35,000:
Error = 35000 - 15025 = 19975.
Error Aggregation Example
Given four observations with errors: 5, -3, 0, 4.
- Total absolute error = |5| + |-3| + |0| + |4| = 12.
- Sum of squared error (SSE) = 5² + (-3)² + 0² + 4² = 25 + 9 + 0 + 16 = 50.
- Mean absolute error (MAE) = 12 / 4 = 3.
- Mean squared error (MSE) = 50 / 4 = 12.5.
Questions asked in class
Q: How is k-fold cross-validation better than holdout?
A: In k-fold, each data point is used for both training and testing across iterations, leading to a more comprehensive model evaluation and better utilization of data.
Q (Muthu): How is k-fold feasible with large datasets compared to holdout?
A: Both methods use the same total dataset size. k-fold increases the number of training iterations but does not require more data, which can improve learning without additional data collection.
Q (G1): Is splitting in k-fold random or sequential?
A: It depends on the data type. For sequential data (e.g., IoT, stock market), the split must be sequential to preserve order. For non-sequential data (e.g., images), the split can be random.
Q: How to calculate the coefficients (θ₀, θ₁)?
A: Not manually. The training process (e.g., least square method or gradient descent optimizer) estimates these values automatically using the dataset.
Q: Why is the intercept called bias?
A: It is a terminological convention in machine learning; there is no specific scientific reason behind this naming.
Q: Clarification on variables in the linear regression equation.
A: y is the output/target variable (e.g., CTC, price). xᵢ are input variables/features. θ₀ (or b₀, w₀) is the bias/intercept and is not associated with any input feature.
Q: How to draw a random line for model initialization?
A: Assume random values for the coefficients (e.g., b₀, b₁) or initialize them with zeros or small numbers.
Q: Why square the error?
A: Squaring ensures negative errors become positive so they do not cancel with positive errors when summed, preventing a misleading total error near zero.
Q: How to reduce error?
A: Draw many lines (models), calculate the total error for each, and select the line with the minimum error. An optimizer automates this search.
Q: Can we have multiple input variables?
A: Yes, that is multiple linear regression, which involves a plane or hyperplane instead of a line.
Q: What if the relationship is not linear?
A: Use polynomial regression (curve fitting) by introducing polynomial terms (e.g., x², x³) determined through hit-and-try.
Key takeaways
- The holdout method splits data once into fixed training (~70-75%) and test (~25-30%) sets.
- k-fold cross-validation partitions data into k equal folds, using each fold as a test set once across k iterations, improving evaluation robustness.
- For sequential data (time series, text), splitting must preserve order; for non-sequential data (images), splitting can be random.
- Supervised learning is divided into regression (continuous target) and classification (categorical target).
- A linear regression model predicts a continuous output as a linear combination of input features plus an intercept.
- Model training starts with weight initialization (random, zeros, or small values) and involves predicting outputs and calculating errors.
- Error can be aggregated as total absolute error or sum of squared error (SSE); mean absolute error (MAE) and mean squared error (MSE) are averages.
- The least square method minimizes the average sum of squared error to find the best-fitting model.
- An optimizer like gradient descent automatically updates model coefficients to iteratively reduce total error.
- Multiple linear regression extends the model to multiple input variables, producing a hyperplane.
- Polynomial regression models non-linear relationships by adding polynomial terms (e.g., squared, cubic) to the equation.
- The intercept (
θ₀) is also called the bias in machine learning terminology.
Exam-style questions
-
Compare and contrast the holdout method and k-fold cross-validation for model evaluation. Include a discussion on when each might be preferred.
Answer outline: Holdout uses a single split; k-fold uses multiple rotations. Holdout is simpler and faster; k-fold provides more reliable performance estimates, especially with limited data. For very large datasets, holdout may be sufficient; for smaller datasets or when robust evaluation is critical, k-fold is preferred. -
Given a linear regression model
price = 10 + 5 * sizeand the following data points (size, price): (2, 25), (4, 30), calculate the predicted prices, individual errors, sum of squared error (SSE), and mean squared error (MSE).
Answer outline: For (2,25): predicted = 20, error = 5. For (4,30): predicted = 30, error = 0. SSE = 5² + 0² = 25. MSE = 25 / 2 = 12.5. -
Explain the steps involved in training a linear regression model using the least square method, from weight initialization to selecting the final model.
Answer outline: 1) Initialize coefficients (e.g., randomly). 2) Use the model to predict outputs. 3) Calculate errors (target - predicted). 4) Compute the sum of squared errors (SSE). 5) Adjust coefficients (via an optimizer) to minimize SSE. 6) Repeat until error is minimized, selecting the model with the least error. -
Describe the difference between sequential and non-sequential data. Provide an example of each and explain how data splitting should be handled in each case.
Answer outline: Sequential data order matters (e.g., stock prices over time); splitting must be sequential (e.g., first 80% for training, next 20% for testing). Non-sequential data order does not matter (e.g., images of cats and dogs); splitting can be random. -
A polynomial regression model is proposed to predict salary based on age, with equation
salary = θ₀ + θ₁*age + θ₂*age². Why might this be more suitable than simple linear regression for this problem?
Answer outline: The relationship between salary and age is often non-linear (e.g., increases then plateaus). The squared term (age²) allows the model to capture curvature, providing a better fit than a straight line.
Glossary
- Holdout method - Data splitting technique using a single, fixed partition into training and test sets.
- k-fold cross-validation - Data splitting technique dividing data into k folds, using each fold as a test set once.
- Sequential data - Data where order matters, such as time series or text sequences.
- Non-sequential data - Data where order does not matter, such as independent images.
- Supervised learning - Machine learning task where models learn from labeled data.
- Regression - Supervised learning predicting a continuous target variable.
- Classification - Supervised learning predicting a categorical target variable.
- Linear regression - A regression model assuming a linear relationship between inputs and output.
- Coefficients/Weights - Parameters in a model that are multiplied by input features.
- Bias/Intercept - The constant term in a linear model, representing the output when all inputs are zero.
- Error - Difference between predicted value and actual target value.
- Sum of squared error (SSE) - Sum of the squares of individual errors.
- Mean squared error (MSE) - Average of the squared errors (SSE divided by number of observations).
- Least square method - Training approach minimizing the sum of squared errors.
- Optimizer - Algorithm that updates model parameters to minimize error (e.g., gradient descent).
- Multiple linear regression - Linear regression with more than one input variable.
- Polynomial regression - Regression using polynomial functions of inputs to model non-linear relationships.
Follow-ups
- How to decide the value of
kin k-fold cross-validation (depends on dataset size and number of features). - Detailed coverage of sequential vs. non-sequential data (to be revisited during NLP topics).
- Gradient descent and its variants (stochastic gradient descent, Adam, batch gradient descent) are part of deep learning and will be covered later.
- Logistic regression (a classification method) will be covered in the next class.
- Hands-on coding implementation of linear regression is pending.
- Assumptions and data preparation for linear regression models are for self-study.
- Polynomial linear regression details, including determining polynomial degree via hit-and-try, require further exploration.
- Other training methods for linear regression beyond least squares are not covered here.
- Next class is scheduled for Saturday; no class on 8th September.