Introduction to Regression
Introduction to Regression
Understanding how Machine Learning predicts continuous values like prices, scores, and trends.
What is Regression?
Regression is a type of supervised Machine Learning technique used to predict a continuous numerical value based on input features.
Simple Example
Imagine you want to predict a person's salary based on years of experience:
| Experience (Years) | Salary (₹) |
|---|---|
| 1 | 25,000 |
| 3 | 40,000 |
| 5 | 60,000 |
| 7 | 80,000 |
| 10 | 1,00,000 |
A Regression model will learn this relationship and predict salary for new experience values, like 6 years.
Why is Regression Used?
Regression is used when the output is a continuous value, such as:
- Predicting house prices
- Forecasting stock prices
- Predicting temperature
- Estimating sales
- Predicting exam scores
- Estimating fuel consumption
Regression vs Classification
| Aspect | Regression | Classification |
|---|---|---|
| Output Type | Continuous (numbers) | Discrete (categories) |
| Example | Predict salary, price | Predict Yes/No, Spam/Not Spam |
| Algorithm Examples | Linear Regression, Random Forest Regressor | Logistic Regression, Decision Tree Classifier |
| Evaluation Metric | MSE, RMSE, MAE, R² | Accuracy, Precision, Recall, F1 |
Support Vector Regression (SVR)
Uses SVM technique for predicting continuous values.
How Regression Works (Workflow)
Step-by-Step Process
- Collect and clean the dataset.
- Identify input (X) and output (Y) variables.
- Split the data into train/test sets.
- Choose a regression algorithm.
- Train the model on training data.
- Predict values on test data.
- Evaluate using metrics (MSE, RMSE, R²).
Python Example — Simple Linear Regression
pip install pandas scikit-learn matplotlib
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Sample dataset
data = {
"Experience": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Salary": [25000, 30000, 40000, 45000, 55000, 65000, 70000, 80000, 90000, 100000]
}
df = pd.DataFrame(data)
# Split data
X = df[["Experience"]]
y = df["Salary"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict
y_pred = model.predict(X_test)
print("Predicted Salaries:", y_pred)
# Visualize
plt.scatter(X, y, color="blue")
plt.plot(X, model.predict(X), color="red")
plt.xlabel("Experience")
plt.ylabel("Salary")
plt.title("Linear Regression Example")
plt.show()
Common Regression Evaluation Metrics
| Metric | Formula | Meaning |
|---|---|---|
| MAE (Mean Absolute Error) | $$ \frac{1}{n} \sum |y - \hat{y}| $$ | Average absolute error |
| MSE (Mean Squared Error) | $$ \frac{1}{n} \sum (y - \hat{y})^2 $$ | Penalizes larger errors more |
| RMSE (Root Mean Squared Error) | $$ \sqrt{MSE} $$ | Same unit as target value |
| R² Score | $$ 1 - \frac{SS_{res}}{SS_{tot}} $$ | How well model explains variance |
Real-Life Analogy
Regression = Predicting Tomorrow's Temperature
Just like a weather model predicts tomorrow's temperature using today's data, regression predicts a continuous value (Y) based on input features (X).
Real-World Applications
Real Estate
- Predict property prices
- Estimate rent values
Finance
- Forecast stock prices
- Risk and credit scoring
Business
- Sales forecasting
- Customer lifetime value
Healthcare
- Predict disease progression
- Estimate treatment outcomes
Weather
- Forecast temperature
- Predict rainfall amount
Automobile
- Predict fuel efficiency
- Maintenance cost estimation
Advantages of Regression
- Easy to understand and implement.
- Provides clear relationships between variables.
- Works well for continuous predictions.
- Many algorithms available for different complexities.
Disadvantages
Common Mistakes to Avoid
Best Practices
Quick Tips
- Always check the distribution of target variable.
- Handle outliers and missing data first.
- Normalize/standardize features if needed.
- Split data into train/test sets.
- Evaluate using multiple metrics (R², RMSE).
- Use cross-validation for stable performance.
Importance of Regression in ML
Predictive Power
- Forecasts future trends
- Strong business impact
Easy Interpretation
- Shows relationships clearly
- Useful for decision-making
Foundation of ML
- Base of many advanced models
- Used in neural networks too
Practical Use
- Used in nearly every industry
- Highly versatile technique
Golden Rule
Key Takeaway
Regression is a fundamental Machine Learning technique used to predict continuous values. It forms the foundation of many real-world applications — from predicting prices to forecasting trends. Understanding regression is the first step toward mastering predictive modeling.
Basic Concept of Regression
Regression tries to find a mathematical function that best fits the data:
Where:
- Y = predicted output (target)
- X = input features
- f(X) = function (model) that maps X to Y
- ε = error term
For example, in Linear Regression:
Key Components of Regression
Dependent Variable (Y)
The variable we want to predict. Also called the target or output.
Example: Salary, Price, Temperature.
Independent Variables (X)
The input features used for prediction. Also called predictors.
Example: Years of experience, area of house, study hours.
Coefficients (Weights)
Values learned by the model that show the importance of each feature.
Error (Residual)
The difference between the predicted value and the actual value.
Formula: Error = Actual − Predicted
Types of Regression
Linear Regression
Models the relationship between X and Y using a straight line.
Example: Predict salary from experience.
Multiple Linear Regression
Uses multiple input features to predict a single target.
Example: Predict house price using area, location, rooms.
Polynomial Regression
Fits a non-linear curve to the data using polynomial features.
Example: Predict growth rate that follows a curve.
Ridge & Lasso Regression
Regularized regression that prevents overfitting by penalizing large coefficients.
Logistic Regression
Despite its name, it's used for classification (Yes/No), not regression.