SlideShare a Scribd company logo
KNN Classification
Algorithm
What’s in it for you?
Why do we need KNN?
What is KNN?
How do we choose the factor ‘K’?
When do we use KNN?
How does KNN Algorithm work?
Use Case: Predict whether a person will have diabetes or
not
Why KNN?
By now, we all know
Machine learning models
makes predictions by
learning from the past
data available
Input value
Machine Learning Model
Predicted Output
Is that a dog?
No dear, you can
differentiate
between a cat
and a dog based
on their
characteristics
No dear, you can
differentiate
between a cat
and a dog based
on their
characteristics
DOGSCATS
Sharp Claws, uses to climb
Smaller length of ears
Meows and purrs
Doesn’t love to play around
Dull Claws
Bigger length of ears
Barks
Loves to run around
No dear, you can
differentiate
between a cat
and a dog based
on their
characteristics
DOGSCATS
Length of ears 
Sharpnessofclaws
Now tell me if it
is a cat or a dog?
Now tell me if
it’s a cat or a
dog?
DOGSCATS
Length of ears 
Sharpnessofclaws
It’s features are
more like cats, it
must be a cat!
DOGSCATS
Length of ears 
Sharpofclaws
Why KNN?
Because KNN is based on
feature similarity, we can
do classification using KNN
Classifier!
Input value
KNN
Predicted Output
What is KNN?
What is KNN Algorithm?
KNN – K Nearest Neighbors, is one of the simplest Supervised Machine Learning algorithm
mostly used for
Classification
It classifies a data point based on how its
neighbors are classified
What is KNN Algorithm?
KNN stores all available cases and
classifies new cases based on a
similarity measure
Chloride Level 
SulphurDioxideLevel
RED or WHITE?
What is KNN Algorithm?
But, what is K?
Chloride Level 
SulphurDioxideLevel
RED or WHITE?
What is KNN Algorithm?
RED or WHITE?
k in KNN is a parameter that refers
to the number of nearest neighbors
to include in the majority voting
process
K=5
Chloride Level 
SulphurDioxideLevel
What is KNN Algorithm?
RED or WHITE?
A data point is classified by majority
votes from its 5 nearest neighbors
K=5
Chloride Level 
SulphurDioxideLevel
What is KNN Algorithm?
Chloride Level 
SulphurDioxideLevel RED
Here, the unknown point would be
classified as red, since 4 out of 5
neighbors are red
K=5
How do we choose ‘k’?
How do we choose the factor ‘k’?
?
K=3
New
Variable
KNN Algorithm is based on feature similarity: Choosing the right value of k is a process
called parameter tuning, and is important for better accuracy
How do we choose the factor ‘k’?
?
k=3
New
Variable
KNN Algorithm is based on feature similarity: Choosing the right value of k is a process
called parameter tuning, and is important for better accuracy
So at k=3, we can classify ‘?’ as
How do we choose the factor ‘k’?
?
k=3
k=7
New
Variable
KNN Algorithm is based on feature similarity: Choosing the right value of k is a process
called parameter tuning, and is important for better accuracy
But at k=7, we classify ‘?’ as
How do we choose the factor ‘k’?
?
K=1
K=3
New
Variable
KNN Algorithm is based on feature similarity: Choosing the right value of k is a process
called parameter tuning, and is important for better accuracy
So at k=3, we can classify ‘?’ as
The class of unknown data
point was at k=3 but
changed at k=7, so which k
should we choose?
How do we choose the factor ‘k’?
Odd value of K is selected to avoid confusion between two classes of data
Sqrt(n), where n is the total number of data points
To choose a value of k:
How do we choose the factor ‘k’?
Odd value of K is selected to avoid confusion between two classes of data
Sqrt(n), where n is the total number of data points
To choose a value of k:
Higher value of k has lesser
chance of error
When do we use KNN?
When do we use KNN Algorithm?
We can use KNN when
Dog
Data is labeled
When do we use KNN Algorithm?
We can use KNN when
Data is noise free
Data is labeled
Dog
Noise
Weight(x2) Height(y2) Class
51 167Underweight
62 182 one-fourty
69 176 23
64 173 hello kitty
65 172 Normal
When do we use KNN Algorithm?
We can use KNN when
Data is noise freeDataset is small
Data is labeled
Dog
Because KNN is a ‘lazy learner’ i.e.
doesn’t learn a discriminative function
from the training set
Noise
Weight(x2) Height(y2) Class
51 167Underweight
62 182 one-fourty
69 176 23
64 173 hello kitty
65 172 Normal
How does KNN Algorithm work?
How does KNN Algorithm work?
Consider a dataset having two variables: height (cm) & weight (kg) and each point is
classified as Normal or Underweight
Weight(x2) Height(y2) Class
51 167 Underweight
62 182 Normal
69 176 Normal
64 173 Normal
65 172 Normal
56 174 Underweight
58 169 Normal
57 173 Normal
55 170 Normal
How does KNN Algorithm work?
On the basis of the given data we have to classify the below set as Normal or
Underweight using KNN
Assuming, we
don’t know how to
calculate BMI!
57 kg 170 cm ?
How does KNN Algorithm work?
To find the nearest neighbors, we will calculate Euclidean
distance
But, what is
Euclidean distance?
How does KNN Algorithm work?
According to the Euclidean distance formula, the distance between two points in the plane with
coordinates (x, y) and (a, b) is given by:
dist(d)= √(x - a)² + (y - b)²
How does KNN Algorithm work?
Let’s calculate it to understand clearly:
Similarly, we will calculate Euclidean distance of unknown data point from
all the points in the dataset
dist(d1)= √(170-167)² + (57-51)² ~= 6.7
d1
dist(d2)= √(170-182)² + (57-62)² ~= 13
d2
dist(d3)= √(170-176)² + (57-69)² ~= 13.4
d3
Unknown data point
How does KNN Algorithm work?
Hence, we have calculated the Euclidean distance of unknown data point from all
the points as shown:
Where (x1, y1) = (57, 170) whose
class we have to classify
Weight(x2) Height(y2) Class Euclidean Distance
51 167 Underweight 6.7
62 182 Normal 13
69 176 Normal 13.4
64 173 Normal 7.6
65 172 Normal 8.2
56 174 Underweight 4.1
58 169 Normal 1.4
57 173 Normal 3
55 170 Normal 2
How does KNN Algorithm work?
Now, lets calculate the nearest neighbor at k=3
k = 3
57 kg 170 cm ?
Weight(x2) Height(y2) Class Euclidean Distance
51 167 Underweight 6.7
62 182 Normal 13
69 176 Normal 13.4
64 173 Normal 7.6
65 172 Normal 8.2
56 174 Underweight 4.1
58 169 Normal 1.4
57 173 Normal 3
55 170 Normal 2
Weight(x2) Height(y2) Class Euclidean Distance
51 167 Underweight 6.7
62 182 Normal 13
69 176 Normal 13.4
64 173 Normal 7.6
65 172 Normal 8.2
56 174 Underweight 4.1
58 169 Normal 1.4
57 173 Normal 3
55 170 Normal 2
How does KNN Algorithm work?
Now, lets calculate the nearest neighbor at k=3
k = 3
57 kg 170 cm ?
We have n=10,
And sqrt(10)=3.1
Hence, we have taken k=3
How does KNN Algorithm work?
So, majority neighbors are pointing towards ‘Normal’
Hence, as per KNN algorithm the class of (57, 170) should be ‘Normal’
k = 3
Class Euclidean Distance
Underweight 6.7
Normal 13
Normal 13.4
Normal 7.6
Normal 8.2
Underweight 4.1
Normal 1.4
Normal 3
Normal 2
Recap of KNN
Recap of KNN
• A positive integer k is specified, along with
a new sample
• We select the k entries in our database
which are closest to the new sample
• We find the most common classification of
these entries
• This is the classification we give to the
new sample
USE CASE: Predict Diabetes
KNN - Predict diabetes
Objective: Predict whether a person will be diagnosed with diabetes or not
We have a dataset of 768 people who were or were not
diagnosed with diabetes
KNN - Predict diabetes
Import the required Scikit-learn libraries as shown:
KNN - Predict diabetes
Load the dataset and have a look:
KNN - Predict diabetes
Values of columns like ‘Glucose’, BloodPressure’ cannot be accepted as zeroes because it will affect the outcome
We can replace such values with the mean of the respective column:
KNN - Predict diabetes
Before proceeding further, let’s split the dataset into train and test:
KNN - Predict diabetes
Feature Scaling:
Rule of thumb: Any algorithm that
computes distance or assumes
normality, scale your features!
KNN - Predict diabetes
Then define the model using KNeighborsClassifier and fit the train data in
the model
N_neighbors here is ‘K’
p is the power parameter to define
the metric used, which is ‘Euclidean’
in our case
KNN - Predict diabetes
There are other metrics
also to evaluate the
distance like Manhattan
distance , Minkowski
distance etc
KNN - Predict diabetes
Let’s predict the test results:
KNN - Predict diabetes
It’s important to evaluate the model, let’s use confusion matrix to do that:
KNN - Predict diabetes
Calculate accuracy of the model:
KNN - Predict diabetes
So, we have created a model using
KNN which can predict whether a
person will have diabetes or not
KNN - Predict diabetes
And accuracy of 80% tells us that it is
a pretty fair fit in the model!
Summary
Why we need knn? Eucledian distance Choosing the value of k
Knn classifier for diabetes predictionHow KNN works?
KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Beginners | Simplilearn

