'How to plot graph of training error rate in KNN

I have the following code:

all_accuracies <- c(accs1, accs2)

all_accuracies

kvalues <- c(1,3,5,7,11)

all_data <- data.frame(kvalues,all_accuracies)

all_data

library(ggplot2)

crossData <- data.frame((k = 1/all_data$kvalues), Accuracy_rate = all_data$all_accuracies)

crossData[1:7] <- "Training Data"

crossData[8:14] <- "Cross Validated Data"

ggplot(crossData, aes(x=k, y=Accuracy_rate)) + geom_line() + geom_point() + labs(x="1/k", y="Accuracy Rate")

On here values of crossData from 1:7 are of normal training data and values from 8:14 are of cross-validated training data.

Basically here, accs1 and accs2 contain accuracy rate of training set and accuracy rate of cross validated training set.

I want to show the accuracy rate over training set and cross-validated accuracy rate of the set for each value of k. I want to plot a graph that looks similar to:

enter image description here

How can I code to draw this plot? I'm having troubles with it.

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source