Explain KNN along with a case study in R

1.0K    Asked by FelicaLaplaca in Data Science , Asked on Dec 17, 2019
Answered by Felica Laplaca

Initially, we will import the dataset

# Importing the dataset

dataset = read.csv('Social_Network_Ads.csv')

dataset = dataset[3:5]

Let us see the head of the dataset and encode the target variable as factors

# Encoding the target feature as factor

dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))

Now we will split the dataset for training and testing

# Splitting the dataset into the Training set and Test set

# install.packages('caTools')

library(caTools)

set.seed(123)

split = sample.split(dataset$Purchased, SplitRatio = 0.75)

training_set = subset(dataset, split == TRUE)

test_set = subset(dataset, split == FALSE)

Now we will normalize the feature variable as required.

# Feature Scaling

training_set[-3] = scale(training_set[-3])

test_set[-3] = scale(test_set[-3])

Now we fit the dataset into a KNN classifier

# Fitting K-NN to the Training set and Predicting the Test set results

library(class)

y_pred = knn(train = training_set[, -3],

             test = test_set[, -3],

             cl = training_set[, 3],

             k = 5,

             prob = TRUE)

The final step is the evaluation of the model which tells us how good the model will perform.

# Making the Confusion Matrix

cm = table(test_set[, 3], y_pred)



Your Answer

Interviews

Parent Categories