More Related Content

What's hot

K Nearest Neighbor Algorithm
K Nearest Neighbor AlgorithmK Nearest Neighbor Algorithm
K Nearest Neighbor Algorithm
Tharuka Vishwajith Sarathchandra
 
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Simplilearn
 
Hierarchical Clustering
Hierarchical ClusteringHierarchical Clustering
Hierarchical Clustering
Carlos Castillo (ChaTo)
 
Knn 160904075605-converted
Knn 160904075605-convertedKnn 160904075605-converted
Knn 160904075605-converted
rameswara reddy venkat
 
Decision tree and random forest
Decision tree and random forestDecision tree and random forest
Decision tree and random forest
Lippo Group Digital
 
Convolution Neural Network (CNN)
Convolution Neural Network (CNN)Convolution Neural Network (CNN)
Convolution Neural Network (CNN)
Suraj Aavula
 
Support Vector Machines ( SVM )
Support Vector Machines ( SVM ) Support Vector Machines ( SVM )
Support Vector Machines ( SVM )
Mohammad Junaid Khan
 
Classification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsClassification Based Machine Learning Algorithms
Classification Based Machine Learning Algorithms
Md. Main Uddin Rony
 
Unsupervised learning clustering
Unsupervised learning clusteringUnsupervised learning clustering
Unsupervised learning clustering
Arshad Farhad
 
