๐งพ An Experimental Study of Underfitting and Overfitting in Supervised Machine Learning Using Polynomial Regression
To understand how model complexity affects generalization performance in supervised machine learning.
- Define and understand underfitting and overfitting
- Experimentally observe the effect of model complexity
- Analyze training error vs validation error relationships
- Understand the biasโvariance tradeoff
- Identify the optimal model complexity for generalization
In supervised machine learning, selecting an appropriate model complexity is critical for achieving good generalization performance.
The Challenge:
- Too simple models โ fail to capture underlying patterns (underfitting)
- Too complex models โ memorize noise instead of patterns (overfitting)
Research Question:
How can we experimentally observe and quantify the effects of model complexity on model generalization?
We generate synthetic nonlinear regression data using:
y = sin(x) + noise
- Function: Sinusoidal pattern to create a known nonlinear relationship
- Noise: Gaussian noise added to simulate real-world uncertainty
- Purpose: Controlled environment to study model behavior
The dataset is split into:
| Split | Purpose |
|---|---|
| Training Set | Model learning and parameter fitting |
| Validation Set | Generalization performance evaluation |
We use Polynomial Regression which allows precise control over model complexity via polynomial degree.
| Degree Range | Expected Behavior |
|---|---|
| 1 | Underfitting (too simple) |
| 4โ6 | Good fit (optimal complexity) |
| 15โ20 | Overfitting (too complex) |
We compute Mean Squared Error (MSE) for:
- Training error (performance on training data)
- Validation error (performance on unseen data)
- Visualization: Plot regression curves for different complexities
- Error Analysis: Plot training vs validation error curves
- Optimization: Identify complexity where validation error is minimized
- Theoretical: Interpret results using biasโvariance decomposition
flowchart TD
A[Start] --> B[Define Research Question]
B --> C[Generate Synthetic Dataset]
C --> D[Split Data: Train/Test]
D --> E[Select Polynomial Degrees]
E --> F[Train Model for Each Degree]
F --> G[Compute Training Error]
G --> H[Compute Validation Error]
H --> I[Create Visualizations:<br/>โข Regression Curves<br/>โข Error vs Complexity]
I --> J[Analyze BiasโVariance Behavior]
J --> K[Draw Conclusions]
K --> L[End]
Generalization Error = Biasยฒ + Variance + Irreducible Noise
| Model Complexity | Bias | Variance | Result |
|---|---|---|---|
| Low (Simple) | High โ | Low โ | Underfitting |
| Optimal | Balanced | Balanced | Good Generalization |
| High (Complex) | Low โ | High โ | Overfitting |
Model Complexity โ
Validation Error
โ
| โญโโฎ
| โฑ โฒ
| โฑ โฒ
| โฑ โฒ
| โฑ โฒ
Training |โฑ___________โฒ___
Error | โฒ
|______________โฒ____โ
Under- Optimal Over-
fitting fitting
| Component | Status | Description |
|---|---|---|
| Data Generation | โ Complete | Synthetic sin(x) + noise generation with configurable parameters |
| Model Training | โ Complete | Polynomial regression with degree control and MSE evaluation |
| Visualization | โ Complete | Regression curve plots and error curve visualization functions |
| Project Setup | โ Complete | Virtual environment, dependencies, and proper project structure |
| Documentation | โ Complete | Comprehensive README with theory and methodology |
| Jupyter Notebook | โ Complete | Interactive experiment analysis setup |
| Version Control | โ Complete | Git repository with proper .gitignore and initial commit |
- Data Generated: 100 synthetic data points using y = sin(x) + Gaussian noise
- Train/Test Split: 70/30 split for proper evaluation
- Model Degrees Tested: [1, 5, 15] representing underfitting, good fit, and overfitting
- Evaluation Metric: Mean Squared Error (MSE) for both training and test sets
- Visualization: Ready to generate regression curves and error plots
-
Run Full Experiment
- Execute complete polynomial degree sweep (1-20)
- Generate training vs validation error curves
- Create regression curve visualizations for key degrees
- Document numerical results and observations
-
Results Analysis
- Identify optimal polynomial degree
- Quantify bias-variance tradeoff observations
- Create comprehensive plots showing underfitting โ optimal โ overfitting transition
-
Parameter Sensitivity Study
- Test different noise levels (0.1, 0.2, 0.5, 1.0)
- Vary dataset sizes (50, 100, 200, 500 samples)
- Compare results across different random seeds
-
Advanced Visualizations
- Create animated plots showing complexity progression
- Generate bias-variance decomposition plots
- Add confidence intervals and error bars
-
Results Documentation
- Update README with actual experimental findings
- Create executive summary of key insights
- Document lessons learned and theoretical confirmations
-
Report Generation
- Compile comprehensive experiment report
- Create presentation slides summarizing findings
- Prepare code demonstrations and walkthroughs
- Python 3.8 or higher
- Virtual environment (recommended)
# Clone or download the project
cd overfitting_ml
# Activate virtual environment
source .venv/bin/activate
# Install dependencies (within the activated virtual environment)
pip install -r requirements.txt# Make sure virtual environment is activated
source .venv/bin/activate
# Option 1: Run individual modules for testing
python3 data_generation.py # Test data generation
python3 model_training.py # Test model training
python3 visualization.py # Test visualization functions
# Option 2: Run interactive experiment analysis (Recommended)
jupyter notebook notebooks/experiment_analysis.ipynb
# Option 3: Run complete experiment (when main.py is created)
python3 main.pyoverfitting_ml/
โโโ README.md # Project documentation (this file)
โโโ .gitignore # Git ignore rules for Python ML projects
โโโ requirements.txt # Python dependencies
โโโ .venv/ # Virtual environment (excluded from git)
โโโ data_generation.py # โ
Synthetic data generation functions
โโโ model_training.py # โ
Polynomial regression training & evaluation
โโโ visualization.py # โ
Plotting functions for analysis
โโโ notebooks/
โ โโโ experiment_analysis.ipynb # โ
Interactive Jupyter notebook for experiments
โโโ results/ (to be created)
โโโ plots/ # ๐ Generated regression and error curve plots
โโโ metrics/ # ๐ Numerical results and MSE data
โโโ reports/ # ๐ Analysis summaries and findings
- โ Core modules: All implemented and tested
- โ Dependencies: Installed and configured
- โ Notebook: Ready for interactive experimentation
- ๐ Results folder: Will be created during first experiment run
- Practical Understanding of underfitting vs overfitting
- Hands-on Experience with model complexity tuning
- Visualization Skills for model evaluation
- Theoretical Connection between bias-variance tradeoff and real results
- Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning
- Bishop, C. M. (2006). Pattern Recognition and Machine Learning
- James, G., Witten, D., Hastie, T., & Tibshirani, R. (2013). An Introduction to Statistical Learning
Author: Patrick Filima
Date: February 2026
Course: Machine Learning Fundamentals# underfitting-overfitting-ml-experiment