MACHINE LEARNING
Dr. P V Siva Teja
Assistant Professor
Dr. P V Siva Teja
UNIT-2: Nearest Neighbor-Based Models
Introduction to Proximity Measures, Distance Measures, Non-Metric Similarity Functions, Proximity Between Binary Patterns, Different Classification Algorithms Based on the Distance Measures, K-Nearest Neighbor Classifier, Radius Distance Nearest Neighbor Algorithm, KNN Regression, Performance of Classifiers, Performance of Regression Algorithms.
Dr. P V Siva Teja
Proximity Measures
Dr. P V Siva Teja
Similarity
Definition: A numerical measure of how alike two data objects are.
Characteristics:
It is higher when objects are more alike.
Often falls in the range [0, 1].
Similarity measure for two objects i & j returns:
0 – if they are unlike.
1 – if they are alike.
Key Principles:
The higher the similarity value, the greater the similarity between objects.
1 indicates complete similarity.
Dr. P V Siva Teja
Dissimilarity
Definition: Numerical measures of how different two objects are.
Characteristics:
Lower when objects are more alike.
Minimum dissimilarity is often zero.
Upper limit varies.
Dissimilarity measure for two objects i & j returns:
0 – if they are alike.
1 – if they are unlike.
Key Principles:
The higher the dissimilarity value, the greater the dissimilarity between objects.
1 indicates complete dissimilarity.
General Relation
The general relation between similarity (s) and dissimilarity (d) is:
s = 1 – d (or) d = 1 - s
Dr. P V Siva Teja
Dr. P V Siva Teja
Case 1: Equal Values
Problem: Let p = 10 and q = 10. Find the dissimilarity and similarity.
Calculation:
Result: For p=q, the objects have 0 dissimilarity and 1 similarity.
Case 2: Unequal Values
Problem: Let p = 10 and q = 25. Find the dissimilarity and similarity.
Calculation:
Using the formula s = 1 - d
s = 1 - 1 = 0
Result: For p q, the objects have 1 dissimilarity and 0 similarity.
Dr. P V Siva Teja
2. Ordinal Attributes
The calculation for dissimilarity between two points, p and q, where the number of variables (or states) n = 2.
Case 1:
Given: p = 10, q = 5, and n = 2.
Formula used:
Calculation:
d = {|10 - 5|}/{2 - 1} = {5}/{1} = 5
Note: There is a crossed-out section below this, followed by a second calculation: s = 1 - 5 = -4.
3. Interval or Ratio Attributes
This section explores how to convert dissimilarity values (d) into similarity values (s).
Given Dissimilarities:
The values of d are 0, 1, 10, 100.
Similarity Formulas used:
Calculations for s = {1}/{1 + d}:
Dr. P V Siva Teja
Dissimilarity (d) | Similarity Calculation (s) | Result |
d = 0 | s = {1}/{1 + 0} | s = 1 |
d = 1 | s = {1}/{1 + 1} = {1}/{2} | s = 0.5 |
d = 10 | s = {1}/{1 + 10} = {1}/{11} | s 0.09 |
d = 100 | s = {1}/{1 + 100} ={1}/{101} | s 0.01 |
It demonstrate that as dissimilarity (d) increases, the similarity (s) decreases toward zero, reflecting an inverse relationship between the two metrics.
Dr. P V Siva Teja
Data Matrix
Suppose that we have n objects (ex. person or courses) described by p attributes (ex. age, height, weight & gender).
The objects are:
Dr. P V Siva Teja
Dissimilarity matrix :- (object by object structure)
This structure stores a collection of proximities that are available for all pairs of n objects.
The proximity measures can be classified into two types :-
Dr. P V Siva Teja
Distance Measures :-
Euclidean Distance
Dr. P V Siva Teja
Final Distance Matrix
| B[0] | B[1] |
A[0] | 5.6569 | 8.4853 |
A[1] | 2.8284 | 5.6569 |
A = [1, 2],
[3, 4]
B = [5, 6],
[7, 8]
Dr. P V Siva Teja
3D Euclidean Distance
Dr. P V Siva Teja
Applications
It is useful metric in many algorithms. They are:
Dr. P V Siva Teja
Manhattan Distance
Also known as: Taxicab distance or city block distance.
Definition: Calculates the distance between two points in a grid-like space. It measures the sum of absolute differences of their Cartesian coordinates, effectively representing the distance a taxi would travel in a city with a grid layout like Manhattan.
Key Characteristics
Grid-based: It is used when movement is restricted to horizontal and vertical lines like city streets.
Sum of absolute differences: Instead of a straight line (Euclidean distance), it adds up the distances along each axis.
Non-negative: Because of the absolute value, the distance always results in a positive number.
Dr. P V Siva Teja
Mathematically, the Manhattan distance between two points in an n-dimensional space is the sum of the absolute differences of their Cartesian coordinates.
Mathematical Formulas
Dr. P V Siva Teja
Example: Google Maps
Calculate the distance between two points, A and B.
Point A: (1, 1)
Point B: (4, 5)
Calculation Steps (as written):
d = (1 - 4) + (1 - 5) = -3 + (-4) = -7
Dr. P V Siva Teja
Applications:
Manhattan distance finds applications in various fields: data analysis & geospatial technology. Here some key areas where manhattan distance is particularly useful. They are:
Dr. P V Siva Teja
Minkowski Distance
It is mainly a mathematical measure used to calculate the distance between two points in a multi-dimensional space. It's an extension of the more commonly known Euclidean distance.
Dr. P V Siva Teja
Dr. P V Siva Teja
Example: The distance calculations between point A(2, 3) and point B(5, 7).
Dr. P V Siva Teja
Point A: (2, 5, 8)
Point B: (3, 1, 6)
Dr. P V Siva Teja
Chebyshev Distance
Also known as: "Chessboard distance" or “ metric."
Definition: A distance metric that measures the distance between two points as the maximum absolute difference between their corresponding coordinates.
Usage: It is used to calculate distance in a space where movement can occur in any direction, including diagonally. It is especially useful in grid-based systems like chessboards or pathfinding in games where diagonal movement is allowed.
Characteristics: Simple and fast to compute; works well in environments with uniform movement in all directions.
Dr. P V Siva Teja
Dr. P V Siva Teja
Example Calculation
Given points:
A = (4, 7, 8)
B = (6, 9, 1)
Formula for 3D space:
D = max(|x1 - y1|, |x2 - y2|, |x3 - y3|)
Step-by-step Calculation:
Result:
D = max(2, 2, 7) = 7
Dr. P V Siva Teja
Metric Similarity Functions in Nearest Neighbors Based Models
Proximity Measures Proximity measures are categorized into a hierarchy shown below:
Dr. P V Siva Teja
Characteristics of Non-metric Similarity Functions
The Cosine Similarity Formula
To compute the Cosine similarity between vectors A and B, you can use the following formula:
The Cosine similarity is one of the most common measures of document similarity. If A and B are two document vectors, then:
Dr. P V Siva Teja
Mathematical Calculations
Vectors:
A = (4, 5, 6)
B = (2, 1, 2)
Dr. P V Siva Teja
Applications
�Advantages
Disadvantages
Dr. P V Siva Teja
Jaccard Similarity
Key Characteristics
Dr. P V Siva Teja
Example:
doc1 = "Data is the new oil of the digital economy"
doc2 = "Data is a new oil"
Let's get the set of unique words for each document.
words_doc1 = {'data', 'is', 'the', 'new', 'oil', 'of', 'digital', 'economy'}
words_doc2 = {'data', 'is', 'a', 'new', 'oil'}
Now, we will calculate the intersection & union of these two sets of words & measure the Jaccard similarity between doc1 & doc2.
Dr. P V Siva Teja
Applications:
Dr. P V Siva Teja
Pearson Correlation
Dr. P V Siva Teja
The Pearson coefficient descriptions were indicated below,
Note on Correlation Values
The correlation coefficient (r) indicates the strength and direction of a linear relationship:
Dr. P V Siva Teja
Uses of Correlation
Dr. P V Siva Teja
Proximity Between Binary Patterns
In Machine Learning & pattern recognition, measuring the proximity (similarity or dissimilarity) between binary patterns (vectors 0's & 1's) is essential for tasks like clustering, classification & Informational retrieval.
Common Methods:-
Dr. P V Siva Teja
Hamming Distance (HD):-
It counts the no. of bit positions where two binary vectors differ.
Formula:-
Dr. P V Siva Teja
Simple Matching Coefficient (SMC)
SMC is a commonly used similarity coefficient. It is represented mathematically as:
Dr. P V Siva Teja
Example: Two binary vectors, x and y, each containing 10 bits.
Vector x : 1 0 0 0 0 0 1 0 0 0
Vector y : 0 0 0 0 0 0 1 0 0 1
From these vectors, the following counts are derived based on pairwise bit comparisons:
Dr. P V Siva Teja
Dr. P V Siva Teja
Jaccard Similarity & Distance
The Jaccard Similarity (also known as the Jaccard Index or Jaccard Coefficient) and the Jaccard Distance are statistical measures used to understand the relationship between two sets of data. They are particularly popular in data science, ecology, and information retrieval (like comparing two documents).
Jaccard Similarity Formula
Dr. P V Siva Teja
Example:
Dr. P V Siva Teja
Cosine Similarity
Treats binary vectors as real vectors and computes the cosine of the angle between them.
Formula:
Dr. P V Siva Teja
Dice Similarity Coefficient
Gives more weight to matching 1's.
Dr. P V Siva Teja
Different classification algorithms based on distance measures
To determine the class of a sample by computing distances between data points in a feature space. These algorithms typically classify a sample by identifying which classes' samples are closest to it.
Different types of classification algorithms based on distance measures in ML are:
Dr. P V Siva Teja
1. KNN :-
Distance metrics are :-
Dr. P V Siva Teja
2. SVM (Support Vector Machine)
Dr. P V Siva Teja
3. Nearest Centroid Classifier
Distance Measures
Dr. P V Siva Teja
4. Learning Vector Quantization (LVQ)
Dr. P V Siva Teja
5. Radius Neighbors Classifier
Dr. P V Siva Teja
6. Fuzzy k-NN
Dr. P V Siva Teja
k-Nearest Neighbor Classifier
Dr. P V Siva Teja
Types of kNN:
1. Classification:
Dr. P V Siva Teja
2. Regression:-
kNN algorithm:-
Dr. P V Siva Teja
KNN FLOW CHART
Dr. P V Siva Teja
Selecting the Value of k in the KNN Algorithm
Methods to Select the Optimal k:
Dr. P V Siva Teja
Advantages:
Disadvantages:
Dr. P V Siva Teja
Radius Distance Nearest Neighbors Algorithm
Dr. P V Siva Teja
Radius-NN Algorithm:
Step 1: Select the number of R of radius.
Step 2: Calculate the Euclidean distance of R no. of neighbor.
Step 3: Take the radius as per for the calculated Euclidean distance.
Step 4: Among all the data points, count all no. of data points in each category.
Step 5: Assign the new data point where the maximum no. of data points in a category.
Step 6: Our model is ready.
It works by calculating the distance between a given data point and all the other data points in the dataset. It then identifies the data points that are within a specified radius of the given point.
The distance can be calculated using various metrics, such as Euclidean distance, Manhattan distance, or Cosine similarity.
Dr. P V Siva Teja
RNN FLOW CHART
Dr. P V Siva Teja
Advantages
Applications
Dr. P V Siva Teja
KNN Regression
Types:
Ex: If you're predicting house prices, KNN will use the average prices of the KNN to estimate the prices of a new house.
2. Classification: KNN assigns a class label to a new data point based on the majority class of its nearest neighbor.
Dr. P V Siva Teja
Algorithm:
Dr. P V Siva Teja
Dr. P V Siva Teja
Select the value of k in KNN algorithm:
Methods to select the optimal k:
Cross Validation:
Dr. P V Siva Teja
Dr. P V Siva Teja
Advantages:
Disadvantages:
Applications:
Dr. P V Siva Teja
Performance of Classifiers
In data science, classifier performance measures the predictive capabilities of ML models with metrics like:
Confusion Matrix
A Confusion matrix is a table i.e., often used to describe the performance of a classification model (or classifier) on a set of test data for which true values are known.
Dr. P V Siva Teja
Key Definitions
Predicted: Negative & Actual values: Positive
You predicted false (FN)
Predicted: Negative & Actual values: Negative
You predicted True (TN)
Predicted: Positive & Actual values: Positive
You predicted True (TP)
Predicted: Positive & Actual value: Negative
You predicted false (FP)
Dr. P V Siva Teja
1. Accuracy
Accuracy is one of the metrics for evaluating a classification model. Formally, accuracy could be defined as the number of correct predictions to a total number of predictions.
Dr. P V Siva Teja
2. Precision
Precision measures the accuracy of positive predictions. It answers the question, "Of all the items the model labeled as positive, how many are actually positive?"
Dr. P V Siva Teja
3. Recall
Recall measures the model's ability to find the positive instances. It shows the question, "Of all the actual positives, how many did the [model] correctly identify?"
Dr. P V Siva Teja
4. F1 Score
F1 Score is the harmonic mean of Precision & Recall. It balances the two metrics into a single number, making it especially useful when Precision & Recall are in trade-off.
Dr. P V Siva Teja
Example:
F21
F12
T33
Dr. P V Siva Teja
Dr. P V Siva Teja
Dr. P V Siva Teja
**************************************************
Dr. P V Siva Teja
Dr. P V Siva Teja
Regression Performance
Regression performance refers to how well a regression model predicts continuous numerical values like prices, temperatures, or sales figures.
Dr. P V Siva Teja
1. Mean Absolute Error (MAE)
Formula:
MAE =(Sum of data points)/(Total no. of data points)
Dr. P V Siva Teja
Dr. P V Siva Teja
2. Mean Squared Error (MSE)
Dr. P V Siva Teja
Dr. P V Siva Teja
3. R-Squared (Coefficient of Determination)
Dr. P V Siva Teja
4.Root Mean Squared Error (RMSE):
Dr. P V Siva Teja
Dr. P V Siva Teja