04 KNN Implementation
04 KNN Implementation
Before Going To Working of Knn I am showing you to all distance that Knn
Use
All Distance:
There are Three Types Of Distance
Hamming Distance
Hamming distance is used for categorical variables. In simple terms it tells us if the two categorical
variables are same or not.
Importing Libraries
In [1]:
#importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings("ignore")
data = pd.read_csv('data_cleaned.csv')
data.shape
Out[2]:
(891, 25)
In [3]:
data.head()
Out[3]:
Survived Age Fare Pclass_1 Pclass_2 Pclass_3 Sex_female Sex_male SibSp_0 Sib
0 0 22.0 7.2500 0 0 1 0 1 0
1 1 38.0 71.2833 1 0 0 1 0 0
2 1 26.0 7.9250 0 0 1 1 0 1
3 1 35.0 53.1000 1 0 0 1 0 0
4 0 35.0 8.0500 0 0 1 0 1 1
5 rows × 25 columns
In [4]:
Out[4]:
In [5]:
In [6]:
x.head()
Out[7]:
5 rows × 24 columns
In [8]:
In [9]:
In [10]:
#See In metric We use all The above Distance To Calculate. here i Use euclidean distance .
return test_error
In [12]:
#Defining K range
k = range(6, 20, 2)
In [13]:
In [14]:
Out[14]:
In [15]:
In [16]:
See Here Before Randomly Put K Value The Score is not well. But After Doing Elbow Method And Find
Appropriate K-Value Then the score increase to 75.
So Now you understood how imporatant K-Value For Knn Algorithm.
This All About Knn Algorithm