Introduction to Deep Learning
Introduction to Deep LearningIntroduction to Deep Learning
Introduction to Deep Learning
Oswald Campesato
 
Multilayer perceptron
Multilayer perceptronMultilayer perceptron
Multilayer perceptron
omaraldabash
 
Knn Algorithm presentation
Knn Algorithm presentationKnn Algorithm presentation
Knn Algorithm presentation
RishavSharma112
 
Feature selection concepts and methods
Feature selection concepts and methodsFeature selection concepts and methods
Feature selection concepts and methods
Reza Ramezani
 
Bias and variance trade off
Bias and variance trade offBias and variance trade off
Bias and variance trade off
VARUN KUMAR
 
Feature selection
Feature selectionFeature selection
Feature selection
Dong Guo
 
Random forest
Random forestRandom forest
Random forest
Musa Hawamdah
 
Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector Machines
nextlib
 
K MEANS CLUSTERING
K MEANS CLUSTERINGK MEANS CLUSTERING
K MEANS CLUSTERING
singh7599
 
Machine Learning with Decision trees
Machine Learning with Decision treesMachine Learning with Decision trees
Machine Learning with Decision trees
Knoldus Inc.
 
Introduction to Recurrent Neural Network
Introduction to Recurrent Neural NetworkIntroduction to Recurrent Neural Network
Introduction to Recurrent Neural Network
Knoldus Inc.
 

What's hot (20)

K Nearest Neighbor Algorithm
K Nearest Neighbor AlgorithmK Nearest Neighbor Algorithm
K Nearest Neighbor Algorithm
 
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
 
Hierarchical Clustering
Hierarchical ClusteringHierarchical Clustering
Hierarchical Clustering
 
Knn 160904075605-converted
Knn 160904075605-convertedKnn 160904075605-converted
Knn 160904075605-converted
 
Decision tree and random forest
Decision tree and random forestDecision tree and random forest
Decision tree and random forest
 
Convolution Neural Network (CNN)
Convolution Neural Network (CNN)Convolution Neural Network (CNN)
Convolution Neural Network (CNN)
 
Support Vector Machines ( SVM )
Support Vector Machines ( SVM ) Support Vector Machines ( SVM )
Support Vector Machines ( SVM )
 
