3 The Code Below Shows The K Nearest Neighbour Knn Algorithm On 3 Species Of Iris Iris Setosa Iris Virginica Iris 1 (43.74 KiB) Viewed 30 times
3 The Code Below Shows The K Nearest Neighbour Knn Algorithm On 3 Species Of Iris Iris Setosa Iris Virginica Iris 2 (21.92 KiB) Viewed 30 times
3) The code below shows the K-Nearest Neighbour (kNN) algorithm on 3 species of Iris (Iris setosa, Iris virginica, Iris versicolor). Based on the code provide, please explains algorithm procedure to obtain the best approximation of k i. (5 Marks) ii. the output of confusion matrix (5 Marks) the best numbers of k and how we can determine the threshold values of k (maximum number of accuracy) 111. (10 Marks) *you may run the code to answer (ii) & (iii). For (iii): required modification of the code # Installing Packages install.packages("e1071") install.packages("caTools") install.packages ("class") # Loading package library(e1071) library(caTools) library(class) # Loading data data(iris) head(iris) # Splitting data into train # and test data split <-sample.split(iris, SplitRatio = 0.7) train_cl <-subset(iris, split == "TRUE") test_cl <- subset(iris, split == "FALSE") # Feature Scaling train_scale <- scale(train_cl[, 1:4]) test_scale <- scale(test_cl[, 1:4]) # Fitting KNN Model # to training dataset classifier_knn <- knn(train = train_scale, classifier_knn test = test_scale, cl = train_cl$Species, k=1) # Confusiin Matrix cm <- table(test_cl$Species, classifier_knn) cm
# Model Evaluation - Choosing K # Calculate out of Sample error misClassError <- mean(classifier_knn != test_cl$Species) print (paste('Accuracy=', 1-misClassError)) #K=2 classifier_knn <- knn(train = train_scale, test = test_scale, cl = train_cl$Species, k=2) misClassError <- mean(classifier_knn != test_cl$Species) print(paste('Accuracy=', 1-misClassError))
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!