MACHINE LEARNING
Dr. P V Siva Teja
Assistant Professor
Dr. P V Siva Teja
UNIT-IV: Linear Discriminants for Machine Learning�(Normal Discriminant Analysis (or) Discriminant Function Analysis)
Introduction to Linear Discriminants, Linear Discriminants for Classification, Perceptron Classifier, Perceptron Learning Algorithm, Support Vector Machines, Linearly Non-Separable Case, Non-linear SVM, Kernel Trick, Logistic Regression, Linear Regression, Multi-Layer Perceptron’s (MLPs), Backpropagation for Training an MLP.
Dr. P V Siva Teja
Features | Principal Component Analysis(PCA) | Linear Discriminant Analysis (LDA) |
Discrimination b/w Classes | PCA deals with the data in its entirety for the principal components analysis without paying any particular attention to the underlying class structure. | LDA deals directly with discrimination b/w classes. |
Supervised Learning Technique | PCA is an unsupervised technique. | LDA is a supervised learning technique that relies on class labels. |
Focus | PCA searches for the directions that have largest variations. | LDA maximizes the ratio of b/w-class variation and with-in class variation. |
Small Datasets | PCA is less superior to LDA. | LDA is superior to PCA. |
Large Datasets | PCA requires fewer computations. | LDA requires significantly more computation than PCA for large datasets. |
Applications | Application of PCA in the prominent field of criminal investigation is beneficial. | Linear Discriminant analysis for data classification is applied to classification problem in speech recognition. |
Dr. P V Siva Teja
Discriminant
The name "discriminant" comes from its ability to "discriminate" or distinguish b/w the different types of solutions an equation can have, specially whether they are:
In math
The discriminant is a value calculated from the coefficients of a polynomial equation that reveals the nature of its roots (solutions) without needing to solve the entire equation.
For a quadratic equation:
ax² + bx + c = 0
Discriminant = b² − 4ac (or) using standard notation: D = b² − 4ac
Dr. P V Siva Teja
The value of the discriminant (D = b² − 4ac) determines the characteristics of the roots of a quadratic equation without the need to solve the entire equation. There are three distinct types of solutions based on this value:
Dr. P V Siva Teja
Example: Finding the Nature of Roots
Equation:�x² − 5x + 6 = 0
Step 1: Identify the coefficients In the standard form ax² + bx + c = 0:�a = 1 b = −5 c = 6
Step 2: Apply the Discriminant formula�The formula for the discriminant (D) is: D = b² − 4ac
Step 3: Calculate the value
D = (−5)² − 4(1)(6) = 25 − 24 = 1
Step 4: Determine the Nature of Roots�Since D = 1 (which is greater than 0), the value of the discriminant is positive (D > 0).�Therefore, the equation has real and distinct roots.
Introduction to Linear Discriminant Analysis (LDA)
Linear Discriminant Analysis Works
1. Supervised Approach
Unlike unsupervised methods like PCA (Principal Component Analysis), LDA is a supervised technique, meaning it uses class labels during the training process to guide its computations.
2. Maximizes Class Separability
LDA aims to find axes (linear discriminants) that maximize the ratio of between-class variance (difference between class means) to within-class variance (spread within each class).
3. Data Projection
The data is then projected onto these newly found axes, transforming the high-dimensional data into a lower-dimensional space where classes are as distinct as possible.
Assumptions
LDA assumes that the data follows a multivariate Gaussian distribution for each class and that all classes share the same covariance matrix.
Dr. P V Siva Teja
Linear Discriminant for Classification
Dr. P V Siva Teja
Dr. P V Siva Teja
LDA Algorithm Overview
LDA aims to find a projection (linear combination of features) that maximizes the separation between classes while minimizing the variance within each class. This makes classes more distinguishable in a lower-dimensional space.
Step-by-Step Algorithm
1. Data Preparation
2. Compute Class Statistics
3. Compute Scatter Matrices
Dr. P V Siva Teja
Dr. P V Siva Teja
Dr. P V Siva Teja
4. Compute Eigenvectors and Eigenvalues
5. Select Discriminant Directions
6. Transform Data
7. Classification
Dr. P V Siva Teja
Dr. P V Siva Teja
Dr. P V Siva Teja
Advantages
Dr. P V Siva Teja
Disadvantages
Applications
Medical diagnosis | Bioinformatics | Image recognition |
Finance & banking | Marketing and customer segmentation | Quality Control |
| | Speech recognition |
| | Environmental Monitoring |
Dr. P V Siva Teja
Perceptron
Perceptron is a simplest possible "brain cell" (neuron) for a computer. It is the fundamental building block of an Artificial Neural Network.
Its job is simple: It takes several inputs, processes them, and decides whether to "fire" (output a 1) or stay silent (output a 0).
Artificial Neural Network (ANN):-
Dr. P V Siva Teja
Comparison of Neural Networks
Biological Neural Network (BNN) | Artificial Neural Network (ANN) |
Dendrites | Inputs |
Cell nucleus | Nodes |
Synapse | Weights |
Axon | Output |
Dr. P V Siva Teja
Components of ANN
A simple neural network consists of three components. They are:
1. Input layer
Input nodes receive inputs / information from the outside world.
2. Hidden layer
Hidden layer is the set of neurons where all the computations are performed on the input data.
There can be any number of hidden layers in a neural network. The simplest network consists of a single hidden layer.
3. Output layer
The output layer is the output / conclusions derived from all the computations performed.
There can be single or multiple nodes in the output layer:
Dr. P V Siva Teja
Perceptron
It is a simple form of Neural Network and consists of a single layer where all the mathematical calculations/computations are performed.
Multilayer Perceptron (MLP)
It is also known as an ANN (Artificial Neural Network). It consists of more than one perceptron which is grouped together to form a multiple layer neural network.
Layer Type | Number of Neurons |
Input Layer | 3 neurons |
Hidden Layer 1 | 4 neurons |
Hidden Layer 2 | 2 neurons |
Hidden Layer 3 | 1 neuron |
Output Layer | 2 neurons |
Dr. P V Siva Teja
Perceptron Learning Algorithm
Dr. P V Siva Teja
Perceptron Model in ML
Perceptron Model is a type of artificial neuron that functions as a linear binary classifier. Its purpose is to classify data points into one of two categories by learning a decision boundary from labeled training data.
This process makes the perceptron particularly useful in tasks such as binary classification in datasets like spam detection or sentiment analysis.
Dr. P V Siva Teja
Basic Components of a Perceptron
1. Input Layer
The input layer consists of feature values or data points that the perceptron will classify. Each input is assigned a corresponding weight.
2. Weights
Weights determine the importance of each input in the classification process. The model adjusts these weights during training to improve accuracy.
3. Bias
The bias term helps the perceptron shift the decision boundary, improving flexibility in data classification.
4. Activation Function
The activation function, such as a step function or sigmoid function, determines the output based on the weighted sum of the inputs.
5. Output
The output is the final result of the perceptron's decision, typically a binary value (0 or 1) in the case of binary classification.
Dr. P V Siva Teja
Perceptron Algorithm
The perceptron model operates in a step-by-step process that involves computing the weighted sum of inputs, applying an activation function, and classifying the output. This process allows the model to differentiate between two classes based on the input data.
Step-by-Step Process
Step 1: Initialize Weights and Bias
The model begins by assuming random weights for the inputs and setting a bias value.
Step 2: Compute Weighted Sum
For each input, the perceptron computes the weighted sum of the input values and the bias term.
It is represented mathematically as:
Z = w₁x₁ + w₂x₂ + … + wₙxₙ + b
Where:�w = weights�x = inputs�b = bias
Dr. P V Siva Teja
Step 3: Apply Activation Function
The weighted sum is then passed through an activation function. If the result meets a certain threshold, the output is classified as 1. Otherwise, it is classified as 0. The step function is a common activation function used for this purpose:
f(Z) = 1, if Z ≥ 0�f(Z) = 0, if Z < 0
Step 4: Update Weights
During training, the model compares the predicted output to the actual output and adjusts the weights using a learning rule (typically gradient descent) to reduce the error.
Step 5: Repeat
This process is repeated for multiple iterations until the model's predictions align with the actual outputs.
Dr. P V Siva Teja
Advantages
Limitations: The perceptron can only solve problems with linear decision boundaries, making it unsuitable for more complex tasks.
Applications
Dr. P V Siva Teja
Support Vector Machines (SVM)
SVM is a Machine Learning (ML) algorithm used for classification and regression.
Goal or Aim
The goal of the SVM algorithm is to find the best line (or hyperplane) to separate data into groups, maximizing the distance between the closest points (Support Vectors) of each group.
Key Features
Dr. P V Siva Teja
SVM Diagram
Dr. P V Siva Teja
Types of SVM
SVM can be classified into two types:
1. Linear SVM:-
It is used for linearly separated data, which means if a dataset can be classified into two classes by using a single straight line, then such data is termed as linearly separable data, and classifier is used called as linear SVM classifier.
2. Non-linear SVM:-
It is used for non-linearly separated data, which means if a dataset cannot be classified by using a straight line, then such data is termed as non-linear data and classifier used is called as non-linear SVM classifier.
Dr. P V Siva Teja
Training datasets which have a significant grouping margin will function well with SVM.
The important terms used in SVM are:
Dr. P V Siva Teja
Hyperplane
A hyperplane is a decision plane or space that is divided into a set of objects having different classes.
2-Dimensional Space: Mathematically, in a 2D space, a hyperplane can be defined by the equation of a straight line:
C0+C1x1+C2x2=0
N-Dimensional Space: Extending this concept to an N-dimensional space, the hyperplane can be defined by the equation:
C0+C1x1+C2x2+⋯+CNxN=0
Vector Representation: This can also be represented as:
C⋅X+C0=0
Dr. P V Siva Teja
Margin
The margin is defined as the gap between two lines on the closest data points of different classes.
Dr. P V Siva Teja
Support Vector Classification Algorithm
The primary goal of this algorithm is to find the "best" line (or hyperplane) that separates two classes of data points with the largest possible gap.
Step 1: Select Initial Hyperplanes
Dr. P V Siva Teja
Step 2: Maximize the Margin
Dr. P V Siva Teja
Step 3: Define the Decision Boundary
Dr. P V Siva Teja
Advantages
Disadvantages
Applications
Dr. P V Siva Teja
Linearly Non-Separable Case
Scenario 1 : Data is linearly separable.
Dr. P V Siva Teja
Finding the Optimal Hyperplane
Dr. P V Siva Teja
Summary of the Optimal Solution
Dr. P V Siva Teja
Scenario-2: Data is Non-linearly Separable (or) linearly Non-separable case
Mapping — From 1D to 2D
Dr. P V Siva Teja
Non-linear SVM
Dr. P V Siva Teja
Comparison: Linear SVM vs. Non-linear SVM
Feature | Linear SVM | Non-linear SVM |
Decision Boundary | Straight line or hyperplane | Curved or complex boundaries |
Data Separation | Works well when data is linearly separable | Suitable for non-linearly separable data |
Kernel Usage | No kernel or uses a linear kernel | Uses non-linear kernels (e.g., RBF, polynomial) |
Computational Cost | Generally faster and less complex | More computationally intensive |
Example Usecase | Spam detection with simple features | Image classification or handwriting recognition |
Dr. P V Siva Teja
Kernel trick in SVM
Kernel trick is a function, used to solve the non-linear Support Vector Machines problems.
Dr. P V Siva Teja
Mapping Function: 2D to 3D
�One mapping function φ: ℝ² → ℝ³ used to transform 2D data to 3D data is given as:
φ(x, y) = (x², √2xy, y²)
Example Case:�Consider a point (2, 3) in 2D space. If you apply the above mapping function, we can convert it into 3D space.:
φ(2, 3) = (2², √12, 3²) = (4, 3.46, 9)
Parameters:�x = 2�y = 3
Dr. P V Siva Teja
Mapping function plays an important role, there are many disadvantages:
Kernels are used to overcome the above two disadvantages.
Kernels are used to compute the value without transforming the data.
Dr. P V Siva Teja
Kernel
Dr. P V Siva Teja
Types of Kernels:
Linear Kernel Polynomial Kernel
Gaussian RBF (Radial Basis Function) Sigmoid
Dr. P V Siva Teja
Dr. P V Siva Teja
S.No | Feature/Aspect | Linear Regression | Logistic Regression |
1. | Primary Use | Used to predict the continuous dependent variable using a given set of independent variables. | Used to predict the categorical dependent variables using a given set of independent variables. |
2. | Problem Type | Used for solving regression problems. | Used for solving classification problems. |
3. | Prediction Goal | We predict the value of a continuous variable. | We predict the value of a categorical variable. |
4. | Curve Type | We find the best-fit line. | We find the S-curve (Sigmoid curve). |
5. | Estimation Method | Least square estimation method is used for estimation of accuracy. | Maximum likelihood estimation method is used for estimation of accuracy. |
6. | Output Type | Output must be a continuous value, such as price, age, etc. | Output must be a categorical value, such as 0 or 1, yes or no, etc. |
7. | Relationship | Requires a linear relationship between dependent and independent variables. | Does not require a linear relationship. |
8. | Collinearity | There may be collinearity between the independent variables. | There should not be collinearity between independent variables. |
Dr. P V Siva Teja
Logistic Regression
It is a supervised ML algorithm for binary classification, predicting the probability of an outcome (e.g., spam or not spam) by applying a sigmoid function to a linear combination of input features.
Dr. P V Siva Teja
Input : Output of the linear regression function.
Output : Uses a sigmoid function to estimate the probability for the given class.
It is used to describe data and to explain the relationship b/w one dependent binary variable and one or more nominal, ordinal, interval or ratio-level independent variables.
Dr. P V Siva Teja
Logistic Regression Works:
Step 1: Prepare the data
The data should be in a format where each row represents a single observation and each column represents a different variable. The target variable should be binary (yes/no, true/false).
Step 2: Train the Model
We teach the model by showing it the training data. This involves finding the value of the model parameters that minimize the error in the training data.
Step 3: Evaluate the Model
The model is evaluated on the held-out test data to assess its performance on unseen data.
Step 4: Use the model to make predictions
After the model has been trained and assessed, it can be used to forecast outcomes on new data.
Dr. P V Siva Teja
Types of Logistic Regression
On the basis of categories, Logistic Regression can be classified into three types:
1. Binomial Logistic Regression
This type is used when the dependent variable has only two possible outcomes.
2. Multinomial Logistic Regression
This type is used when the dependent variable has 3 or more possible unordered outcomes.
3. Ordinal Logistic Regression
This type is used when the dependent variable has 3 or more possible ordered outcomes (where the sequence matters).
Dr. P V Siva Teja
Advantages
Disadvantages
Applications
Logistic Regression is widely used in various fields for classification tasks:
Spam Detection, Medical Diagnosis, Fraud Detection, Customer Churn Prediction, Loan Default Prediction, Product Purchase Prediction, Risk Assessment
Dr. P V Siva Teja
Assumptions for Logistic Regression
1. Linearity
A straight line (best fit line) should exist between the independent variables and the log odds of the dependent variable. Unlike linear regression, it does not require a linear relationship between the independent and dependent variables themselves.
2. Independence
The observations (outputs) should stand alone from one another.
3. No Multicollinearity
There shouldn't be a high correlation between the independent variables.
Dr. P V Siva Teja
4. Large Sample Size
A substantial number of samples is needed to obtain accurate estimations of the coefficients. Small datasets can lead to unreliable results in logistic regression.
5. No Outliers
Dr. P V Siva Teja
Linear Regression Overview
Practical Example: Salary.csv
Dr. P V Siva Teja
Dr. P V Siva Teja
Types of Linear Regression
1. Simple Linear Regression
It is used when we want to predict a target value (Dependent Variable) using only one input feature (Independent Variable). It assumes a straight-line relationship between the two.
Formula:
y = β₀ + β₁x
Where:�y : Predicted value�x : Independent variable�β₀ : Intercept (value of y when x = 0)�β₁ : Slope or coefficient
Dr. P V Siva Teja
2. Multiple Linear Regression
It involves more than one independent variable and one dependent variable. The equation for multiple linear regression is:
Formula:
y = β₀ + β₁x₁ + β₂x₂ + … + βₙxₙ
Where:�y : Predicted / Dependent value�x₁, x₂, …, xₙ : Independent variables�β₀ : Intercept�β₁, β₂, …, βₙ : Coefficients corresponding to the predictors
Dr. P V Siva Teja
Best Fit Line in Linear Regression
Key Characteristics:
Dr. P V Siva Teja
The best fit line designing following:
1. Goal of the Best-Fit line:-
The goal of linear regression is to find a st. line (straight line) that minimizes the error between the observed data points and the predicted value.
This line helps us predict the dependent variable for new unseen data (test data).
�
Dr. P V Siva Teja
2. Equation of the Best-Fit Line
The line is represented by the following equivalent formulas:
y = mx + c
or
y = β₀ + β₁x
y — Predicted value (Dependent value / Outcome)
x — Input value (Independent value / Predictor)
m (or β₁) — Slope of the line (Gradient / Rate of change)
c (or β₀) — Intercept (y=value when x = 0)
Dr. P V Siva Teja
3. Minimizing the Error
To find the best-fit line, a method called Least Squares (MSE) is used.
How the Method Works
Dr. P V Siva Teja
4. Interpretation of the Best-Fit Line
Understanding the components of the line allows you to describe the relationship between variables.
The Slope (m)
m = dy/dx
dy : Change in the dependent variable�dx : Change in the independent variable
The Intercept (c)
Definition:�The intercept represents the predicted value of y when x = 0.
Dr. P V Siva Teja
Advantages:
Dr. P V Siva Teja
Disadvantages:
Dr. P V Siva Teja
Applications:
Dr. P V Siva Teja
Multi-Layer Perceptron’s (MLPs)
In particular, the MLP can solve the XOR problem (or) trivial problems.
Dr. P V Siva Teja
Layer Type | Configuration |
Input (I/P) | 2 neurons |
Hidden Layers | 2 layers total |
- Hidden Layer 1 (HL 1) | 3 neurons |
- Hidden Layer 2 (HL 2) | 2 neurons |
Output (O/P) | 3 neurons |
Dr. P V Siva Teja
The layers close to the input layer are usually called the lower layers, and the ones close to the outputs are usually called the upper layers.
Key Characteristics of Artificial Neural Networks (ANN)
Dr. P V Siva Teja
Computations Performed on Hidden Layers
The process is divided into two primary steps:
Step 1: Linear Transformation
All inputs are multiplied by their respective weights, and a bias variable is added to the sum. The formula for this linear combination is:
z₁ = x₁w₁ + x₂w₂ + … + xₙwₙ + b
Dr. P V Siva Teja
Step 2: Activation Function
The result of the linear equation (z1) is passed through an activation function.
In a Multi-Layer Perceptron (MLP), the system evaluates its performance by calculating the error:
error = predicted output − actual output
Dr. P V Siva Teja
Backpropagation for Training an MLP
Dr. P V Siva Teja
Backpropagation Algorithm Overview
There are overall four main steps in the backpropagation algorithm:
1. Forward Pass
This is the first step of the backpropagation process, and it's illustrated below:
Dr. P V Siva Teja
Two inputs → x₁, x₂
Two hidden layers → N₁X and N₂X, where X = 1, 2, 3.
Dr. P V Siva Teja
2. Error Calculation (predictions)
Formula: error value = actual output − predicted output
Dr. P V Siva Teja
3. Backward Pass
This is an actual backpropagation step, and cannot be performed without the above forward and error calculation steps. Here is how it works:
Dr. P V Siva Teja
Dr. P V Siva Teja
4. Weights Update
Dr. P V Siva Teja
Advantages
Disadvantages