KNN(K-Nearest-Neighbour) using Python-Jupyter Notebook

#import library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Create Data Friendly
df=pd.read_csv("E:\dataset\Social_Network_Ads.csv") 
df.head()

Output:


df.shape

Output:

(400, 5)

df.info()
img
#Checking missing values
df.isna().sum()

Output:

User ID                    0
Gender                    0
Age                          0
EstimatedSalary      0
Purchased               0
dtype: int64


#Choose X and y [X:Independent Variable, y: Dependent Variable]
X=df.iloc[:,[1,2,3]].values
y =  df.iloc[:, 4].values

#Perform encoding operation(Conver Gender column values into numeric)
from sklearn.preprocessing import LabelEncoder
obj = LabelEncoder()
df['Gender']=obj.fit_transform(df['Gender'])
print(X)

[[1 19 19000]
[1 35 20000]
[0 26 43000]
...
[0 50 20000]
[1 36 33000]
[0 49 36000]]


#Split dataframe into training and testing data 
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, 
random_state = 0
#Perform scaling operation
from sklearn.preprocessing import StandardScaler 
sc = StandardScaler()
X_train = sc.fit_transform(X_train) 
X_test = sc.transform(X_test)
#Create KNN Model
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
#Trained the model
model.fit(X_train, y_train)

Output:

KNeighborsClassifier()

#Predict the value
y_pred = model.predict(X_test) 
y_pred

Output:

array([0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1], dtype=int64)


#Find the accuracy
from sklearn.metrics import confusion_matrix,accuracy_score 
cm = confusion_matrix(y_test, y_pred)
ac = accuracy_score(y_test,y_pred) 

print(cm)
print(ac)

[[55 3]
[ 1 21]]
0.95


To get the dataset click Here


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext