Topic: Classification vs Regression in Machine Learning
To understand how machine learning models make predictions, it’s important to know the
difference between Classification and Regression. Both are supervised learning techniques,
but they solve different types of problems depending on the nature of the target variable.
Classification predicts categories or labels like spam/not spam, disease/no disease,
etc.
Regression predicts continuous values like price, temperature, sales, etc.
Classification uses a decision boundary to separate data into classes, while regression
fits a line through continuous data points to predict numerical values.
A) Understanding Regression
Regression analysis determines the relationship between independent variables and a
continuous target variable. It identifies trends and patterns in data to make future
predictions or estimate unknown values based on learned relationships.
How Regression Works
Regression tries to learn a function
f(x)→y
f(x)→y, where input features x are mapped to a continuous output y. To achieve this,
the model minimizes the error between the predicted value y^and the actual value y
using a loss function.
1) Loss Functions
Mean Squared Error (MSE): Measures the average of squared errors,
penalizing larger mistakes more heavily.
Mean Absolute Error (MAE): Computes the average absolute difference
between actual and predicted values.
Huber Loss: Acts like MSE for small errors and like MAE for large errors,
making it robust to outliers.
Types of Regression
Linear Regression: Fits a straight line to model the relationship between input
features and a continuous output.
Multiple Linear Regression: Extends linear regression by using multiple input
variables for more accurate predictions.
Polynomial Regression: Models non-linear relationships by introducing polynomial
terms of the input features.
Regularized Regression: Used to reduce overfitting by adding penalty terms
Decision Tree Regression: Predicts continuous values by splitting data based on
feature conditions.
Random Forest Regression: Uses an ensemble of multiple decision trees to improve
accuracy and reduce variance.
Support Vector Regression (SVR): Uses margin optimization and kernel functions
to predict continuous values.
Neural Network Regression: Learns complex patterns using multiple interconnected
layers of neurons.
Applications
Regression is widely used across various real world domains to predict continuous outcomes.
Business Forecasting: Estimates sales, revenue and customer demand.
Healthcare Predictions: Helps in predicting disease progression and patient risk
scores.
Agriculture: Used for crop yield estimation and soil/nutrient analysis.
Market Trend Analysis: Identifies price movements and financial trends.
Energy Demand Prediction: Forecasts electricity consumption and load requirements.
B)Understanding Classification
Classification is used when you want to categorize data into different classes or
groups. Classification is used when the output is a label instead of a continuous value.
How it Works
Classification models learn decision boundaries that separate one class from another.
Using activation functions such as
Sigmoid: Used in binary classification to map outputs between 0 and 1 as a
probability.
Softmax: Used in multi-class classification to convert scores into probabilities
that sum to 1.
The class with the highest probability is selected as the final prediction.
Loss Functions
Binary Cross-Entropy (Log Loss): Used for binary classification; measures
the error between predicted probability and actual class (0 or 1).
Categorical Cross-Entropy: Used for multi-class classification; compares
predicted probability distribution with the true one-hot encoded class.
Sparse Categorical Cross-Entropy: Similar to categorical cross-entropy but
works directly with integer class labels instead of one-hot vectors.
Hinge Loss: Common in Support Vector Machines (SVM) penalizes
predictions that fall on the wrong side of the margin.
Types of Classification Models
Binary Classification: Handles two classes.
Multi-Class Classification: Used when there are more than two classes
Classification Algorithms
Logistic Regression: Linear model used for binary and multi-class
classification.
Support Vector Machine: Finds the best hyperplane to separate classes.
k-Nearest Neighbors: Classifies based on the closest neighboring points.
Naive Bayes: Probabilistic classifier based on Bayes’ theorem with
independence assumptions.
Decision Tree: Splits data using decision rules to classify samples.
Random Forest: Ensemble of multiple decision trees for better accuracy and
stability.
Applications
Email Filtering: Classifies emails as spam or not spam.
Medical Diagnosis: Identifies diseases such as tumor vs no tumor.
Image Recognition: Detects objects like cats, dogs, vehicles, etc.
Sentiment Analysis: Classifies reviews as positive, negative or neutral.
Fraud Detection: Flags suspicious transactions in banking systems.
Customer Segmentation: Groups users based on behavior for targeted
marketing.
Difference between Regression and Classification
in Machine Learning
Aspect Regression Classification
Predicts a continuous numerical Predicts a discrete class or
Definition
value category
Output Type Continuous (real-valued) Categorical / Class labels
Yes/No, Spam/Not Spam, Class
Example Outputs 25.6, 102000, 3.8
A/B/C
Typical Use Case Predicting quantities Predicting categories
Example House price prediction, temperature Email spam detection, disease
Problems forecasting diagnosis
Evaluation Accuracy, Precision, Recall, F1-
MAE, MSE, RMSE, R² score
Metrics score
Common Logistic Regression, SVM,
Linear Regression, Ridge, Lasso
Algorithms Decision Tree
Decision
Not applicable Yes, separates classes
Boundary
……………………………………………………………………………….
Loss Functions in regression
1.1) Mean Squared Error (MSE): Measures the average of
squared errors, penalizing larger mistakes more heavily.
Mean Squared Error (MSE) is a fundamental concept in statistics and machine
learning, playing a crucial role in assessing the accuracy of predictive models.
The MSE value provides a way to analyze the accuracy of the model.
It measures the average squared difference between predicted values and the
actual values in the dataset.
It is calculated by taking the average of the squared residuals, where the
residual is the difference between the predicted value and the actual value for
each data point.
Mean Squared Error Formula
The formula for the mean squared error is:
Where:
• n is the number of observations in the dataset.
• yi is the actual value of the observation.
• Y^iY^i is the predicted value of the ith observation.
Interpretation of Mean Squared Error
The Interpreting MSE involves understanding the magnitude of the error and its
implications for the model's performance.
A lower MSE indicates that the model's predictions are closer to the actual
values, signifying better accuracy.
Conversely, a higher MSE suggests that the model's predictions deviate further
from the true value, indicating poorer performance.
Significance of Mean Squared Error
The Mean Squared Error is widely used in various fields, including statistics, machine
learning, and econometrics, due to its several important properties:
It provides the quantitative measure of the accuracy of the predictive models.
It penalizes large errors more heavily than small errors, making it sensitive to
the outliers.
It is mathematically convenient and easy to interpret, making it a preferred
choice for evaluating model performance.
Applications of Mean Squared Error
The Mean Squared Error is extensively used in various applications, including:
Regression analysis: Assessing the goodness of fit of the regression models.
Model evaluation: Comparing the performance of the different machine
learning algorithms.
Optimization: Minimizing MSE during the model training to improve
predictive accuracy.
Predictive modeling: Evaluating the accuracy of the regression and forecasting
models.
Image processing: Assessing the quality of the image reconstruction and
restoration algorithms.
Financial modeling: Analyzing the performance of the investment strategies
and risk models.
How to Minimize Mean Squared Error in Model
Training
To minimize Mean Squared Error during the model training, several strategies can be
employed, including:
Feature selection: Choosing relevant features that contribute most to reducing
prediction errors.
Model selection: Experimenting with the different algorithms and model
architectures to identify the best-performing model.
Hyperparameter tuning: The Optimizing model hyperparameters such as the
learning rate, regularization strength, and network depth to improve predictive
accuracy.
Example problems on Mean Squared Error
Example: Suppose we have a dataset consisting of the actual and predicted
values for the regression problem
Actual Values: [10, 20, 30, 40, 50]
Predicted Values: [12, 18, 32, 38, 48]
Solution:
To calculate MSE we first compute the squared differences between the each actual
and predicted value:
Squared Differences: [(10-12)2, (20-18)2, (30-32)2, (40-38)2, (50-48)2]
= [4, 4, 4, 4, 4]
Next, we take the average of these squared differences to the obtain the MSE:
MSE = (4 + 4 + 4 + 4 + 4) / 5
= 20 / 5
=4
Therefore, the MSE for this regression model is 4.
Root Mean Square Error
The Root Mean Squared Error (RMSE) is a variant of MSE that calculates the square
root of the average squared difference between actual and predicted values. It is often
preferred over MSE as it provides an interpretable measure of the error in the same
units as the original data. RMSE Formula is:
RMSE=√(MSE)
Example of Root Mean Square Error
Example: Given the actual and predicted values for the regression problem, calculate
the MSE and RMSE.
Actual Values: [15, 25, 35, 45, 55]
Predicted Values: [18, 22, 38, 42, 52]
Solution:
The Calculate the squared differences between the actual and predicted values:
Squared Differences: [(15-18)2, (25-22)2, (35-38)2, (45-42)2, (55-52)2]
= [9, 9, 9, 9, 9]
Compute the MSE
MSE = (9 + 9 + 9 + 9 + 9) / 5
= 45 / 5
=9
Calculate the RMSE:
RMSE = √(9)= 3
MSE vs RMSE vs MAE
Mean Squared Error is often compared with other error metrics, such as the Mean
Absolute Error (MAE) and Root Mean Squared Error (RMSE), to evaluate model
performance.
While MAE measures the average absolute difference between predicted and
actual values
RMSE measures the square root of the average squared difference. T
he MSE and RMSE penalize large errors more heavily than MAE, making
them more sensitive to the outliers.