Classification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsClassification Based Machine Learning Algorithms
Classification Based Machine Learning Algorithms
 
Unsupervised learning clustering
Unsupervised learning clusteringUnsupervised learning clustering
Unsupervised learning clustering
 
Introduction to Deep Learning
Introduction to Deep LearningIntroduction to Deep Learning
Introduction to Deep Learning
 
Multilayer perceptron
Multilayer perceptronMultilayer perceptron
Multilayer perceptron
 
Knn Algorithm presentation
Knn Algorithm presentationKnn Algorithm presentation
Knn Algorithm presentation
 
Feature selection concepts and methods
Feature selection concepts and methodsFeature selection concepts and methods
Feature selection concepts and methods
 
Bias and variance trade off
Bias and variance trade offBias and variance trade off
Bias and variance trade off
 
Feature selection
Feature selectionFeature selection
Feature selection
 
Random forest
Random forestRandom forest
Random forest
 
Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector Machines
 
K MEANS CLUSTERING
K MEANS CLUSTERINGK MEANS CLUSTERING
K MEANS CLUSTERING
 
Machine Learning with Decision trees
Machine Learning with Decision treesMachine Learning with Decision trees
Machine Learning with Decision trees
 
Introduction to Recurrent Neural Network
Introduction to Recurrent Neural NetworkIntroduction to Recurrent Neural Network
Introduction to Recurrent Neural Network
 

Similar to KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Beginners | Simplilearn

K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
Abdullah al Mamun
 
Machine Learning Algorithm - KNN
Machine Learning Algorithm - KNNMachine Learning Algorithm - KNN
Machine Learning Algorithm - KNN
Kush Kulshrestha
 
K- Nearest Neighbor Approach
K- Nearest Neighbor ApproachK- Nearest Neighbor Approach
K- Nearest Neighbor Approach
Kumud Arora
 
Data Science Job Required Skill Analysis
Data Science Job Required Skill AnalysisData Science Job Required Skill Analysis
Data Science Job Required Skill Analysis
Harsh Kevadia
 
Scalable k-means plus plus
Scalable k-means plus plusScalable k-means plus plus
Scalable k-means plus plus
Prabin Giri, PhD Student
 
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
Edureka!
 
K nearest neighbor algorithm
  K nearest neighbor algorithm  K nearest neighbor algorithm
K nearest neighbor algorithm
Learnbay Datascience
 
K nearest neighbor: classify by closest training points.
K nearest neighbor: classify by closest training points.K nearest neighbor: classify by closest training points.
K nearest neighbor: classify by closest training points.
mcafarewell2e
 
Knn
KnnKnn
Ml9 introduction to-unsupervised_learning_and_clustering_methods
Ml9 introduction to-unsupervised_learning_and_clustering_methodsMl9 introduction to-unsupervised_learning_and_clustering_methods
Ml9 introduction to-unsupervised_learning_and_clustering_methods
ankit_ppt
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
19526YuvaKumarIrigi
 
KNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptx
KNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptxKNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptx
KNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptx
Nishant83346
 
K means clustering algorithm
K means clustering algorithmK means clustering algorithm
K means clustering algorithm
Darshak Mehta
 
Clustering techniques
Clustering techniquesClustering techniques
Clustering techniques
talktoharry
 
Enhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial DatasetEnhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial Dataset
AlaaZ
 
K Means Clustering in ML.pptx
K Means Clustering in ML.pptxK Means Clustering in ML.pptx
K Means Clustering in ML.pptx
Ramakrishna Reddy Bijjam
 
Ml10 dimensionality reduction-and_advanced_topics
Ml10 dimensionality reduction-and_advanced_topicsMl10 dimensionality reduction-and_advanced_topics
Ml10 dimensionality reduction-and_advanced_topics
ankit_ppt
 
07 learning
07 learning07 learning
07 learning
ankit_ppt
 
What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...
What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...
What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...
Smarten Augmented Analytics
 
Data analysis of weather forecasting
Data analysis of weather forecastingData analysis of weather forecasting
Data analysis of weather forecasting
Trupti Shingala, WAS, CPACC, CPWA, JAWS, CSM
 

Similar to KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Beginners | Simplilearn (20)

K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
 
Machine Learning Algorithm - KNN
Machine Learning Algorithm - KNNMachine Learning Algorithm - KNN
Machine Learning Algorithm - KNN
 
K- Nearest Neighbor Approach
K- Nearest Neighbor ApproachK- Nearest Neighbor Approach
K- Nearest Neighbor Approach
 
