date: 2026-03-06
Polynomial Regression
Polynomial Regression is a machine learning technique used when the relationship between the independent variable and dependent variable is non-linear.
Instead of fitting a straight line like Linear Regression, Polynomial Regression fits a curved line to better capture complex patterns in the data.
📌 Concept
Linear Regression equation:
y = b0 + b1x
Polynomial Regression equation:
y = b0 + b1x + b2x² + b3x³ + ... + bnxⁿ
Here the model learns polynomial relationships between the variables.
Although it is called Polynomial Regression, it is actually Linear Regression applied on transformed polynomial features.
📊 Example
For a dataset like:
| X | y |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
The relationship follows:
y = x²
Polynomial Regression with degree = 2 can perfectly model this pattern.
⚙️ Steps to Implement
- Import required libraries
- Create dataset
- Convert features into polynomial features
- Train Linear Regression model
- Make predictions
- Evaluate model using R² score
- Visualize results
🧠 Libraries Used
- numpy
- pandas
- matplotlib
- scikit-learn
Install dependencies:
pip install numpy pandas matplotlib scikit-learn
💻 Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
# Dataset
X = np.array([1,2,3,4,5,6,7,8,9,10]).reshape(-1,1)
y = np.array([1,4,9,16,25,36,49,64,81,100])
# Create polynomial features
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)
# Train model
model = LinearRegression()
model.fit(X_poly, y)
# Prediction
y_pred = model.predict(X_poly)
# R2 Score
print("R2 Score:", r2_score(y, y_pred))
📈 Visualization
plt.scatter(X, y, color='blue')
plt.plot(X, y_pred, color='red')
plt.title("Polynomial Regression")
plt.xlabel("X")
plt.ylabel("y")
plt.show()
Blue points represent actual data
Red line represents the polynomial regression curve
📊 Model Evaluation
R² Score measures how well the model explains the variance in the data.
R² = 1 → Perfect prediction
R² ≈ 0 → Model performs poorly
In this example, the score is 1.0 because the dataset perfectly follows a quadratic relationship.
⚠️ Overfitting
Choosing a very high polynomial degree can cause overfitting.
Example:
| Degree | Result |
|---|---|
| 1 | Underfitting |
| 2 | Good Fit |
| 10 | Overfitting |
Always choose an appropriate polynomial degree.
🚀 Applications
Polynomial Regression is used in:
- Population growth prediction
- Stock trend analysis
- Temperature forecasting
- Sales trend prediction
- Scientific curve fitting
📚 Machine Learning Series
This project is part of my Machine Learning learning series where I practice one algorithm daily and share the implementation on GitHub.
🔗 Author
Suraj Singh
Learning Machine Learning and building projects in public.