Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 3 - Classification/K Nearest Neighbors/knn.R
1009 views
1
# K-Nearest Neighbors (K-NN)
2
3
# Importing the dataset
4
dataset = read.csv('Social_Network_Ads.csv')
5
dataset = dataset[3:5]
6
7
# Encoding the target feature as factor
8
dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))
9
10
# Splitting the dataset into the Training set and Test set
11
# install.packages('caTools')
12
library(caTools)
13
set.seed(123)
14
split = sample.split(dataset$Purchased, SplitRatio = 0.75)
15
training_set = subset(dataset, split == TRUE)
16
test_set = subset(dataset, split == FALSE)
17
18
# Feature Scaling
19
training_set[-3] = scale(training_set[-3])
20
test_set[-3] = scale(test_set[-3])
21
22
# Fitting K-NN to the Training set and Predicting the Test set results
23
library(class)
24
y_pred = knn(train = training_set[, -3],
25
test = test_set[, -3],
26
cl = training_set[, 3],
27
k = 5,
28
prob = TRUE)
29
30
# Making the Confusion Matrix
31
cm = table(test_set[, 3], y_pred)
32
33
# Visualising the Training set results
34
library(ElemStatLearn)
35
set = training_set
36
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
37
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
38
grid_set = expand.grid(X1, X2)
39
colnames(grid_set) = c('Age', 'EstimatedSalary')
40
y_grid = knn(train = training_set[, -3], test = grid_set, cl = training_set[, 3], k = 5)
41
plot(set[, -3],
42
main = 'K-NN (Training set)',
43
xlab = 'Age', ylab = 'Estimated Salary',
44
xlim = range(X1), ylim = range(X2))
45
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
46
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
47
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
48
49
# Visualising the Test set results
50
library(ElemStatLearn)
51
set = test_set
52
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
53
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
54
grid_set = expand.grid(X1, X2)
55
colnames(grid_set) = c('Age', 'EstimatedSalary')
56
y_grid = knn(train = training_set[, -3], test = grid_set, cl = training_set[, 3], k = 5)
57
plot(set[, -3],
58
main = 'K-NN (Test set)',
59
xlab = 'Age', ylab = 'Estimated Salary',
60
xlim = range(X1), ylim = range(X2))
61
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
62
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
63
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
64