Data Science Job Required Skill Analysis
Data Science Job Required Skill AnalysisData Science Job Required Skill Analysis
Data Science Job Required Skill Analysis
 
Scalable k-means plus plus
Scalable k-means plus plusScalable k-means plus plus
Scalable k-means plus plus
 
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
 
K nearest neighbor algorithm
  K nearest neighbor algorithm  K nearest neighbor algorithm
K nearest neighbor algorithm
 
K nearest neighbor: classify by closest training points.
K nearest neighbor: classify by closest training points.K nearest neighbor: classify by closest training points.
K nearest neighbor: classify by closest training points.
 
Knn
KnnKnn
Knn
 
Ml9 introduction to-unsupervised_learning_and_clustering_methods
Ml9 introduction to-unsupervised_learning_and_clustering_methodsMl9 introduction to-unsupervised_learning_and_clustering_methods
Ml9 introduction to-unsupervised_learning_and_clustering_methods
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
 
KNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptx
KNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptxKNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptx
KNN CLASSIFIER, INTRODUCTION TO K-NEAREST NEIGHBOR ALGORITHM.pptx
 
K means clustering algorithm
K means clustering algorithmK means clustering algorithm
K means clustering algorithm
 
Clustering techniques
Clustering techniquesClustering techniques
Clustering techniques
 
Enhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial DatasetEnhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial Dataset
 
K Means Clustering in ML.pptx
K Means Clustering in ML.pptxK Means Clustering in ML.pptx
K Means Clustering in ML.pptx
 
Ml10 dimensionality reduction-and_advanced_topics
Ml10 dimensionality reduction-and_advanced_topicsMl10 dimensionality reduction-and_advanced_topics
Ml10 dimensionality reduction-and_advanced_topics
 
07 learning
07 learning07 learning
07 learning
 
What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...
What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...
What is the KMeans Clustering Algorithm and How Does an Enterprise Use it to ...
 
Data analysis of weather forecasting
Data analysis of weather forecastingData analysis of weather forecasting
Data analysis of weather forecasting
 

More from Simplilearn

🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...
🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...
🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...
Simplilearn
 
Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...
Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...
Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...
Simplilearn
 
How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...
How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...
How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...
Simplilearn
 
Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...
Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...
Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...
Simplilearn
 
🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...
🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...
🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...
Simplilearn
 
🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...
🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...
🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...
Simplilearn
 
🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...
🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...
🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...
Simplilearn
 
🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...
🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...
🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...
Simplilearn
 
Project Manager vs Program Manager - What’s the Difference ? | Project Manage...
Project Manager vs Program Manager - What’s the Difference ? | Project Manage...Project Manager vs Program Manager - What’s the Difference ? | Project Manage...
Project Manager vs Program Manager - What’s the Difference ? | Project Manage...
Simplilearn
 
Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...
Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...
Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...
Simplilearn
 
🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn
🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn
🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn
Simplilearn
 
ChatGPT in Cybersecurity
ChatGPT in CybersecurityChatGPT in Cybersecurity
ChatGPT in Cybersecurity
Simplilearn
 
Whatis SQL Injection.pptx
Whatis SQL Injection.pptxWhatis SQL Injection.pptx
Whatis SQL Injection.pptx
Simplilearn
 
Top 5 High Paying Cloud Computing Jobs in 2023
 Top 5 High Paying Cloud Computing Jobs in 2023  Top 5 High Paying Cloud Computing Jobs in 2023
Top 5 High Paying Cloud Computing Jobs in 2023
Simplilearn
 
Types Of Cloud Jobs In 2024
Types Of Cloud Jobs In 2024Types Of Cloud Jobs In 2024
Types Of Cloud Jobs In 2024
Simplilearn
 
Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...
Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...
Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...
Simplilearn
 
What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...
What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...
What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...
Simplilearn
 
Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...
Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...
Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...
Simplilearn
 
React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...
React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...
React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...
Simplilearn
 
Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...
Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...
Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...
Simplilearn
 

More from Simplilearn (20)

🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...
🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...
🔥 Cyber Security Engineer Vs Ethical Hacker: What's The Difference | Cybersec...
 
Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...
Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...
Top 10 Companies Hiring Machine Learning Engineer | Machine Learning Jobs | A...
 
How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...
How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...
How to Become Strategy Manager 2023 ? | Strategic Management | Roadmap | Simp...
 
Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...
Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...
Top 20 Devops Engineer Interview Questions And Answers For 2023 | Devops Tuto...
 
🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...
🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...
🔥 Big Data Engineer Roadmap 2023 | How To Become A Big Data Engineer In 2023 ...
 
🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...
🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...
🔥 AI Engineer Resume For 2023 | CV For AI Engineer | AI Engineer CV 2023 | Si...
 
🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...
🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...
🔥 Top 5 Skills For Data Engineer In 2023 | Data Engineer Skills Required For ...
 
🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...
🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...
🔥 6 Reasons To Become A Data Engineer | Why You Should Become A Data Engineer...
 
Project Manager vs Program Manager - What’s the Difference ? | Project Manage...
Project Manager vs Program Manager - What’s the Difference ? | Project Manage...Project Manager vs Program Manager - What’s the Difference ? | Project Manage...
Project Manager vs Program Manager - What’s the Difference ? | Project Manage...
 
Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...
Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...
Deloitte Interview Questions And Answers | Top 45 Deloitte Interview Question...
 
🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn
🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn
🔥 Deep Learning Roadmap 2024 | Deep Learning Career Path 2024 | Simplilearn
 
ChatGPT in Cybersecurity
ChatGPT in CybersecurityChatGPT in Cybersecurity
ChatGPT in Cybersecurity
 
Whatis SQL Injection.pptx
Whatis SQL Injection.pptxWhatis SQL Injection.pptx
Whatis SQL Injection.pptx
 
Top 5 High Paying Cloud Computing Jobs in 2023
 Top 5 High Paying Cloud Computing Jobs in 2023  Top 5 High Paying Cloud Computing Jobs in 2023
Top 5 High Paying Cloud Computing Jobs in 2023
 
Types Of Cloud Jobs In 2024
Types Of Cloud Jobs In 2024Types Of Cloud Jobs In 2024
Types Of Cloud Jobs In 2024
 
Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...
Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...
Top 12 AI Technologies To Learn 2024 | Top AI Technologies in 2024 | AI Trend...
 
What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...
What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...
What is LSTM ?| Long Short Term Memory Explained with Example | Deep Learning...
 
Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...
Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...
Top 10 Chat GPT Use Cases | ChatGPT Applications | ChatGPT Tutorial For Begin...
 
React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...
React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...
React JS Vs Next JS - What's The Difference | Next JS Tutorial For Beginners ...
 
Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...
Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...
Backpropagation in Neural Networks | Back Propagation Algorithm with Examples...
 

Recently uploaded

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
christianmathematics
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 

Recently uploaded (20)

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 

KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Beginners | Simplilearn

  • 2. What’s in it for you? Why do we need KNN? What is KNN? How do we choose the factor ‘K’? When do we use KNN? How does KNN Algorithm work? Use Case: Predict whether a person will have diabetes or not
  • 3. Why KNN? By now, we all know Machine learning models makes predictions by learning from the past data available Input value Machine Learning Model Predicted Output
  • 4. Is that a dog?
  • 5. No dear, you can differentiate between a cat and a dog based on their characteristics
  • 6. No dear, you can differentiate between a cat and a dog based on their characteristics DOGSCATS Sharp Claws, uses to climb Smaller length of ears Meows and purrs Doesn’t love to play around Dull Claws Bigger length of ears Barks Loves to run around
  • 7. No dear, you can differentiate between a cat and a dog based on their characteristics DOGSCATS Length of ears  Sharpnessofclaws
  • 8. Now tell me if it is a cat or a dog?
  • 9. Now tell me if it’s a cat or a dog? DOGSCATS Length of ears  Sharpnessofclaws
  • 10. It’s features are more like cats, it must be a cat!
  • 11. DOGSCATS Length of ears  Sharpofclaws
  • 12. Why KNN? Because KNN is based on feature similarity, we can do classification using KNN Classifier! Input value KNN Predicted Output
  • 14. What is KNN Algorithm? KNN – K Nearest Neighbors, is one of the simplest Supervised Machine Learning algorithm mostly used for Classification It classifies a data point based on how its neighbors are classified
  • 15. What is KNN Algorithm? KNN stores all available cases and classifies new cases based on a similarity measure Chloride Level  SulphurDioxideLevel RED or WHITE?
  • 16. What is KNN Algorithm? But, what is K? Chloride Level  SulphurDioxideLevel RED or WHITE?
  • 17. What is KNN Algorithm? RED or WHITE? k in KNN is a parameter that refers to the number of nearest neighbors to include in the majority voting process K=5 Chloride Level  SulphurDioxideLevel
  • 18. What is KNN Algorithm? RED or WHITE? A data point is classified by majority votes from its 5 nearest neighbors K=5 Chloride Level  SulphurDioxideLevel
  • 19. What is KNN Algorithm? Chloride Level  SulphurDioxideLevel RED Here, the unknown point would be classified as red, since 4 out of 5 neighbors are red K=5
  • 20. How do we choose ‘k’?
  • 21. How do we choose the factor ‘k’? ? K=3 New Variable KNN Algorithm is based on feature similarity: Choosing the right value of k is a process called parameter tuning, and is important for better accuracy
  • 22. How do we choose the factor ‘k’? ? k=3 New Variable KNN Algorithm is based on feature similarity: Choosing the right value of k is a process called parameter tuning, and is important for better accuracy So at k=3, we can classify ‘?’ as
  • 23. How do we choose the factor ‘k’? ? k=3 k=7 New Variable KNN Algorithm is based on feature similarity: Choosing the right value of k is a process called parameter tuning, and is important for better accuracy But at k=7, we classify ‘?’ as
  • 24. How do we choose the factor ‘k’? ? K=1 K=3 New Variable KNN Algorithm is based on feature similarity: Choosing the right value of k is a process called parameter tuning, and is important for better accuracy So at k=3, we can classify ‘?’ as The class of unknown data point was at k=3 but changed at k=7, so which k should we choose?
  • 25. How do we choose the factor ‘k’? Odd value of K is selected to avoid confusion between two classes of data Sqrt(n), where n is the total number of data points To choose a value of k:
  • 26. How do we choose the factor ‘k’? Odd value of K is selected to avoid confusion between two classes of data Sqrt(n), where n is the total number of data points To choose a value of k: Higher value of k has lesser chance of error
  • 27. When do we use KNN?
  • 28. When do we use KNN Algorithm? We can use KNN when Dog Data is labeled
  • 29. When do we use KNN Algorithm? We can use KNN when Data is noise free Data is labeled Dog Noise Weight(x2) Height(y2) Class 51 167Underweight 62 182 one-fourty 69 176 23 64 173 hello kitty 65 172 Normal
  • 30. When do we use KNN Algorithm? We can use KNN when Data is noise freeDataset is small Data is labeled Dog Because KNN is a ‘lazy learner’ i.e. doesn’t learn a discriminative function from the training set Noise Weight(x2) Height(y2) Class 51 167Underweight 62 182 one-fourty 69 176 23 64 173 hello kitty 65 172 Normal
  • 31. How does KNN Algorithm work?
  • 32. How does KNN Algorithm work? Consider a dataset having two variables: height (cm) & weight (kg) and each point is classified as Normal or Underweight Weight(x2) Height(y2) Class 51 167 Underweight 62 182 Normal 69 176 Normal 64 173 Normal 65 172 Normal 56 174 Underweight 58 169 Normal 57 173 Normal 55 170 Normal
  • 33. How does KNN Algorithm work? On the basis of the given data we have to classify the below set as Normal or Underweight using KNN Assuming, we don’t know how to calculate BMI! 57 kg 170 cm ?
  • 34. How does KNN Algorithm work? To find the nearest neighbors, we will calculate Euclidean distance But, what is Euclidean distance?
  • 35. How does KNN Algorithm work? According to the Euclidean distance formula, the distance between two points in the plane with coordinates (x, y) and (a, b) is given by: dist(d)= √(x - a)² + (y - b)²
  • 36. How does KNN Algorithm work? Let’s calculate it to understand clearly: Similarly, we will calculate Euclidean distance of unknown data point from all the points in the dataset dist(d1)= √(170-167)² + (57-51)² ~= 6.7 d1 dist(d2)= √(170-182)² + (57-62)² ~= 13 d2 dist(d3)= √(170-176)² + (57-69)² ~= 13.4 d3 Unknown data point
  • 37. How does KNN Algorithm work? Hence, we have calculated the Euclidean distance of unknown data point from all the points as shown: Where (x1, y1) = (57, 170) whose class we have to classify Weight(x2) Height(y2) Class Euclidean Distance 51 167 Underweight 6.7 62 182 Normal 13 69 176 Normal 13.4 64 173 Normal 7.6 65 172 Normal 8.2 56 174 Underweight 4.1 58 169 Normal 1.4 57 173 Normal 3 55 170 Normal 2
  • 38. How does KNN Algorithm work? Now, lets calculate the nearest neighbor at k=3 k = 3 57 kg 170 cm ? Weight(x2) Height(y2) Class Euclidean Distance 51 167 Underweight 6.7 62 182 Normal 13 69 176 Normal 13.4 64 173 Normal 7.6 65 172 Normal 8.2 56 174 Underweight 4.1 58 169 Normal 1.4 57 173 Normal 3 55 170 Normal 2
  • 39. Weight(x2) Height(y2) Class Euclidean Distance 51 167 Underweight 6.7 62 182 Normal 13 69 176 Normal 13.4 64 173 Normal 7.6 65 172 Normal 8.2 56 174 Underweight 4.1 58 169 Normal 1.4 57 173 Normal 3 55 170 Normal 2 How does KNN Algorithm work? Now, lets calculate the nearest neighbor at k=3 k = 3 57 kg 170 cm ? We have n=10, And sqrt(10)=3.1 Hence, we have taken k=3
  • 40. How does KNN Algorithm work? So, majority neighbors are pointing towards ‘Normal’ Hence, as per KNN algorithm the class of (57, 170) should be ‘Normal’ k = 3 Class Euclidean Distance Underweight 6.7 Normal 13 Normal 13.4 Normal 7.6 Normal 8.2 Underweight 4.1 Normal 1.4 Normal 3 Normal 2
  • 41. Recap of KNN Recap of KNN • A positive integer k is specified, along with a new sample • We select the k entries in our database which are closest to the new sample • We find the most common classification of these entries • This is the classification we give to the new sample
  • 42. USE CASE: Predict Diabetes
  • 43. KNN - Predict diabetes Objective: Predict whether a person will be diagnosed with diabetes or not We have a dataset of 768 people who were or were not diagnosed with diabetes
  • 44. KNN - Predict diabetes Import the required Scikit-learn libraries as shown:
  • 45. KNN - Predict diabetes Load the dataset and have a look:
  • 46. KNN - Predict diabetes Values of columns like ‘Glucose’, BloodPressure’ cannot be accepted as zeroes because it will affect the outcome We can replace such values with the mean of the respective column:
  • 47. KNN - Predict diabetes Before proceeding further, let’s split the dataset into train and test:
  • 48. KNN - Predict diabetes Feature Scaling: Rule of thumb: Any algorithm that computes distance or assumes normality, scale your features!
  • 49. KNN - Predict diabetes Then define the model using KNeighborsClassifier and fit the train data in the model N_neighbors here is ‘K’ p is the power parameter to define the metric used, which is ‘Euclidean’ in our case
  • 50. KNN - Predict diabetes There are other metrics also to evaluate the distance like Manhattan distance , Minkowski distance etc
  • 51. KNN - Predict diabetes Let’s predict the test results:
  • 52. KNN - Predict diabetes It’s important to evaluate the model, let’s use confusion matrix to do that:
  • 53. KNN - Predict diabetes Calculate accuracy of the model:
  • 54. KNN - Predict diabetes So, we have created a model using KNN which can predict whether a person will have diabetes or not
  • 55. KNN - Predict diabetes And accuracy of 80% tells us that it is a pretty fair fit in the model!
  • 56. Summary Why we need knn? Eucledian distance Choosing the value of k Knn classifier for diabetes predictionHow KNN works?

Editor's Notes

  1. Remove title case
  2. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  3. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  4. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  5. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  6. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  7. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  8. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  9. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  10. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  11. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  12. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  13. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  14. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  15. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  16. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  17. ,. Unlike other regression algorithms where we have one or more than one dependent variables
  18. ,. Unlike other regression algorithms where we have one or more than one dependent variables We have a small data set as KNN is a lazy learner so it becomes slow with large datasets
  19. ,. Unlike other regression algorithms where we have one or more than one dependent variables We have a small data set as KNN is a lazy learner so it becomes slow with large datasets
  20. ,. Unlike other regression algorithms where we have one or more than one dependent variables We have a small data set as KNN is a lazy learner so it becomes slow with large datasets
  21.  Most of the times, your dataset will contain features highly varying in magnitudes, units and range. we need to bring all features to the same level of magnitudes. This can be acheived by scaling
  22. The F1 score is the harmonic average of the precision and recall, where an F1 score reaches its best value at 1 (perfect precision and recall) and worst at 0.