# modified of code from 100-Days-of-ML-Code
# Day 11 implementation of KNN
# importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# importing dataset
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
Y = dataset.iloc[:, 4].values
# splitting the dataset into the training sets and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
# Fitting KNN to the training sets
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors= 5, metric= 'euclidean', p=2)
classifier.fit(X_train, Y_train)
# Predicting the test sets
y_pred = classifier.predict(X_test)
# Making the confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test, y_pred)
print(cm)
# visualization
import seaborn as sns
sns.set()
f, ax= plt.subplots()
sns.heatmap(cm, annot = True, ax = ax)
ax.set_title('confusion matrix')
ax.set_xlabel('predict')
ax.set_ylabel('true')
plt.show()