KNN Algorithm - PPT (Autosaved)
KNN Algorithm - PPT (Autosaved)
We can see in the above diagram the three nearest neighbors of the
data point with black dot. Among those three, two of them lies in Red
KNN ALGORITHM
Supervised Learning :
Where a computer algorithm is trained on input data that has been labeled for a
particular output. (which means some data is already tagged with the correct
answer.)
All the other columns in the dataset are known as the Feature or Predictor
Variable or Independent Variable.
Supervised Learning is classified into two categories:
Classification: when the output variable is categorical i.e. with 2 or
more classes (yes/no, true/false). We make use of classification
Regression: Relationship between two or more variables where a
change in one variable is associated with a change in other variable.
predict the continuous values such as price, salary, age, etc.
How does KNN work ?
STEP 1: Select the number k of the neighbors
STEP 2: Calculate the Euclidean distance of k number of Neighbors
STEP 3: Take the K -nearest neighbors as per the calculated Euclidean
distance
STEP 4: Among these k neighbors ,count the no. of the data points in
each category
STEP 5: Assign the new data point to that category for which the no. of
the neighbors is maximum
STEP 6: Our model is ready
• PROS
It is a very simple and Easy to Implement.
A good value of k makes it robust to noise.
KNN learns a non- linear decision boundary.
There is no Training required.
• CONS
Inefficient.
Does not work well with high dimensions.
It does not handle categorial features very well.
Euclidean :
Manhattan :
Minkowski :
Implementation of KNN Algorithm in Python
1.Importing the modules
2.Creating Dataset.
3.Visualize the Dataset
4.Splitting Data into Training and Testing Datasets.
5.KNN Classifier Implementation.
6.Predictions for the KNN Classifiers.
7.Predict Accuracy for both k values.
8.Visualize Predictions.